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



85
86
87
88
89
90
91
92
# File 'lib/fat_zebra/object_helper.rb', line 85

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



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

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



18
19
20
# File 'lib/fat_zebra/object_helper.rb', line 18

def errors
  @errors
end

#rawHash (readonly)

Returns JSON parsed response.

Returns:

  • (Hash)

    JSON parsed response



12
13
14
# File 'lib/fat_zebra/object_helper.rb', line 12

def raw
  @raw
end

Instance Method Details

#[](key) ⇒ Object

get attribute value



34
35
36
# File 'lib/fat_zebra/object_helper.rb', line 34

def [](key)
  @values[key.to_sym]
end

#[]=(key, value) ⇒ Object

set attribute value



40
41
42
# File 'lib/fat_zebra/object_helper.rb', line 40

def []=(key, value)
  send(:"#{key}=", value)
end

#add_accessor(name, value) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/fat_zebra/object_helper.rb', line 100

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



94
95
96
97
98
# File 'lib/fat_zebra/object_helper.rb', line 94

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)


76
77
78
# File 'lib/fat_zebra/object_helper.rb', line 76

def add_data(data)
  @data << data
end

#initialize(values = {}) ⇒ Object

Initialize and create accessor for values



24
25
26
27
28
29
30
# File 'lib/fat_zebra/object_helper.rb', line 24

def initialize(values = {})
  @data   = []
  @values = {}
  @errors = []

  update_attributes(values)
end

#inspectObject



80
81
82
83
# File 'lib/fat_zebra/object_helper.rb', line 80

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



46
47
48
# File 'lib/fat_zebra/object_helper.rb', line 46

def keys
  @values.keys
end

#remove_accessor(name) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/fat_zebra/object_helper.rb', line 111

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



52
53
54
# File 'lib/fat_zebra/object_helper.rb', line 52

def to_hash
  @values
end

#to_json(_object = nil) ⇒ JSON

Returns values to JSON.

Returns:

  • (JSON)

    values to JSON



58
59
60
# File 'lib/fat_zebra/object_helper.rb', line 58

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)


66
67
68
69
70
# File 'lib/fat_zebra/object_helper.rb', line 66

def update_attributes(attributes)
  attributes.each do |(key, value)|
    add_accessor(key, value)
  end
end