class Tacokit::Resource
Constants
- ATTR_PREDICATE
- ATTR_SETTER
- ISO8601
rubocop:disable Metrics/LineLength
- SPECIAL_METHODS
Attributes
_fields[R]
attrs[R]
to_h[R]
to_hash[R]
Public Class Methods
new(data = {})
click to toggle source
# 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
Public Instance Methods
[](method)
click to toggle source
# File lib/tacokit/resource.rb, line 32 def [](method) send(method.to_sym) rescue NoMethodError nil end
[]=(method, value)
click to toggle source
# File lib/tacokit/resource.rb, line 38 def []=(method, value) send("#{method}=", value) rescue NoMethodError nil end
each(&block)
click to toggle source
# File lib/tacokit/resource.rb, line 28 def each(&block) @attrs.each(&block) end
inspect()
click to toggle source
# File lib/tacokit/resource.rb, line 50 def inspect (to_attrs.respond_to?(:pretty_inspect) ? to_attrs.pretty_inspect : to_attrs.inspect) end
Also aliased as: to_s
key?(key)
click to toggle source
# File lib/tacokit/resource.rb, line 44 def key?(key) @_fields.include?(key) end
to_attrs()
click to toggle source
# 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
update(attributes)
click to toggle source
# File lib/tacokit/resource.rb, line 68 def update(attributes) attributes.each do |key, value| send("#{key}=", value) end end
Private Instance Methods
cast_value_type(value)
click to toggle source
rubocop:enable Metrics/LineLength
# File lib/tacokit/resource.rb, line 135 def cast_value_type(value) case value when ISO8601 then Time.parse(value) else value end rescue value end
getter_missing(attr_name, suffix)
click to toggle source
# File lib/tacokit/resource.rb, line 122 def getter_missing(attr_name, suffix) value = @attrs[attr_name.to_sym] case suffix when nil new_attr(attr_name) value when ATTR_PREDICATE then !!value end end
method_missing(method, *args)
click to toggle source
Calls superclass method
# 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
new_attr(name)
click to toggle source
# File lib/tacokit/resource.rb, line 106 def new_attr(name) name = name.to_sym @_fields << name unless respond_to?(name) define_singleton_method(name) { @attrs[name] } define_singleton_method("#{name}=") { |v| @attrs[name] = v } define_singleton_method("#{name}?") { !!@attrs[name] } end name end
new_attrs(*names)
click to toggle source
# File lib/tacokit/resource.rb, line 102 def new_attrs(*names) names.map { |n| new_attr(n) } end
process_value(value)
click to toggle source
# File lib/tacokit/resource.rb, line 76 def process_value(value) case value when Hash then self.class.new(value) when Array then value.map { |v| process_value(v) } else cast_value_type(value) end end
setter_missing(attr_name, value)
click to toggle source
# File lib/tacokit/resource.rb, line 117 def setter_missing(attr_name, value) new_attr(attr_name) send("#{attr_name}=", value) end