Ordinary
Normalizer for any model
Installation
Add this line to your application's Gemfile:
gem 'ordinary'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ordinary
Usage
First, you will include Ordinary
to the model of the object. Following is an example of using ActiveModel.
require 'ordinary'
class Person
include ActiveModel::Model
include Ordinary
attr_accessor :name
normalizes :name do |value|
value.strip.squeeze(' ')
end
end
You can get the normalized value with #normalize_attribute
or #normalized_ATTR_NAME
.
person = Person.new(:name => ' Koyomi Araragi ')
puts person.name
puts person.normalize_attribute(:name)
puts person.normalized_name
And you can get the normalized model with #normalize
.
normalized_person = person.normalize
puts normalized_person.normalized?
puts normalized_person.name
Off course, it doesn't affect the original model.
puts person.normalized?
puts person.name
However, if you use #normalize!
, the original model will also be normalized.
How to define normalization
How to define normalization is from where you include Ordinary
.
require 'ordinary'
class AnyModel
include Ordinary
end
Incidentally, in order to enable to read and write to a target attirbute, you must define #ATTR_NAME
and #ATTR_NAME=
.
Normalization defines with .normalizes
.
class AnyModel
attr_accessor :attr1, :attr2
normalizes :attr1, :attr2 do |value|
end
end
You can define in a variety of ways.
class AnyModel
attr_accessor :attr1, :attr2
normalizes :attr1 do |value|
"#{value}_1"
end
normalizes :attr1 do |value|
"#{value}_2"
end
normalizes :attr2, lambda { lstrip | rstrip }
# also specify both block and Proc (position of process of block decides by
# block unit)
normalizes :attr2, lambda { block | squeeze(' ') } do |value|
"#{value}_3"
end
normalizes :attr2, if: lambda { !attr2.nil? }, with: lambda { block | at(0) } do |value|
(value.empty? or %w(0 false f).include?(value)) ? 'false' : 'true'
end
# ...
end
How to create an units module
You can create a module bundled some units. You'll use Ordinary::Module
to do so.
require 'ordinary/module'
module AnyModule
extend Ordinary::Module
end
And you can register to use the module with Ordinary.register
.
require 'ordinary'
Ordinary.register(AnyModule)
Define an unit
An unit can define with .unit
in the module.
module AnyModule
unit :some_unit do |value|
end
end
You can define in a variety of ways.
module AnyModule
unit :lstrip do |value|
value.lstrip
end
unit :rstrip, lambda { |value| value.rstrip }
unit :lstrip
unit :ltrim, :lstrip
p lstrip
unit :strip, lstrip | rstrip
# ...
end
Define a dependency
If exist dependencies to some libraries to units in the module, will resolve with .requires
.
module AnyModule
requires 'nkf'
unit :to_half do |value|
NKF.nkf('-wWZ1', value)
end
end
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request