Class: Plucky::Normalizers::OptionsHashValue
- Defined in:
- lib/plucky/normalizers/options_hash_value.rb
Instance Method Summary collapse
-
#call(key, value) ⇒ Object
Public: Returns value normalized for Mongo.
-
#default_fields_value_normalizer ⇒ Object
Private.
-
#default_limit_value_normalizer ⇒ Object
Private.
-
#default_skip_value_normalizer ⇒ Object
Private.
-
#default_sort_value_normalizer ⇒ Object
Private.
-
#initialize(args = {}) ⇒ OptionsHashValue
constructor
Public: Initialize an OptionsHashValue.
Constructor Details
#initialize(args = {}) ⇒ OptionsHashValue
Public: Initialize an OptionsHashValue.
args - The hash of arguments (default: {})
:key_normalizer - The key normalizer to use, must respond to call
:value_normalizers - Hash where key is name of options hash key
to normalize and value is what should be used
to normalize the value accordingly (must respond
to call). Allows adding normalizers for new keys
and overriding existing default normalizers.
Examples
Plucky::Normalizers::OptionsHashValue.new({
:key_normalizer => lambda { |key| key}, # key normalizer must responds to call
:value_normalizers => {
:new_key => lambda { |key| key.to_s.upcase }, # add normalizer for :new_key
:fields => lambda { |key| key }, # override normalizer for fields to one that does nothing
}
})
Returns the duplicated String.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/plucky/normalizers/options_hash_value.rb', line 31 def initialize(args = {}) @key_normalizer = args.fetch(:key_normalizer) { raise ArgumentError, "Missing required key :key_normalizer" } @value_normalizers = { :fields => default_fields_value_normalizer, :sort => default_sort_value_normalizer, :limit => default_limit_value_normalizer, :skip => default_skip_value_normalizer, } if (value_normalizers = args[:value_normalizers]) @value_normalizers.update(value_normalizers) end end |
Instance Method Details
#call(key, value) ⇒ Object
Public: Returns value normalized for Mongo
key - The name of the key whose value is being normalized value - The value to normalize
Returns value normalized for Mongo.
54 55 56 57 58 59 60 |
# File 'lib/plucky/normalizers/options_hash_value.rb', line 54 def call(key, value) if (value_normalizer = @value_normalizers[key]) value_normalizer.call(value) else value end end |
#default_fields_value_normalizer ⇒ Object
Private
63 64 65 |
# File 'lib/plucky/normalizers/options_hash_value.rb', line 63 def default_fields_value_normalizer Normalizers::FieldsValue.new end |
#default_limit_value_normalizer ⇒ Object
Private
75 76 77 |
# File 'lib/plucky/normalizers/options_hash_value.rb', line 75 def default_limit_value_normalizer Normalizers::Integer.new end |
#default_skip_value_normalizer ⇒ Object
Private
80 81 82 |
# File 'lib/plucky/normalizers/options_hash_value.rb', line 80 def default_skip_value_normalizer Normalizers::Integer.new end |
#default_sort_value_normalizer ⇒ Object
Private
68 69 70 71 72 |
# File 'lib/plucky/normalizers/options_hash_value.rb', line 68 def default_sort_value_normalizer Normalizers::SortValue.new({ :key_normalizer => @key_normalizer, }) end |