Class: Delighted::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/delighted/resource.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



34
35
36
37
38
# File 'lib/delighted/resource.rb', line 34

def initialize(attributes = {})
  @id = attributes[:id]
  define_id_reader if @id
  build_from_attributes(attributes)
end

Class Attribute Details

.expandable_attributesObject



7
8
9
# File 'lib/delighted/resource.rb', line 7

def expandable_attributes
  @expandable_attributes ||= {}
end

.pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/delighted/resource.rb', line 4

def path
  @path
end

.singleton_resource=(value) ⇒ Object (writeonly)

Sets the attribute singleton_resource

Parameters:

  • value

    the value to set the attribute singleton_resource to.



5
6
7
# File 'lib/delighted/resource.rb', line 5

def singleton_resource=(value)
  @singleton_resource = value
end

Class Method Details

.identifier_string(identifier_hash) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

.singleton_resource?Boolean

Returns:

  • (Boolean)


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

def singleton_resource?
  !!@singleton_resource
end

Instance Method Details

#to_hashObject Also known as: to_h

Attributes used for serialization



41
42
43
44
45
46
47
48
49
50
51
# 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