Class: Nestful::Resource
- Inherits:
-
Object
show all
- Defined in:
- lib/nestful/resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.all(*args) ⇒ Object
-
.defaults(value = nil) ⇒ Object
(also: defaults=, options, options=)
-
.delete(action = '', params = {}, options = {}) ⇒ Object
-
.endpoint(value = nil) ⇒ Object
(also: endpoint=)
-
.find(id) ⇒ Object
-
.get(action = '', params = {}, options = {}) ⇒ Object
-
.new(attributes = {}) ⇒ Object
-
.path(value = nil) ⇒ Object
-
.path= ⇒ Object
-
.post(action = '', params = {}, options = {}) ⇒ Object
-
.put(action = '', params = {}, options = {}) ⇒ Object
-
.request(url, options = {}) ⇒ Object
-
.uri(*parts) ⇒ Object
-
.url ⇒ Object
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Resource
Returns a new instance of Resource.
77
78
79
80
|
# File 'lib/nestful/resource.rb', line 77
def initialize(attributes = {})
@attributes = {}
load(attributes)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *arguments) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/nestful/resource.rb', line 149
def method_missing(method_symbol, *arguments) method_name = method_symbol.to_s
if method_name =~ /(=|\?)$/
case $1
when "="
attributes[$`] = arguments.first
when "?"
attributes[$`]
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.
75
76
77
|
# File 'lib/nestful/resource.rb', line 75
def attributes
@attributes
end
|
Class Method Details
.all(*args) ⇒ Object
59
60
61
|
# File 'lib/nestful/resource.rb', line 59
def self.all(*args)
self.new(get('', *args))
end
|
.defaults(value = nil) ⇒ Object
Also known as:
defaults=, options, options=
17
18
19
20
21
|
# File 'lib/nestful/resource.rb', line 17
def self.defaults(value = nil)
@defaults = value if value
return @defaults if @defaults
superclass.respond_to?(:defaults) ? superclass.defaults : {}
end
|
.delete(action = '', params = {}, options = {}) ⇒ Object
51
52
53
|
# File 'lib/nestful/resource.rb', line 51
def self.delete(action = '', params = {}, options = {})
request(uri(action), options.merge(:method => :delete, :params => params))
end
|
.endpoint(value = nil) ⇒ Object
Also known as:
endpoint=
5
6
7
8
9
|
# File 'lib/nestful/resource.rb', line 5
def self.endpoint(value = nil)
@endpoint = value if value
return @endpoint if @endpoint
superclass.respond_to?(:endpoint) ? superclass.endpoint : nil
end
|
.find(id) ⇒ Object
63
64
65
|
# File 'lib/nestful/resource.rb', line 63
def self.find(id)
self.new(get(id))
end
|
.get(action = '', params = {}, options = {}) ⇒ Object
39
40
41
|
# File 'lib/nestful/resource.rb', line 39
def self.get(action = '', params = {}, options = {})
request(uri(action), options.merge(:method => :get, :params => params))
end
|
.new(attributes = {}) ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/nestful/resource.rb', line 67
def self.new(attributes = {})
if attributes.is_a?(Array)
attributes.map {|set| super(set) }
else
super
end
end
|
.path(value = nil) ⇒ Object
11
12
13
14
15
|
# File 'lib/nestful/resource.rb', line 11
def self.path(value = nil)
@path = value if value
return @path if @path
superclass.respond_to?(:path) ? superclass.path : nil
end
|
.path= ⇒ Object
25
26
27
28
29
|
# File 'lib/nestful/resource.rb', line 25
def self.path(value = nil)
@path = value if value
return @path if @path
superclass.respond_to?(:path) ? superclass.path : nil
end
|
.post(action = '', params = {}, options = {}) ⇒ Object
47
48
49
|
# File 'lib/nestful/resource.rb', line 47
def self.post(action = '', params = {}, options = {})
request(uri(action), options.merge(:method => :post, :params => params))
end
|
.put(action = '', params = {}, options = {}) ⇒ Object
43
44
45
|
# File 'lib/nestful/resource.rb', line 43
def self.put(action = '', params = {}, options = {})
request(uri(action), options.merge(:method => :put, :params => params))
end
|
.request(url, options = {}) ⇒ Object
.uri(*parts) ⇒ Object
35
36
37
|
# File 'lib/nestful/resource.rb', line 35
def self.uri(*parts)
URI.parse(Helpers.to_path(url, *parts))
end
|
.url ⇒ Object
31
32
33
|
# File 'lib/nestful/resource.rb', line 31
def self.url
URI.join(endpoint.to_s, path.to_s).to_s
end
|
Instance Method Details
#[](key) ⇒ Object
110
111
112
|
# File 'lib/nestful/resource.rb', line 110
def [](key)
attributes[key]
end
|
#[]=(key, value) ⇒ Object
114
115
116
|
# File 'lib/nestful/resource.rb', line 114
def []=(key,value)
attributes[key] = value
end
|
#delete(action = '', *args) ⇒ Object
94
95
96
|
# File 'lib/nestful/resource.rb', line 94
def delete(action = '', *args)
self.class.delete(path(action), *args)
end
|
#get(action = '', *args) ⇒ Object
82
83
84
|
# File 'lib/nestful/resource.rb', line 82
def get(action = '', *args)
self.class.get(path(action), *args)
end
|
#id ⇒ Object
102
103
104
|
# File 'lib/nestful/resource.rb', line 102
def id self['id']
end
|
#load(attributes = {}) ⇒ Object
128
129
130
131
132
|
# File 'lib/nestful/resource.rb', line 128
def load(attributes = {})
attributes.to_hash.each do |key, value|
send("#{key}=", value)
end
end
|
#path(*parts) ⇒ Object
98
99
100
|
# File 'lib/nestful/resource.rb', line 98
def path(*parts)
Helpers.to_path(self.id, *parts)
end
|
#post(action = '', *args) ⇒ Object
90
91
92
|
# File 'lib/nestful/resource.rb', line 90
def post(action = '', *args)
self.class.post(path(action), *args)
end
|
#put(action = '', *args) ⇒ Object
86
87
88
|
# File 'lib/nestful/resource.rb', line 86
def put(action = '', *args)
self.class.put(path(action), *args)
end
|
#respond_to?(method, include_priv = false) ⇒ Boolean
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/nestful/resource.rb', line 136
def respond_to?(method, include_priv = false)
method_name = method.to_s
if attributes.nil?
super
elsif attributes.include?(method_name.sub(/[=\?]\Z/, ''))
true
else
super
end
end
|
#respond_to_without_attributes? ⇒ Object
134
|
# File 'lib/nestful/resource.rb', line 134
alias_method :respond_to_without_attributes?, :respond_to?
|
#to_hash ⇒ Object
Also known as:
as_json
118
119
120
|
# File 'lib/nestful/resource.rb', line 118
def to_hash
attributes.dup
end
|
#to_json ⇒ Object
124
125
126
|
# File 'lib/nestful/resource.rb', line 124
def to_json(*)
as_json.to_json
end
|
#type ⇒ Object
106
107
108
|
# File 'lib/nestful/resource.rb', line 106
def type self['type']
end
|