Module: HashMapAttributes::ClassMethods
- Defined in:
- lib/hash_map_attributes.rb
Instance Method Summary collapse
- #_hash_map_attributes ⇒ Object
- #hash_map_attributes(*attributes, to:, prefix: nil, allow_nil: nil) ⇒ Object
- #where_hash(**options) ⇒ Object
Instance Method Details
#_hash_map_attributes ⇒ Object
61 62 63 |
# File 'lib/hash_map_attributes.rb', line 61 def _hash_map_attributes @_hash_map_attributes ||= [] end |
#hash_map_attributes(*attributes, to:, prefix: nil, allow_nil: nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/hash_map_attributes.rb', line 25 def hash_map_attributes(*attributes, to:, prefix: nil, allow_nil: nil) _hash_map_attributes attributes.each do |attribute| @_hash_map_attributes << AttributeContainer.new(attribute, to, prefix, allow_nil) end class_eval do _hash_map_attributes.each do |container| _attribute = container.attribute _to = container.to method_name = container.method_name define_method method_name do send(_to).to_hash.stringify_keys![_attribute] end define_method "#{method_name}=" do |v| send(_to).to_hash.stringify_keys![_attribute] = v end end end end |
#where_hash(**options) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/hash_map_attributes.rb', line 46 def where_hash(**) arr = [] class_name = model_name.plural .each_pair do |k, v| container = _hash_map_attributes.find { |a| a.method_name == k.to_s } to = container.to.to_s _attribute = container.attribute.to_s next if to.nil? arr << "#{class_name}.#{to}->>'#{_attribute}' = '#{v}'" end sql = arr.join(' and ') where(sql) end |