lib/spark_api/models/base.rb in spark_api-1.2.1 vs lib/spark_api/models/base.rb in spark_api-1.3.0
- old
+ new
@@ -5,18 +5,17 @@
# active model type niceties.
class Base
extend Paginate
include Dirty
- attr_accessor :attributes, :errors
+ attr_accessor :attributes, :errors, :parent
# Name of the resource as related to the path name
def self.element_name
# TODO I'd love to pull in active model at this point to provide default naming
@element_name ||= "resource"
end
-
def self.element_name=(name)
@element_name = name
end
# Resource path prefix, prepended to the url
@@ -24,13 +23,29 @@
@prefix ||= "/"
end
def self.prefix=(prefix)
@prefix = prefix
end
+
+ def resource_uri
+ self.ResourceUri.sub(/^\/#{SparkApi.client.version}/, "") if persisted?
+ end
+
def self.path
"#{prefix}#{element_name}"
end
+ def path
+ if self.persisted?
+ resource_uri.sub(/\/[0-9]{26}$/, "")
+ else
+ if @parent
+ "#{@parent.class.path}/#{@parent.Id}#{self.class.path}"
+ else
+ self.class.path
+ end
+ end
+ end
def self.connection
SparkApi.client
end
def connection
@@ -59,10 +74,15 @@
def self.count(options={})
connection.get(path, options.merge({:_pagination=>"count"}))
end
+ # update/create hash (can be overridden)
+ def post_data
+ { resource_pluralized => [ attributes ] }
+ end
+
def method_missing(method_symbol, *arguments)
method_name = method_symbol.to_s
if method_name =~ /(=|\?|_will_change!)$/
case $1
@@ -80,11 +100,11 @@
return attributes[method_name] if attributes.include?(method_name)
super # GTFO
end
end
- def respond_to?(method_symbol, include_private=false)
+ def respond_to?(method_symbol)
if super
return true
else
method_name = method_symbol.to_s
@@ -103,11 +123,11 @@
def parse_id(uri)
uri[/\/.*\/(.+)$/, 1]
end
- def persisted?
- !(@attributes['Id'].nil? && @attributes['ResourceUri'].nil?)
+ def persisted?;
+ !@attributes['Id'].nil? && !@attributes['ResourceUri'].nil?
end
protected
def write_attribute(attribute, value)