Class: Panda::Base

Inherits:
Object
  • Object
show all
Includes:
Router
Defined in:
lib/panda/base.rb

Direct Known Subclasses

Cloud, Resource

Constant Summary

Constants included from Router

Router::DEFAULT_FORMAT, Router::VAR_PATTERN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Router

included, #object_url_map

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
# File 'lib/panda/base.rb', line 6

def initialize(attributes = {})
  init_load
  load(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *arguments) ⇒ Object (private)



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/panda/base.rb', line 85

def method_missing(method_symbol, *arguments)
  method_name = method_symbol.to_s
  if method_name =~ /(=|\?)$/
    case $1
    when '='
      attributes[$`] = arguments.first
      @changed_attributes[$`] = arguments.first
    when '?'
      !! attributes[$`]
    end
  else
    return attributes[method_name] if attributes.include?(method_name)
    super
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

Class Method Details

.end_class_nameObject



16
17
18
# File 'lib/panda/base.rb', line 16

def end_class_name
  "#{name.split('::').last}"
end

.id(this_id) ⇒ Object



12
13
14
# File 'lib/panda/base.rb', line 12

def id(this_id)
  find(this_id)
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/panda/base.rb', line 21

def changed?
  !@changed_attributes.empty?
end

#deleteObject



29
30
31
32
# File 'lib/panda/base.rb', line 29

def delete
  response = connection.delete(object_url_map(self.class.one_path))
  !!response['deleted']
end

#idObject



34
35
36
# File 'lib/panda/base.rb', line 34

def id
  attributes['id']
end

#id=(id) ⇒ Object



38
39
40
# File 'lib/panda/base.rb', line 38

def id=(id)
  attributes['id'] = id
end

#new?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/panda/base.rb', line 25

def new?
  id.nil?
end

#reloadObject



42
43
44
45
# File 'lib/panda/base.rb', line 42

def reload
  perform_reload
  self
end

#to_jsonObject



47
48
49
# File 'lib/panda/base.rb', line 47

def to_json
  attributes.to_json
end