Class: SparkApi::Models::Base
- Inherits:
-
Object
- Object
- SparkApi::Models::Base
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.
Direct Known Subclasses
Account, Account::Address, Account::Email, Account::Image, Account::Phone, Account::Website, Activity, Comment, Connect, Contact, CustomFields, Document, EmailLink, Fields, FloPlan, Idx, IdxLink, IncompleteListing, Listing, ListingCart, ListingMetaTranslations, MarketStatistics, Message, NewsFeedMeta, Newsfeed, Note, Notification, OpenHouse, Photo, Portal, PropertyTypes, QuickSearch, RentalCalendar, Rule, SavedSearch, SharedLink, SharedListing, Sort, StandardFields, SystemInfo, SystemInfoSearch, TourOfHome, Video, VirtualTour, VowAccount
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.
60
61
62
63
64
|
# File 'lib/spark_api/models/base.rb', line 60
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# 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)
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
#attributes ⇒ Object
Returns the value of attribute attributes.
10
11
12
|
# File 'lib/spark_api/models/base.rb', line 10
def attributes
@attributes
end
|
#errors ⇒ Object
Returns the value of attribute errors.
10
11
12
|
# File 'lib/spark_api/models/base.rb', line 10
def errors
@errors
end
|
#parent ⇒ Object
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
.connection ⇒ Object
53
54
55
|
# File 'lib/spark_api/models/base.rb', line 53
def self.connection
SparkApi.client
end
|
.count(options = {}) ⇒ Object
81
82
83
|
# File 'lib/spark_api/models/base.rb', line 81
def self.count(options={})
connection.get(path, options.merge({:_pagination=>"count"}))
end
|
.element_name ⇒ Object
Name of the resource as related to the path name
18
19
20
21
|
# File 'lib/spark_api/models/base.rb', line 18
def self.element_name
@element_name ||= "resource"
end
|
.element_name=(name) ⇒ Object
22
23
24
|
# File 'lib/spark_api/models/base.rb', line 22
def self.element_name=(name)
@element_name = name
end
|
.first(options = {}) ⇒ Object
77
78
79
|
# File 'lib/spark_api/models/base.rb', line 77
def self.first(options={})
get(options).first
end
|
.get(options = {}) ⇒ Object
73
74
75
|
# File 'lib/spark_api/models/base.rb', line 73
def self.get(options={})
collect(connection.get(path, options))
end
|
.path ⇒ Object
38
39
40
|
# File 'lib/spark_api/models/base.rb', line 38
def self.path
"#{prefix}#{element_name}"
end
|
.prefix ⇒ Object
Resource path prefix, prepended to the url
27
28
29
|
# File 'lib/spark_api/models/base.rb', line 27
def self.prefix
@prefix ||= "/"
end
|
.prefix=(prefix) ⇒ Object
30
31
32
|
# File 'lib/spark_api/models/base.rb', line 30
def self.prefix=(prefix)
@prefix = prefix
end
|
Instance Method Details
#connection ⇒ Object
56
57
58
|
# File 'lib/spark_api/models/base.rb', line 56
def connection
self.class.connection
end
|
#id ⇒ Object
More familiar accessor for our Spark API Id method
13
14
15
|
# File 'lib/spark_api/models/base.rb', line 13
def id
self.Id
end
|
#load(attributes, options = {}) ⇒ Object
66
67
68
69
70
71
|
# 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
|
#parse_id(uri) ⇒ Object
125
126
127
|
# File 'lib/spark_api/models/base.rb', line 125
def parse_id(uri)
uri[/\/.*\/(.+)$/, 1]
end
|
#path ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
|
# 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? ⇒ Boolean
129
130
131
|
# File 'lib/spark_api/models/base.rb', line 129
def persisted?;
!@attributes['Id'].nil? && !@attributes['ResourceUri'].nil?
end
|
#resource_pluralized ⇒ Object
142
143
144
145
146
147
148
|
# 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 ⇒ Object
34
35
36
|
# 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) ⇒ Boolean
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# 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 ⇒ Object
133
134
135
|
# File 'lib/spark_api/models/base.rb', line 133
def to_param
attributes['Id']
end
|
#to_partial_path ⇒ Object
137
138
139
|
# File 'lib/spark_api/models/base.rb', line 137
def to_partial_path
"#{underscore(resource_pluralized)}/#{underscore(self.class.name.split('::').last)}"
end
|