Giter Site home page Giter Site logo

normalizr's Introduction

Normalizr

Gem Version Build Status Code Climate Test Coverage

The attribute_normalizer replacement.

Synopsis

Attribute normalizer doesn't normalize overloaded methods correctly. Example:

class Phone
  include AttributeNormalizer

  attr_accessor :number
  normalize_attribute :number

  def number=(value)
    @number = value
  end
end

number will never be normalized as expected. Normalizr resolves this problem and doesn't pollute target object namespace.

Magic based on ruby's prepend feature, so it requires 2.0 or higher version.

Installation

Add this line to your application's Gemfile:

gem 'normalizr'

And then execute:

$ bundle

Or install it yourself as:

$ gem install normalizr

Usage

Specify default normalizers:

Normalizr.configure do
  default :strip, :blank
end

Register custom normalizer:

Normalizr.configure do
  add :titleize do |value|
    String === value ? value.titleize : value
  end

  add :truncate do |value, options|
    if String === value
      options.reverse_merge!(length: 30, omission: '...')
      l = options[:length] - options[:omission].mb_chars.length
      chars = value.mb_chars
      (chars.length > options[:length] ? chars[0...l] + options[:omission] : value).to_s
    else
      value
    end
  end

  add :indent do |value, amount = 2|
    if String === value
      value.indent(amount)
    else
      value
    end
  end
end

Add attributes normalization:

class User < ActiveRecord::Base
  normalize :first_name, :last_name, :about # with default normalizers
  normalize :email, with: :downcase

  # you can use default and custom normalizers together
  normalize :middle_name, with: [:default, :titleize]

  # supports `normalize_attribute` and `normalize_attributes` as well
  normalize_attribute :skype

  # array normalization is supported too
  normalize :skills
end

user = User.new(first_name: '', last_name: '', middle_name: 'elizabeth ', skills: [nil, '', ' ruby'])
user.email = "[email protected]"

user.first_name
#=> nil
user.last_name
#=> nil
user.middle_name
#=> "Elizabeth"
user.email
#=> "[email protected]"
user.skills
#=> ["ruby"]
class SMS
  include Normalizr::Concern

  attr_accessor :phone, :message

  normalize :phone, with: :phone
  normalize :message

  def initialize(phone, message)
    self.phone   = phone
    self.message = message
  end
end

sms = SMS.new("+1 (810) 555-0000", "It works \n")
sms.phone
#=> "18105550000"
sms.message
#=> "It works"

You can also use if/unless options (they accept a symbol (method name) or proc):

class Book
  include Normalizr::Concerns

  attr_accessor :author, :description, :date

  normalize :author, if: :author_should_be_normalized?
  normalize :description, unless: :description_should_not_be_normalized?

  normalize :author, if: -> { date.today? }
end

Normalize values outside of class:

Normalizr.normalize(value)
Normalizr.normalize(value, :strip, :blank)
Normalizr.normalize(value, :strip, truncate: { length: 20 })

ORMs

Normalizr automatically loads into:

  • ActiveRecord
  • Mongoid

RSpec matcher

describe User do
  it { should normalize(:name) }
  it { should normalize(:phone).from('+1 (810) 555-0000').to('18105550000') }
  it { should normalize(:email).from('[email protected]').to('[email protected]') }
end

Built-in normalizers

  • blank
  • boolean
  • capitalize
  • control_chars
  • downcase
  • phone
  • squish
  • strip
  • upcase
  • whitespace

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Thanks

Special thanks to Michael Deering for original idea.

normalizr's People

Contributors

badlamer avatar cbillen avatar dmeremyanin avatar jackc avatar sikachu avatar subbbotin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

normalizr's Issues

NoMethodError when assigning a normalized attribute through a relation on Rails 4.2

Given the following model:

class Book < ActiveRecord::Base
  normalize :title
end

This will not work (if there is no book with that title):

irb(main):002:0> Book.where(title: 'ruby').first_or_initialize.title
  Book Load (0.1ms)  SELECT  "books".* FROM "books" WHERE "books"."title" = ?  ORDER BY "books"."id" ASC LIMIT 1  [["title", "ruby"]]
