Class: Whoa::WoObject

Inherits:
Object
  • Object
show all
Defined in:
lib/whoa/wo_object.rb

Direct Known Subclasses

Experiment, Page

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_recordObject

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

#attributesObject

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

#destroyObject



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

Returns:

  • (Boolean)


78
79
80
# File 'lib/whoa/wo_object.rb', line 78

def new_record?
  self.new_record && wo_url.nil?
end

#saveObject



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