Module: FatZebra::ObjectHelper

Included in:
FatZebraObject
Defined in:
lib/fat_zebra/object_helper.rb

Overview

Object Helper

API objects helper

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataHash (readonly)

Returns JSON parsed response.

Returns:

  • (Hash)

    JSON parsed response



14
15
16
# File 'lib/fat_zebra/object_helper.rb', line 14

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



16
17
18
# File 'lib/fat_zebra/object_helper.rb', line 16

def errors
  @errors
end

#rawHash (readonly)

Returns JSON parsed response.

Returns:

  • (Hash)

    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

Parameters:

  • data (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

#inspectObject



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

#keysArray

Returns all the keys.

Returns:

  • (Array)

    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_hashHash

Returns values to hash.

Returns:

  • (Hash)

    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.

Returns:

  • (JSON)

    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

Parameters:

  • values (Hash)


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