Class: BitBalloon::Model
- Inherits:
-
Object
show all
- Defined in:
- lib/bitballoon/model.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(client, attributes) ⇒ Model
Returns a new instance of Model.
27
28
29
30
31
32
|
# File 'lib/bitballoon/model.rb', line 27
def initialize(client, attributes)
@client = client
@attributes = {}
@prefix = attributes.delete(:prefix)
process(attributes)
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3
4
5
|
# File 'lib/bitballoon/model.rb', line 3
def attributes
@attributes
end
|
#client ⇒ Object
Returns the value of attribute client.
3
4
5
|
# File 'lib/bitballoon/model.rb', line 3
def client
@client
end
|
#prefix ⇒ Object
Returns the value of attribute prefix.
3
4
5
|
# File 'lib/bitballoon/model.rb', line 3
def prefix
@prefix
end
|
Class Method Details
.collection(value = nil) ⇒ Object
23
24
25
|
# File 'lib/bitballoon/model.rb', line 23
def self.collection(value = nil)
@collection ||= BitBalloon.const_get(to_s.split("::").last + "s")
end
|
.fields(*names) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/bitballoon/model.rb', line 5
def self.fields(*names)
return @fields if names.empty?
@fields ||= []
names.each do |name|
define_method name do
@attributes[name.to_sym]
end
define_method "#{name}=" do |value|
@attributes[name.to_sym] = value
end
@fields.push(name.to_sym)
end
end
|
Instance Method Details
#collection ⇒ Object
57
58
59
|
# File 'lib/bitballoon/model.rb', line 57
def collection
self.class.collection
end
|
#destroy ⇒ Object
48
49
50
|
# File 'lib/bitballoon/model.rb', line 48
def destroy
client.request(:delete, path)
end
|
#path ⇒ Object
61
62
63
|
# File 'lib/bitballoon/model.rb', line 61
def path
::File.join(*[prefix, collection.path, id.to_s].compact)
end
|
#process(attributes) ⇒ Object
34
35
36
37
38
39
40
41
|
# File 'lib/bitballoon/model.rb', line 34
def process(attributes)
self.class.fields.each do |field|
if attributes.has_key?(field) || attributes.has_key?(field.to_s)
@attributes[field] = attributes[field] || attributes[field.to_s]
end
end
self
end
|
#refresh ⇒ Object
52
53
54
55
|
# File 'lib/bitballoon/model.rb', line 52
def refresh
response = client.request(:get, path)
process(response.parsed)
end
|
#update(attributes) ⇒ Object
43
44
45
46
|
# File 'lib/bitballoon/model.rb', line 43
def update(attributes)
response = client.request(:put, path, :body => attributes)
process(response.parsed) if response.parsed
end
|