Class: Panda::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Builders, Finders, Router
Defined in:
lib/panda/base.rb

Direct Known Subclasses

Cloud, Resource

Constant Summary

Constants included from Router

Router::VAR_PATTERN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Finders

included

Methods included from Builders

#create, #create!, included

Methods included from Router

included, #replace_pattern_with_self_variables

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/panda/base.rb', line 93

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.



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

def attributes
  @attributes
end

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

Class Method Details

.sti_nameObject



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

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

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  !@changed_attributes.empty?
end

#idObject



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

def id
  attributes['id']
end

#id=(id) ⇒ Object



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

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

#inspectObject



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

def inspect
  attributes_as_nice_string = self.attributes.map {|k,v| "#{k}: #{v.inspect}"}.compact.join(", ")
  "#<#{self.class} #{attributes_as_nice_string}>"
end

#new?Boolean

Returns:

  • (Boolean)


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

def new?
  id.nil?
end

#reloadObject



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

def reload
  perform_reload
  self
end

#to_json(*args) ⇒ Object



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

def to_json(*args)
  MultiJson.dump(self.attributes)
end