Class: InventosStreak::Base
- Inherits:
-
Object
- Object
- InventosStreak::Base
show all
- Defined in:
- lib/inventos_streak/base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(args = {}) ⇒ Base
Returns a new instance of Base.
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/inventos_streak/base.rb', line 70
def initialize args = {}
set_parents
set_children
args.each do |k, v|
if self.respond_to? k
self.send("#{k}=", v)
else
raise ArgumentError.new("#{k} is not allowed.")
end
end
end
|
Class Method Details
.all ⇒ Object
3
4
5
|
# File 'lib/inventos_streak/base.rb', line 3
def all
InventosStreak.get(path)
end
|
.belongs_to(relation) ⇒ Object
16
17
18
|
# File 'lib/inventos_streak/base.rb', line 16
def belongs_to relation
parents.push relation
end
|
.children ⇒ Object
28
29
30
|
# File 'lib/inventos_streak/base.rb', line 28
def children
@children ||= []
end
|
.find(key, options = {}) ⇒ Object
7
8
9
10
11
12
13
14
|
# File 'lib/inventos_streak/base.rb', line 7
def find key, options = {}
options.merge!(key: key)
m = self.new options
InventosStreak.get(m.final_path).map do |k, v|
m.instance_variable_set("@#{k}", v)
end
m
end
|
.has_many(relation) ⇒ Object
20
21
22
|
# File 'lib/inventos_streak/base.rb', line 20
def has_many relation
children.push relation
end
|
.parent ⇒ Object
32
33
34
|
# File 'lib/inventos_streak/base.rb', line 32
def parent
@parents.first
end
|
.parents ⇒ Object
24
25
26
|
# File 'lib/inventos_streak/base.rb', line 24
def parents
@parents ||= []
end
|
Instance Method Details
#attributes ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/inventos_streak/base.rb', line 89
def attributes
h = {}
self.instance_variables.each do |iv|
val = self.instance_variable_get(iv)
if val.class.to_s.include? 'InventosStreak::'
h[iv.to_s.gsub('@', '').to_sym] = val.attributes
else
h[iv.to_s.gsub('@', '').to_sym] = val
end
end
h
end
|
#destroy ⇒ Object
61
62
63
64
65
66
67
68
|
# File 'lib/inventos_streak/base.rb', line 61
def destroy
if self.key == '' or self.key.nil?
raise ArgumentError.new("Cannot destroy a #{self.class.to_s} without a key")
else
InventosStreak.delete(self.final_path)
self.key = nil
end
end
|
#final_path ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/inventos_streak/base.rb', line 38
def final_path
url = []
m = self
while m != nil
unless m.key == '' or m.key.nil?
url.unshift m.key
end
url.unshift m.class.path
m = m.class.parent.nil? ? nil : m.send(m.class.parent)
end
url.join("/")
end
|
#save ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/inventos_streak/base.rb', line 53
def save
if self.key == '' or self.key.nil?
self.set_variables InventosStreak.put(self.final_path, self.attributes)
else
self.set_variables InventosStreak.post(self.final_path, self.attributes)
end
end
|
#set_variables(hash) ⇒ Object
83
84
85
86
87
|
# File 'lib/inventos_streak/base.rb', line 83
def set_variables hash
hash.each do |k, v|
self.instance_variable_set("@#{k}", v)
end
end
|