Class: PlateApi::PlateObject::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/plate_api/plate_object/base.rb

Constant Summary collapse

HasOneRelations =
{}
HasManyRelations =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, attributes, relations, object_handler = nil) ⇒ Base



9
10
11
12
13
# File 'lib/plate_api/plate_object/base.rb', line 9

def initialize(id, attributes, relations, object_handler=nil)
  @id = id
  @object_handler = object_handler
  initialize_state(attributes, relations)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/plate_api/plate_object/base.rb', line 49

def method_missing(m, *args, &block)
  if attributes[m.to_s]
    return attributes[m.to_s]
  elsif attributes["content"] && attributes["content"][m.to_s]
    return attributes["content"][m.to_s]["value"]
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/plate_api/plate_object/base.rb', line 3

def attributes
  @attributes
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/plate_api/plate_object/base.rb', line 3

def id
  @id
end

#object_handlerObject

Returns the value of attribute object_handler.



4
5
6
# File 'lib/plate_api/plate_object/base.rb', line 4

def object_handler
  @object_handler
end

#relationsObject (readonly)

Returns the value of attribute relations.



3
4
5
# File 'lib/plate_api/plate_object/base.rb', line 3

def relations
  @relations
end

Instance Method Details

#==(other) ⇒ Object



59
60
61
# File 'lib/plate_api/plate_object/base.rb', line 59

def ==(other)
  return other.id == @id && other.class == self.class
end

#api_nameObject



15
16
17
# File 'lib/plate_api/plate_object/base.rb', line 15

def api_name
  self.class.api_name
end

#deleteObject

Raises:

  • (ArgumentError)


36
37
38
39
# File 'lib/plate_api/plate_object/base.rb', line 36

def delete
  raise ArgumentError.new("No object_handler is attached to this object") unless @object_handler
  @object_handler.delete(@id)
end

#inspectObject



45
46
47
# File 'lib/plate_api/plate_object/base.rb', line 45

def inspect
  to_s
end

#reloadObject

Raises:

  • (ArgumentError)


19
20
21
22
23
# File 'lib/plate_api/plate_object/base.rb', line 19

def reload
  raise ArgumentError.new("No object_handler is set.") unless @object_handler
  reinitialize(@object_handler.find(@id))
  return self
end

#to_sObject



41
42
43
# File 'lib/plate_api/plate_object/base.rb', line 41

def to_s
  "<Plate #{self.class.name.split('::').last}, @id=#{@id}, @attributes=#{@attributes}, @object_handler=#{@object_handler}>"
end

#update(attributes) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
# File 'lib/plate_api/plate_object/base.rb', line 25

def update(attributes)
  raise ArgumentError.new("Input `attributes` is not a Hash") unless attributes.is_a? Hash
  raise ArgumentError.new("No object_handler is attached to this object") unless @object_handler
  if new_object = @object_handler.update(@id, attributes)
    reinitialize(new_object)
  else
    raise ArgumentError.new("The update was unsuccesful.")
  end
  return self
end