class Rbvore::Resource
Attributes
links[R]
Public Class Methods
attr_collections(hash)
click to toggle source
attr_collections
sets up an associated list of objects that can be parsed from the json
# File lib/rbvore/resource.rb, line 42 def self.attr_collections(hash) hash.each do |name, klass| define_link_getter(name) define_method("#{name}=") do |ary| instance_variable_set( "@#{name}", parse_collection(ary, klass), ) end end end
attr_objects(hash)
click to toggle source
attr_objects
sets up an associated object that can be parsed from json
# File lib/rbvore/resource.rb, line 28 def self.attr_objects(hash) hash.each do |name, klass| define_link_getter(name) define_method("#{name}=") do |obj| instance_variable_set( "@#{name}", parse_object(obj, klass), ) end end end
attr_timestamp_accessor(*attrs)
click to toggle source
# File lib/rbvore/resource.rb, line 54 def self.attr_timestamp_accessor(*attrs) attrs.each do |name| attr_reader(name) define_method("#{name}=") { |value| instance_variable_set("@#{name}", parse_timestamp(value)) } end end
define_link_getter(name)
click to toggle source
define_link_getter
sets up an attribute getter that fetches from the associated link, if available
# File lib/rbvore/resource.rb, line 18 def self.define_link_getter(name) define_method(name) do |opts = {}| stored_value = instance_variable_get("@#{name}") return stored_value unless stored_value.nil? instance_variable_set("@#{name}", fetch_link(name, api_key: opts[:api_key])) end end
Public Instance Methods
_embedded=(hash)
click to toggle source
# File lib/rbvore/resource.rb, line 87 def _embedded=(hash) set_attributes(hash) end
_links=(hash)
click to toggle source
# File lib/rbvore/resource.rb, line 81 def _links=(hash) @links = hash.each_with_object({}) { |(name, data), memo| memo[name.to_sym] = Link.new(data) } end
fetch_link(name, api_key: nil, params: {})
click to toggle source
# File lib/rbvore/resource.rb, line 74 def fetch_link(name, api_key: nil, params: {}) link = links[name.to_sym] raise UnknownLinkError, "Unknown link `#{name}` for #{self.class.singularize}" if link.nil? link.fetch(api_key: api_key, params: params) end
inspect()
click to toggle source
# File lib/rbvore/resource.rb, line 91 def inspect "#<#{self.class} #{[@id, @name || @display_name].join(" ")}>" end
set_attribute(key, value)
click to toggle source
# File lib/rbvore/resource.rb, line 63 def set_attribute(key, value) setter = "#{self.class.underscore(key)}=" send(setter, value) if respond_to?(setter) end
set_attributes(hash)
click to toggle source
# File lib/rbvore/resource.rb, line 68 def set_attributes(hash) # rubocop:disable Naming/AccessorMethodName hash.each do |key, value| set_attribute(key, value) end end