Class: Whoa::WoObject
- Inherits:
-
Object
- Object
- Whoa::WoObject
- Defined in:
- lib/whoa/wo_object.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#new_record ⇒ Object
Returns the value of attribute new_record.
Class Method Summary collapse
-
.all(opts = {}) ⇒ Object
e.g.
- .create(opts = {}) ⇒ Object
- .first(opts = {}) ⇒ Object
- .handle_response(response) ⇒ Object
- .inherited(subclass) ⇒ Object
- .last(opts = {}) ⇒ Object
Instance Method Summary collapse
-
#attributes ⇒ Object
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
- #copy! ⇒ Object
- #destroy ⇒ Object
-
#initialize(opts = {}) ⇒ WoObject
constructor
TODO: hack.
- #merge_attributes(opts) ⇒ Object (also: #attributes=)
-
#new_record? ⇒ Boolean
TODO: hacky.
- #save ⇒ Object
- #update_attributes(opts = {}) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ WoObject
TODO: hack. should add some protection so wo_url can’t be set manually Or some other new record magic.
11 12 13 14 |
# File 'lib/whoa/wo_object.rb', line 11 def initialize(opts={}) self.new_record = true merge_attributes(opts) end |
Instance Attribute Details
#new_record ⇒ Object
Returns the value of attribute new_record.
3 4 5 |
# File 'lib/whoa/wo_object.rb', line 3 def new_record @new_record end |
Class Method Details
.all(opts = {}) ⇒ Object
e.g. Whoa::Feed.all_experiments
25 26 27 |
# File 'lib/whoa/wo_object.rb', line 25 def self.all(opts={}) Whoa::Feed.send("all_#{name.split("::").last.downcase.pluralize}", opts) end |
.create(opts = {}) ⇒ Object
37 38 39 40 41 |
# File 'lib/whoa/wo_object.rb', line 37 def self.create(opts={}) obj = new(opts) obj.new_record = true obj.save end |
.first(opts = {}) ⇒ Object
29 30 31 |
# File 'lib/whoa/wo_object.rb', line 29 def self.first(opts={}) all(opts).last end |
.handle_response(response) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/whoa/wo_object.rb', line 82 def self.handle_response(response) if response.code.to_s !~ /^(200|201)/ raise "Not a valid response from WO, was #{CGI.unescapedHTML(response.body)}" else parse(response.body) end end |
.inherited(subclass) ⇒ Object
5 6 7 |
# File 'lib/whoa/wo_object.rb', line 5 def self.inherited(subclass) subclass.send :include, HappyMapper end |
.last(opts = {}) ⇒ Object
33 34 35 |
# File 'lib/whoa/wo_object.rb', line 33 def self.last(opts={}) all(opts).last end |
Instance Method Details
#attributes ⇒ Object
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
70 71 72 73 74 75 |
# File 'lib/whoa/wo_object.rb', line 70 def attributes self.instance_variable_names.inject({}) do |attrs, name| attrs[name.gsub("@","")] = instance_variable_get(name) attrs end end |
#copy! ⇒ Object
48 49 50 51 52 |
# File 'lib/whoa/wo_object.rb', line 48 def copy! self.new_record = true self.wo_url = nil save end |
#destroy ⇒ Object
63 64 65 66 67 |
# File 'lib/whoa/wo_object.rb', line 63 def destroy Whoa::Request.delete(url, to_xml) do |resp| resp.code == 200 ? true : false end end |
#merge_attributes(opts) ⇒ Object Also known as: attributes=
16 17 18 19 20 21 |
# File 'lib/whoa/wo_object.rb', line 16 def merge_attributes(opts) opts.each_pair do |key, value| self.send("#{key}=", value) end self end |
#new_record? ⇒ Boolean
TODO: hacky
78 79 80 |
# File 'lib/whoa/wo_object.rb', line 78 def new_record? self.new_record && wo_url.nil? end |
#save ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/whoa/wo_object.rb', line 54 def save verb = new_record? ? :post : :put Whoa::Request.send(verb, url, to_xml) do |response| self.new_record = false obj = self.class.handle_response(response) merge_attributes(obj.attributes) end end |
#update_attributes(opts = {}) ⇒ Object
43 44 45 46 |
# File 'lib/whoa/wo_object.rb', line 43 def update_attributes(opts={}) merge_attributes(opts) save end |