Class: Resourceful::Model::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/resourceful/model/base.rb

Direct Known Subclasses

Json, Xml

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.



23
24
# File 'lib/resourceful/model/base.rb', line 23

def initialize(data)
end

Class Method Details

.agent(&block) ⇒ Object



7
8
9
# File 'lib/resourceful/model/base.rb', line 7

def self.agent(&block)
  @@agent[self.name.to_s] = block;
end

.find(id, opts, force) ⇒ Object

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/resourceful/model/base.rb', line 19

def self.find(id, opts, force)
  raise NotImplementedError, "no find method has been defined"
end

.get(path, opts = {}) ⇒ Object



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

def self.get(path, opts={})
  block = opts.delete(:on_response)
  set_agent.get(path, opts, &block)
end

.get_collection(path, opts = {}) ⇒ Object



15
16
17
18
# File 'lib/resourceful/model/base.rb', line 15

def self.get_collection(path, opts={})
  block = opts.delete(:on_response)
  (yield set_agent.get(path, opts, &block)).collect{|data| new(data)}
end

Instance Method Details

#attributes(force = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/resourceful/model/base.rb', line 34

def attributes(force=false)
  if @attributes.nil? || force
    @attributes = {}
    self.class.ancestors.each do |anc|
      if @@attributes[anc.to_s]
        @attributes.merge!(@@attributes[anc.to_s].inject({}) { |hsh, key| hsh[key] = self.send("_#{key}"); hsh })
      end
    end
  end
  @attributes
end

#dataObject



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

def data
  @data
end

#destroy {|attributes(true)| ... } ⇒ Object

Yields:



58
59
60
61
# File 'lib/resourceful/model/base.rb', line 58

def destroy
  yield attributes(true)
  @data = nil
end

#new_record?Boolean

Returns:

  • (Boolean)


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

def new_record?
  @data.nil?
end

#saveObject



53
54
55
56
# File 'lib/resourceful/model/base.rb', line 53

def save
  @data = yield attributes(true)
  reset_attributes
end

#update_attributes(attr_hash = {}) ⇒ Object



46
47
48
49
50
51
# File 'lib/resourceful/model/base.rb', line 46

def update_attributes(attr_hash={})
  attr_hash.each do |k,v|
    self.send("#{key}=", v)
  end
  attributes(true)
end