Class: Tacokit::Resource

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

Constant Summary

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Resource) initialize(data = {})

Returns a new instance of Resource



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

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

- (Object) method_missing(method, *args) (private)



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

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

- (Object) _fields (readonly)

Returns the value of attribute _fields



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

def _fields
  @_fields
end

- (Object) attrs (readonly) Also known as: to_hash, to_h

Returns the value of attribute attrs



13
14
15
# File 'lib/tacokit/resource.rb', line 13

def attrs
  @attrs
end

Instance Method Details

- (Object) [](method)



32
33
34
35
36
# File 'lib/tacokit/resource.rb', line 32

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

- (Object) []=(method, value)



38
39
40
41
42
# File 'lib/tacokit/resource.rb', line 38

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

- (Object) each(&block)



28
29
30
# File 'lib/tacokit/resource.rb', line 28

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

- (Object) inspect Also known as: to_s



50
51
52
# File 'lib/tacokit/resource.rb', line 50

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

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

Returns:

  • (Boolean)


44
45
46
# File 'lib/tacokit/resource.rb', line 44

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

- (Object) to_attrs



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

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

- (Object) update(attributes)



68
69
70
71
72
# File 'lib/tacokit/resource.rb', line 68

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