NoMethodError: super: no superclass method `title=' for #<Book id: nil, title: nil, created_at: nil, updated_at: nil>
    from /Users/Emma/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activemodel-4.2.0/lib/active_model/attribute_methods.rb:430:in `method_missing'
    from /Users/Emma/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/normalizr-0.1.0/lib/normalizr/concern.rb:16:in `block (3 levels) in normalize'
    from /Users/Emma/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/scoping.rb:26:in `block in populate_with_current_scope_attributes'
...

But after creating a new object it will:

irb(main):001:0> Book.new
=> #<Book id: nil, title: nil, created_at: nil, updated_at: nil>
irb(main):002:0> Book.where(title: 'ruby').first_or_initialize.title
  Book Load (0.2ms)  SELECT  "books".* FROM "books" WHERE "books"."title" = ?  ORDER BY "books"."id" ASC LIMIT 1  [["title", "ruby"]]
=> "ruby"

Cannot normalize an array itself? Per-element is not optional?

I am a longtime user of Normalizr on a few projects. I recently upgraded an old project that had a large number (30+) custom normalizations from version 1.1.1 into a more recent version and fell victim to an issue with array normalization. Specifically, there is no way to normalize an array other than to normalize per-element, per lib/normalizr.rb:

def process(obj, name, options)
  if Array === obj
    obj.map { |item| process(item, name, options) }.tap do |ary|
      ary.compact! if name == :blank
    end
  else
    find(name).call(obj, options)
  end
end

This project had many years of adding its own array_of_foobar normalizations to account for the lack of array support in the past. Thus the feature is very nice to have! In some ways! However the implementation details means that some of the possibilities for normalizing arrays which were possible before are impossible to achieve now.

Arrays are forced to normalize per element, even if this is not desired.

What? Why does this matter?

A simple case in this project was forcing uniqueness in an array:

add :set_uniqueness  do |values, options|
  options ||= {}
  stringify = options.fetch(:stringify, false)

  if stringify
    values.flatten.map(&:to_s).uniq
  else
    values.flatten.uniq
  end
end

Asserting that a list of values contains no duplicates is, in my mind, a valid task for "normalization". However, with the update, this is not possible. The process method detects and array and tries to run this on every element. If a model were to use this normalization, it would either (a) not function correctly and raise error such as NoMethodError on flatten, or (b) produce a double-wrapped array (e.g. [["foo"], ["bar"]], depending how input was structured (I have run into both of these outcomes).

There is (as best I can tell) no way to write a normalization that operates in the entire context of the array itself. Each normalization function is constrained to only consider the single element--not the whole array, even if that is the goal.

What can be done?

I realize it has been six years since Normalizr gained this automatic array functionality. It is unreasonable for me to come in and say "Please change this default and break everyone's expectations to meet my use case!" I completely understand that.

However, it feels strange that there is no way to opt-out of the behavior that forces per-element application of normalization functions when an array is passed to an attribute. Furthermore, I see that the current behavior (automatic array detection) is desirable in many cases!

I can imagine a few ways to implement a solution to this that provide (a) a way to operate on array with its full context and (b) is backwards compatible:

  1. Allow passing a per-normalization option such as auto_array: (false|true), or each_element: (false|true), or something similarly named, to signal that the normalizers should be passed the full value even if the value is an array. The default being true would maintain existing behavior.
  2. A library-wide configuration such as automatic_array_normalization: (false|true) that would disable the automatic per-element processing for arrays entirely.
  3. Both! In case of a full library setting that disables array processing like automatically_detect_arrays: false, one could still provide the per-attribute normalize :favorite_colors, auto_array: true and opt-in to automatic array processing in that one case. Or if automatically_detect_arrays: true then individual attributes could be opted out by auto_array: false or similar.

Summary

I feel strongly that operating on an array as a whole is a valid type of normalization. I would be happy to develop a solution that reflects any of these and provide a pull request, as long as any of these proposals match the project vision. This is a tightly-focused library that does what it does, it does so very well, and I appreciate using it for as long as I have. I would welcome contributing back to this project.

Disable on a certain model

Hi probably not an issue, but cant find in docs. Is there any way to disable Normalizr on just one model/class, as have a situation where need to do this? Or can a handler be prepended to the model? Thanks

Model method to normalize input values

I'd like to have a class method such as normalize_values that does something like this:

input = {product_code: "  ABC123  "}
clean_input = Product.normalize_values(input)
#=> {product_code: "ABC123"}
Product.where(clean_input)

This would effectively build a new model instance to push values through the normalizer, and read them back. Is that within scope of your gem for a pull request?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.