Class: Resourceful::Model::Base
- Inherits:
-
Object
- Object
- Resourceful::Model::Base
- Includes:
- AttributeTypes
- Defined in:
- lib/resourceful/model/base.rb
Constant Summary collapse
- @@namespaces =
[""]
Class Method Summary collapse
- .agent(&block) ⇒ Object
- .cleanup_name(name) ⇒ Object
- .find(id, opts, force) ⇒ Object
- .get(path, opts = {}) ⇒ Object
- .get_collection(path, opts = {}) ⇒ Object
- .get_namespaced_klass(class_name) ⇒ Object
- .namespaces ⇒ Object
Instance Method Summary collapse
- #attributes(force = false) ⇒ Object
- #data ⇒ Object
- #destroy {|attributes(true)| ... } ⇒ Object
-
#initialize(data) ⇒ Base
constructor
A new instance of Base.
- #new_record? ⇒ Boolean
- #save ⇒ Object
- #update_attributes(attr_hash = {}) ⇒ Object
Methods included from AttributeTypes
Constructor Details
#initialize(data) ⇒ Base
Returns a new instance of Base.
50 51 |
# File 'lib/resourceful/model/base.rb', line 50 def initialize(data) end |
Class Method Details
.agent(&block) ⇒ Object
11 12 13 |
# File 'lib/resourceful/model/base.rb', line 11 def self.agent(&block) @@agent[self.name.to_s] = block; end |
.cleanup_name(name) ⇒ Object
96 97 98 |
# File 'lib/resourceful/model/base.rb', line 96 def self.cleanup_name(name) name.to_s.gsub(/\W/,'') end |
.find(id, opts, force) ⇒ Object
46 47 48 |
# File 'lib/resourceful/model/base.rb', line 46 def self.find(id, opts, force) raise NotImplementedError, "no find method has been defined" end |
.get(path, opts = {}) ⇒ Object
38 39 40 41 |
# File 'lib/resourceful/model/base.rb', line 38 def self.get(path, opts={}) block = opts.delete(:on_response) set_agent.get(path, opts, &block) end |
.get_collection(path, opts = {}) ⇒ Object
42 43 44 45 |
# File 'lib/resourceful/model/base.rb', line 42 def self.get_collection(path, opts={}) block = opts.delete(:on_response) (yield set_agent.get(path, opts, &block)).collect{|data| new(data)} end |
.get_namespaced_klass(class_name) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/resourceful/model/base.rb', line 22 def self.get_namespaced_klass(class_name) klass = nil if class_name.kind_of?(::String) @@namespaces.each do |ns| begin klass = "#{ns}::#{class_name}".resourceful_constantize break; rescue Exception => err end end else klass = class_name end klass end |
.namespaces ⇒ Object
16 17 18 |
# File 'lib/resourceful/model/base.rb', line 16 def self.namespaces @@namespaces.dup end |
Instance Method Details
#attributes(force = false) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/resourceful/model/base.rb', line 61 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 |
#data ⇒ Object
53 54 55 |
# File 'lib/resourceful/model/base.rb', line 53 def data @data end |
#destroy {|attributes(true)| ... } ⇒ Object
91 92 93 94 |
# File 'lib/resourceful/model/base.rb', line 91 def destroy yield attributes(true) @data = nil end |
#new_record? ⇒ Boolean
57 58 59 |
# File 'lib/resourceful/model/base.rb', line 57 def new_record? @data.nil? end |
#save ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/resourceful/model/base.rb', line 80 def save # TODO: I think this is where it is failing if response does not return any data # => test it! if response_data = yield(attributes(true)) @data = response_data reset_attributes else @attributes end end |
#update_attributes(attr_hash = {}) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/resourceful/model/base.rb', line 73 def update_attributes(attr_hash={}) attr_hash.each do |k,v| self.send("#{k}=", v) end attributes(true) end |