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

Returns a new instance of 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(method_name, *args, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/plate_api/plate_object/base.rb', line 52

def method_missing(method_name, *args, &block)
  if attributes[method_name.to_s]
    attributes[method_name.to_s]
  elsif attributes["content"] && attributes["content"][method_name.to_s]
    attributes["content"][method_name.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



69
70
71
# File 'lib/plate_api/plate_object/base.rb', line 69

def ==(other)
  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)


38
39
40
41
42
# File 'lib/plate_api/plate_object/base.rb', line 38

def delete
  raise ArgumentError, "No object_handler is attached to this object" unless @object_handler

  @object_handler.delete(@id)
end

#inspectObject



48
49
50
# File 'lib/plate_api/plate_object/base.rb', line 48

def inspect
  to_s
end

#reloadObject

Raises:

  • (ArgumentError)


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

def reload
  raise ArgumentError, "No object_handler is set." unless @object_handler

  reinitialize(@object_handler.find(@id))
  self
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/plate_api/plate_object/base.rb', line 62

def respond_to_missing?(method_name, include_private = false)
  return true if attributes[method_name.to_s]
  return true if attributes["content"] && attributes["content"][method_name.to_s]

  super
end

#to_sObject



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

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

#update(attributes) ⇒ Object

Raises:

  • (ArgumentError)


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

def update(attributes)
  raise ArgumentError, "Input `attributes` is not a Hash" unless attributes.is_a?(Hash)
  raise ArgumentError, "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, "The update was unsuccesful."
  end
  self
end