Class: AttrPermit
- Inherits:
-
Object
- Object
- AttrPermit
- Defined in:
- lib/attr_permit.rb,
lib/attr_permit/version.rb
Direct Known Subclasses
Constant Summary collapse
- VERSION =
"0.0.4"
Class Method Summary collapse
- .attr_permit(*permissible_methods) ⇒ Object
- .map_attribute(to, from) ⇒ Object
- .map_attributes(map_hash) ⇒ Object
- .mapped_methods ⇒ Object
- .permissible_methods ⇒ Object
Instance Method Summary collapse
- #==(obj) ⇒ Object
- #hash ⇒ Object
-
#initialize(source = nil) ⇒ AttrPermit
constructor
A new instance of AttrPermit.
- #is_equivalent?(obj) ⇒ Boolean
- #map_hash(big_decimal_as_string: false, all_values_as_string: false) ⇒ Object
- #non_nil_values(hash_type = :to_hash) ⇒ Object
- #permit_hash(big_decimal_as_string: false, all_values_as_string: false) ⇒ Object
- #to_enum ⇒ Object
- #to_hash(big_decimal_as_string: false, all_values_as_string: false) ⇒ Object (also: #to_h)
- #update(source) ⇒ Object
Constructor Details
#initialize(source = nil) ⇒ AttrPermit
Returns a new instance of AttrPermit.
61 62 63 64 |
# File 'lib/attr_permit.rb', line 61 def initialize(source=nil) @source = source update(source) end |
Class Method Details
.attr_permit(*permissible_methods) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/attr_permit.rb', line 15 def attr_permit(*permissible_methods) self.permissible_methods.concat [*permissible_methods, *get_super_permissible_methods] self.permissible_methods.each do |meth| send(:define_method, meth) do call_method(meth) end attr_writer meth unless public_instance_methods.include?("#{meth}=") end end |
.map_attribute(to, from) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/attr_permit.rb', line 33 def map_attribute(to, from) self.mapped_methods.concat [*to, *get_super_mapped_methods] if from.is_a? Proc send(:define_method, to) do source.instance_exec(&from) end else send(:define_method, to) do call_method(from) end end end |
.map_attributes(map_hash) ⇒ Object
27 28 29 30 31 |
# File 'lib/attr_permit.rb', line 27 def map_attributes(map_hash) map_hash.each do |to, from| map_attribute to, from end end |
.mapped_methods ⇒ Object
11 12 13 |
# File 'lib/attr_permit.rb', line 11 def mapped_methods @mapped_methods ||= [] end |
.permissible_methods ⇒ Object
7 8 9 |
# File 'lib/attr_permit.rb', line 7 def permissible_methods @permissible_methods ||= [] end |
Instance Method Details
#==(obj) ⇒ Object
102 103 104 |
# File 'lib/attr_permit.rb', line 102 def ==(obj) self.hash == obj.hash end |
#hash ⇒ Object
114 115 116 |
# File 'lib/attr_permit.rb', line 114 def hash self.to_hash.hash end |
#is_equivalent?(obj) ⇒ Boolean
106 107 108 109 110 111 112 |
# File 'lib/attr_permit.rb', line 106 def is_equivalent?(obj) self_hash = self.to_hash obj_hash = obj.to_hash self_hash.each { |k, v| self_hash[k] = v.to_s } obj_hash.each { |k, v| obj_hash[k] = v.to_s } self_hash == obj_hash end |
#map_hash(big_decimal_as_string: false, all_values_as_string: false) ⇒ Object
82 83 84 |
# File 'lib/attr_permit.rb', line 82 def map_hash(big_decimal_as_string: false, all_values_as_string: false) hasher([*self.class.mapped_methods], big_decimal_as_string, all_values_as_string) end |
#non_nil_values(hash_type = :to_hash) ⇒ Object
96 97 98 99 100 |
# File 'lib/attr_permit.rb', line 96 def non_nil_values(hash_type=:to_hash) hash = {} send(hash_type).each { |k, v| hash[k] = v unless v.nil? } hash end |
#permit_hash(big_decimal_as_string: false, all_values_as_string: false) ⇒ Object
86 87 88 |
# File 'lib/attr_permit.rb', line 86 def permit_hash(big_decimal_as_string: false, all_values_as_string: false) hasher([*self.class.permissible_methods], big_decimal_as_string, all_values_as_string) end |
#to_enum ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/attr_permit.rb', line 118 def to_enum copy = self.dup copy.singleton_class.send(:include, Enumerable) def copy.each(&block) self.class.permissible_methods.each do |item| block.call(public_send(item)) end end copy end |
#to_hash(big_decimal_as_string: false, all_values_as_string: false) ⇒ Object Also known as: to_h
90 91 92 |
# File 'lib/attr_permit.rb', line 90 def to_hash(big_decimal_as_string: false, all_values_as_string: false) hasher([*self.class.permissible_methods, *self.class.mapped_methods], big_decimal_as_string, all_values_as_string) end |
#update(source) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/attr_permit.rb', line 66 def update(source) return if source.nil? source = OpenStruct.new(source) if source.class <= Hash self.class.permissible_methods.each do |meth| send("#{meth}=", source.send(meth)) if source.respond_to? meth end end |