Class: Tacokit::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/tacokit/resource.rb

Constant Summary collapse

SPECIAL_METHODS =
Set.new(%w[fields])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Resource

Returns a new instance of Resource.


17
18
19
20
21
22
23
24
# File 'lib/tacokit/resource.rb', line 17

def initialize(data = {})
  @attrs = {}
  @_fields = Set.new
  data.each do |key, value|
    @attrs[key.to_sym] = process_value(value)
  end
  new_attrs(*data.keys)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tacokit/resource.rb', line 85

def method_missing(method, *args)
  attr_name, suffix = method.to_s.scan(/([a-z0-9\_]+)(\?|\=)?$/i).first
  if suffix == ATTR_SETTER
    setter_missing(attr_name, args.first)
  elsif attr_name && @_fields.include?(attr_name.to_sym)
    getter_missing(attr_name, suffix)
  elsif suffix.nil? && SPECIAL_METHODS.include?(attr_name)
    instance_variable_get "@_#{attr_name}"
  elsif attr_name && !@_fields.include?(attr_name.to_sym)
    nil
  else
    super
  end
end

Instance Attribute Details

#_fieldsObject (readonly)

Returns the value of attribute _fields


10
11
12
# File 'lib/tacokit/resource.rb', line 10

def _fields
  @_fields
end

#attrsObject (readonly) Also known as: to_hash, to_h

Returns the value of attribute attrs


11
12
13
# File 'lib/tacokit/resource.rb', line 11

def attrs
  @attrs
end

Instance Method Details

#[](method) ⇒ Object


30
31
32
33
34
# File 'lib/tacokit/resource.rb', line 30

def [](method)
  send(method.to_sym)
rescue NoMethodError
  nil
end

#[]=(method, value) ⇒ Object


36
37
38
39
40
# File 'lib/tacokit/resource.rb', line 36

def []=(method, value)
  send("#{method}=", value)
rescue NoMethodError
  nil
end

#each(&block) ⇒ Object


26
27
28
# File 'lib/tacokit/resource.rb', line 26

def each(&block)
  @attrs.each(&block)
end

#inspectObject Also known as: to_s


48
49
50
# File 'lib/tacokit/resource.rb', line 48

def inspect
  (to_attrs.respond_to?(:pretty_inspect) ? to_attrs.pretty_inspect : to_attrs.inspect)
end

#key?(key) ⇒ Boolean Also known as: has_key?, include?

Returns:

  • (Boolean)

42
43
44
# File 'lib/tacokit/resource.rb', line 42

def key?(key)
  @_fields.include?(key)
end

#to_attrsObject


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tacokit/resource.rb', line 54

def to_attrs
  hash = attrs.clone
  hash.keys.each do |k|
    if hash[k].is_a?(Resource)
      hash[k] = hash[k].to_attrs
    elsif hash[k].is_a?(Array) && hash[k].all? { |el| el.is_a?(Resource) }
      hash[k] = hash[k].collect(&:to_attrs)
    end
  end
  hash
end

#update(attributes) ⇒ Object


66
67
68
69
70
# File 'lib/tacokit/resource.rb', line 66

def update(attributes)
  attributes.each do |key, value|
    send("#{key}=", value)
  end
end