lib/siteleaf/entity.rb in siteleaf-2.0.0.pre.beta7 vs lib/siteleaf/entity.rb in siteleaf-2.0.0.pre.beta9

- old
+ new

@@ -6,41 +6,41 @@ def initialize(attributes = {}) self.attributes = attributes end def self.all - result = Client.get "#{self.endpoint}" - result.map { |r| self.new(r) } if result.is_a? Array + result = Client.get endpoint + result.map { |r| new(r) } if result.is_a? Array end - def self.find(id) - result = Client.get "#{self.endpoint}/#{id}" - self.new(result) if result + def self.find(identifier) + result = Client.get "#{endpoint}/#{identifier}" + new(result) if result end def self.create(attributes = {}) - self.new(attributes).save + new(attributes).save end + + def self.delete(identifier) + Client.delete "#{endpoint}/#{identifier}" + end def save - if self.id - result = Client.put "#{self.class.endpoint}/#{self.id}", self.attributes + if identifier + result = Client.put entity_endpoint, attributes else - result = Client.post "#{self.create_endpoint}", self.attributes + result = Client.post create_endpoint, attributes end if result self.attributes = result return self end end - def self.delete(id) - Client.delete "#{self.endpoint}/#{id}" - end - def delete - Client.delete "#{self.class.endpoint}/#{self.id}" + Client.delete entity_endpoint end def attributes Hash[self.instance_variables.map { |name| [name[1..-1], self.instance_variable_get(name)] }] end @@ -57,9 +57,17 @@ "#{self.class_name.downcase}s" end def create_endpoint self.class.endpoint + end + + def entity_endpoint + "#{self.class.endpoint}/#{identifier}" + end + + def identifier + id end end end