Module: FatZebra::ObjectHelper
- Included in:
- FatZebraObject
- Defined in:
- lib/fat_zebra/object_helper.rb
Overview
Object Helper
API objects helper
Instance Attribute Summary collapse
-
#data ⇒ Hash
readonly
JSON parsed response.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#raw ⇒ Hash
readonly
JSON parsed response.
Instance Method Summary collapse
-
#[](key) ⇒ Object
get attribute value.
-
#[]=(key, value) ⇒ Object
set attribute value.
- #add_accessor(name, value) ⇒ Object
- #add_accessors(keys, payload = raw) ⇒ Object
-
#add_data(data) ⇒ Object
Add data for sub-object.
-
#initialize(values = {}) ⇒ Object
Initialize and create accessor for values.
- #inspect ⇒ Object
-
#keys ⇒ Array
All the keys.
- #method_missing(name, *args) ⇒ Object
- #remove_accessor(name) ⇒ Object
-
#to_hash ⇒ Hash
Values to hash.
-
#to_json(_object = nil) ⇒ JSON
Values to JSON.
-
#update_attributes(attributes) ⇒ Object
Update the attribute and add accessor for new attributes.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/fat_zebra/object_helper.rb', line 83 def method_missing(name, *args) super unless name.to_s.end_with?('=') attribute = name.to_s[0...-1].to_sym value = args.first add_accessor(attribute, value) end |
Instance Attribute Details
#data ⇒ Hash (readonly)
Returns JSON parsed response.
14 15 16 |
# File 'lib/fat_zebra/object_helper.rb', line 14 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
16 17 18 |
# File 'lib/fat_zebra/object_helper.rb', line 16 def errors @errors end |
#raw ⇒ Hash (readonly)
Returns JSON parsed response.
10 11 12 |
# File 'lib/fat_zebra/object_helper.rb', line 10 def raw @raw end |
Instance Method Details
#[](key) ⇒ Object
get attribute value
32 33 34 |
# File 'lib/fat_zebra/object_helper.rb', line 32 def [](key) @values[key.to_sym] end |
#[]=(key, value) ⇒ Object
set attribute value
38 39 40 |
# File 'lib/fat_zebra/object_helper.rb', line 38 def []=(key, value) send(:"#{key}=", value) end |
#add_accessor(name, value) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/fat_zebra/object_helper.rb', line 98 def add_accessor(name, value) @values[name] = value define_singleton_method(name) { @values[name] } define_singleton_method(:"#{name}=") do |v| @values[name] = v end define_singleton_method(:"#{name}?") { value } if [FalseClass, TrueClass].include?(value.class) end |
#add_accessors(keys, payload = raw) ⇒ Object
92 93 94 95 96 |
# File 'lib/fat_zebra/object_helper.rb', line 92 def add_accessors(keys, payload = raw) keys.each do |key| add_accessor(key, payload[key]) end end |
#add_data(data) ⇒ Object
Add data for sub-object
74 75 76 |
# File 'lib/fat_zebra/object_helper.rb', line 74 def add_data(data) @data << data end |
#initialize(values = {}) ⇒ Object
Initialize and create accessor for values
22 23 24 25 26 27 28 |
# File 'lib/fat_zebra/object_helper.rb', line 22 def initialize(values = {}) @data = [] @values = {} @errors = [] update_attributes(values) end |
#inspect ⇒ Object
78 79 80 81 |
# File 'lib/fat_zebra/object_helper.rb', line 78 def inspect id_string = respond_to?(:id) && !id.nil? ? " id=#{id}" : '' "#<#{self.class}:0x#{object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values) end |
#keys ⇒ Array
Returns all the keys.
44 45 46 |
# File 'lib/fat_zebra/object_helper.rb', line 44 def keys @values.keys end |
#remove_accessor(name) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/fat_zebra/object_helper.rb', line 109 def remove_accessor(name) @values.delete(name) singleton_class.class_eval { remove_method name.to_sym } if singleton_methods.include?(name.to_sym) singleton_class.class_eval { remove_method "#{name}=".to_sym } if singleton_methods.include?("#{name}=".to_sym) singleton_class.class_eval { remove_method "#{name}?".to_sym } if singleton_methods.include?("#{name}?".to_sym) end |
#to_hash ⇒ Hash
Returns values to hash.
50 51 52 |
# File 'lib/fat_zebra/object_helper.rb', line 50 def to_hash @values end |
#to_json(_object = nil) ⇒ JSON
Returns values to JSON.
56 57 58 |
# File 'lib/fat_zebra/object_helper.rb', line 56 def to_json(_object = nil) JSON.generate(@values) end |
#update_attributes(attributes) ⇒ Object
Update the attribute and add accessor for new attributes
64 65 66 67 68 |
# File 'lib/fat_zebra/object_helper.rb', line 64 def update_attributes(attributes) attributes.each do |(key, value)| add_accessor(key, value) end end |