Class: SparkApi::Models::Base

Inherits:
Object
  • Object
show all
Extended by:
Paginate
Includes:
Dirty
Defined in:
lib/spark_api/models/base.rb

Overview

API Model Base class

Intended to be a lot like working with ActiveResource, this class adds most of the basic active model type niceties.

Constant Summary

Constants included from Paginate

Paginate::DEFAULT_PAGE_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Paginate

collect, paginate, per_page

Methods included from Dirty

#changed, #changed?, #changed_attributes, #changes, #dirty_attributes, #previous_changes

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



55
56
57
58
59
# File 'lib/spark_api/models/base.rb', line 55

def initialize(attributes={})
  @attributes = {}
  @errors = []
  load(attributes, { :clean => true })
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *arguments) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/spark_api/models/base.rb', line 80

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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



10
11
12
# File 'lib/spark_api/models/base.rb', line 10

def attributes
  @attributes
end

#errorsObject

Returns the value of attribute errors.



10
11
12
# File 'lib/spark_api/models/base.rb', line 10

def errors
  @errors
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/spark_api/models/base.rb', line 10

def parent
  @parent
end

Class Method Details

.connectionObject



48
49
50
# File 'lib/spark_api/models/base.rb', line 48

def self.connection
  SparkApi.client
end

.count(options = {}) ⇒ Object



76
77
78
# File 'lib/spark_api/models/base.rb', line 76

def self.count(options={})
  connection.get(path, options.merge({:_pagination=>"count"}))
end

.element_nameObject

Name of the resource as related to the path name



13
14
15
16
# File 'lib/spark_api/models/base.rb', line 13

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) ⇒ Object



17
18
19
# File 'lib/spark_api/models/base.rb', line 17

def self.element_name=(name)
  @element_name = name
end

.first(options = {}) ⇒ Object



72
73
74
# File 'lib/spark_api/models/base.rb', line 72

def self.first(options={})
  get(options).first
end

.get(options = {}) ⇒ Object



68
69
70
# File 'lib/spark_api/models/base.rb', line 68

def self.get(options={})
  collect(connection.get(path, options))
end

.pathObject



33
34
35
# File 'lib/spark_api/models/base.rb', line 33

def self.path
  "#{prefix}#{element_name}"
end

.prefixObject

Resource path prefix, prepended to the url



22
23
24
# File 'lib/spark_api/models/base.rb', line 22

def self.prefix
  @prefix ||= "/"
end

.prefix=(prefix) ⇒ Object



25
26
27
# File 'lib/spark_api/models/base.rb', line 25

def self.prefix=(prefix)
  @prefix = prefix
end

Instance Method Details

#connectionObject



51
52
53
# File 'lib/spark_api/models/base.rb', line 51

def connection
  self.class.connection
end

#load(attributes, options = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/spark_api/models/base.rb', line 61

def load(attributes, options = {})
  attributes.each do |key,val|
    attribute_will_change!(key) unless options[:clean]
    @attributes[key.to_s] = val
  end
end

#parse_id(uri) ⇒ Object



120
121
122
# File 'lib/spark_api/models/base.rb', line 120

def parse_id(uri)
  uri[/\/.*\/(.+)$/, 1]
end

#pathObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spark_api/models/base.rb', line 36

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?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/spark_api/models/base.rb', line 124

def persisted?;
  !@attributes['Id'].nil? && !@attributes['ResourceUri'].nil?
end

#resource_pluralizedObject

can be overridden



137
138
139
140
141
142
143
# File 'lib/spark_api/models/base.rb', line 137

def resource_pluralized
  resource = self.class.name.split('::').last
  unless resource.split('').last == "s"
    resource = resource + "s"
  end
  resource
end

#resource_uriObject



29
30
31
# File 'lib/spark_api/models/base.rb', line 29

def resource_uri
  self.ResourceUri.sub(/^\/#{SparkApi.client.version}/, "") if persisted?
end

#respond_to?(method_symbol, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/spark_api/models/base.rb', line 101

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_paramObject



128
129
130
# File 'lib/spark_api/models/base.rb', line 128

def to_param
  attributes['Id']
end

#to_partial_pathObject



132
133
134
# File 'lib/spark_api/models/base.rb', line 132

def to_partial_path
  "#{underscore(resource_pluralized)}/#{underscore(self.class.name.split('::').last)}"
end