class Delighted::Resource

Attributes

expandable_attributes[W]
path[RW]
singleton_resource[W]
attributes[R]

Public Class Methods

expandable_attributes() click to toggle source
# File lib/delighted/resource.rb, line 7
def expandable_attributes
  @expandable_attributes ||= {}
end
identifier_string(identifier_hash) click to toggle source
# File lib/delighted/resource.rb, line 15
def identifier_string(identifier_hash)
  raise ArgumentError, "must pass Hash" unless Hash === identifier_hash
  raise ArgumentError, "must pass exactly one identifier name and value" unless identifier_hash.size == 1

  identifier_key = identifier_hash.keys.first
  identifier_value = identifier_hash.values.first

  if identifier_key.to_s == "id"
    identifier_value.to_s
  else
    "#{identifier_key}:#{identifier_value}"
  end
end
new(attributes = {}) click to toggle source
# File lib/delighted/resource.rb, line 34
def initialize(attributes = {})
  @id = attributes[:id]
  define_id_reader if @id
  build_from_attributes(attributes)
end
singleton_resource?() click to toggle source
# File lib/delighted/resource.rb, line 11
def singleton_resource?
  !!@singleton_resource
end

Public Instance Methods

to_h()
Alias for: to_hash
to_hash() click to toggle source

Attributes used for serialization

# File lib/delighted/resource.rb, line 41
def to_hash
  serialized_attributes = attributes.dup

  self.class.expandable_attributes.each_pair.select do |attribute_name, expanded_class|
    if expanded_class === attributes[attribute_name]
      serialized_attributes[attribute_name] = serialized_attributes[attribute_name].id
    end
  end

  serialized_attributes
end
Also aliased as: to_h

Private Instance Methods

build_from_attributes(attributes) click to toggle source
# File lib/delighted/resource.rb, line 68
def build_from_attributes(attributes)
  @attributes = Utils.hash_without_key(attributes, :id)

  self.class.expandable_attributes.each_pair do |attribute_name, expanded_class|
    if Hash === @attributes[attribute_name]
      @attributes[attribute_name] = expanded_class.new(@attributes.delete(attribute_name))
    end
  end

  define_attribute_accessors(@attributes.keys)
end
define_attribute_accessors(keys) click to toggle source
# File lib/delighted/resource.rb, line 86
def define_attribute_accessors(keys)
  Utils.eigenclass(self).instance_eval do
    keys.each do |key|
      define_method(key) do
        attributes[key]
      end

      define_method("#{key}=") do |value|
        attributes[key] = value
      end
    end
  end
end
define_id_reader() click to toggle source
# File lib/delighted/resource.rb, line 80
def define_id_reader
  Utils.eigenclass(self).instance_eval do
    attr_reader :id
  end
end
expanded_attribute_names() click to toggle source
# File lib/delighted/resource.rb, line 56
def expanded_attribute_names
  names = Set.new

  self.class.expandable_attributes.each_pair.select do |attribute_name, expanded_class|
    if expanded_class === attributes[attribute_name]
      names << attribute_name
    end
  end

  names
end