class SparkApi::Models::Base
API Model Base
class¶ ↑
Intended to be a lot like working with ActiveResource, this class adds most of the basic active model type niceties.
Attributes
attributes[RW]
errors[RW]
parent[RW]
Public Class Methods
connection()
click to toggle source
# File lib/spark_api/models/base.rb, line 53 def self.connection SparkApi.client end
count(options={})
click to toggle source
# File lib/spark_api/models/base.rb, line 81 def self.count(options={}) connection.get(path, options.merge({:_pagination=>"count"})) end
element_name()
click to toggle source
Name of the resource as related to the path name
# File lib/spark_api/models/base.rb, line 18 def self.element_name # TODO I'd love to pull in active model at this point to provide default naming @element_name ||= "resource" end
element_name=(name)
click to toggle source
# File lib/spark_api/models/base.rb, line 22 def self.element_name=(name) @element_name = name end
first(options={})
click to toggle source
# File lib/spark_api/models/base.rb, line 77 def self.first(options={}) get(options).first end
get(options={})
click to toggle source
# File lib/spark_api/models/base.rb, line 73 def self.get(options={}) collect(connection.get(path, options)) end
new(attributes={})
click to toggle source
# File lib/spark_api/models/base.rb, line 60 def initialize(attributes={}) @attributes = {} @errors = [] load(attributes, { :clean => true }) end
path()
click to toggle source
# File lib/spark_api/models/base.rb, line 38 def self.path "#{prefix}#{element_name}" end
prefix()
click to toggle source
Resource path prefix, prepended to the url
# File lib/spark_api/models/base.rb, line 27 def self.prefix @prefix ||= "/" end
prefix=(prefix)
click to toggle source
# File lib/spark_api/models/base.rb, line 30 def self.prefix=(prefix) @prefix = prefix end
Public Instance Methods
connection()
click to toggle source
# File lib/spark_api/models/base.rb, line 56 def connection self.class.connection end
id()
click to toggle source
More familiar accessor for our Spark API Id method
# File lib/spark_api/models/base.rb, line 13 def id self.Id end
load(attributes, options = {})
click to toggle source
# File lib/spark_api/models/base.rb, line 66 def load(attributes, options = {}) attributes.each do |key,val| attribute_will_change!(key) unless options[:clean] @attributes[key.to_s] = val end end
method_missing(method_symbol, *arguments)
click to toggle source
Calls superclass method
# File lib/spark_api/models/base.rb, line 85 def method_missing(method_symbol, *arguments) method_name = method_symbol.to_s if method_name =~ /(=|\?|_will_change!)$/ case $1 when "=" write_attribute($`, arguments.first) # TODO figure out a nice way to present setters for the standard fields when "?" raise NoMethodError unless attributes.include?($`) attributes[$`] ? true : false when "_will_change!" raise NoMethodError unless attributes.include?($`) attribute_will_change!($`) end else return attributes[method_name] if attributes.include?(method_name) super end end
parse_id(uri)
click to toggle source
# File lib/spark_api/models/base.rb, line 125 def parse_id(uri) uri[/\/.*\/(.+)$/, 1] end
path()
click to toggle source
# File lib/spark_api/models/base.rb, line 41 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
persisted?()
click to toggle source
# File lib/spark_api/models/base.rb, line 129 def persisted?; !@attributes['Id'].nil? && !@attributes['ResourceUri'].nil? end
resource_pluralized()
click to toggle source
can be overridden
# File lib/spark_api/models/base.rb, line 142 def resource_pluralized resource = self.class.name.split('::').last unless resource.split('').last == "s" resource = resource + "s" end resource end
resource_uri()
click to toggle source
# File lib/spark_api/models/base.rb, line 34 def resource_uri self.ResourceUri.sub(/^\/#{SparkApi.client.version}/, "") if persisted? end
respond_to?(method_symbol, include_all=false)
click to toggle source
Calls superclass method
# File lib/spark_api/models/base.rb, line 106 def respond_to?(method_symbol, include_all=false) if super return true else method_name = method_symbol.to_s if method_name =~ /=$/ true elsif method_name =~ /(\?)$/ attributes.include?($`) elsif method_name =~ /(\w*)_will_change!$/ attributes.include?($1) else attributes.include?(method_name) end end end
to_param()
click to toggle source
# File lib/spark_api/models/base.rb, line 133 def to_param attributes['Id'] end
to_partial_path()
click to toggle source
# File lib/spark_api/models/base.rb, line 137 def to_partial_path "#{underscore(resource_pluralized)}/#{underscore(self.class.name.split('::').last)}" end
Protected Instance Methods
write_attribute(attribute, value)
click to toggle source
# File lib/spark_api/models/base.rb, line 152 def write_attribute(attribute, value) attribute = attribute.to_s unless attributes[attribute] == value attribute_will_change!(attribute) attributes[attribute] = value end end
Private Instance Methods
underscore(string)
click to toggle source
# File lib/spark_api/models/base.rb, line 162 def underscore(string) string.to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end