Class: Rubix::Model
- Inherits:
-
Object
show all
- Extended by:
- Logs
- Includes:
- Logs
- Defined in:
- lib/rubix/models/model.rb
Overview
It might be worth using ActiveModel – but maybe not. The goal is to keep dependencies low while still retaining expressiveness.
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Logs
debug, error, fatal, info, log_name, warn
Constructor Details
#initialize(properties = {}) ⇒ Model
Returns a new instance of Model.
20
21
22
23
24
|
# File 'lib/rubix/models/model.rb', line 20
def initialize properties={}
@properties = properties
@id = properties[:id]
@log_name = self.class.model_name
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
7
8
9
|
# File 'lib/rubix/models/model.rb', line 7
def id
@id
end
|
#properties ⇒ Object
Returns the value of attribute properties.
7
8
9
|
# File 'lib/rubix/models/model.rb', line 7
def properties
@properties
end
|
Class Method Details
.find(options = {}) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/rubix/models/model.rb', line 38
def self.find options={}
response = find_request(options)
case
when response.has_data?
build(response.result.first)
when response.success?
else
error("Could not find #{options.inspect}: #{response.error_message}")
nil
end
end
|
.find_or_create(options = {}) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/rubix/models/model.rb', line 51
def self.find_or_create options={}
response = find_request(options)
case
when response.has_data?
build(response.result.first)
when response.success?
obj = new(options)
if obj.save
obj
else
false
end
else
error("Could not create #{options.inspect}: #{response.error_message}")
false
end
end
|
.log_name ⇒ Object
16
17
18
|
# File 'lib/rubix/models/model.rb', line 16
def self.log_name
model_name
end
|
.model_name ⇒ Object
12
13
14
|
# File 'lib/rubix/models/model.rb', line 12
def self.model_name
self.to_s.split('::').last
end
|
.request(method, params) ⇒ Object
Instance Method Details
#after_update ⇒ Object
101
102
103
|
# File 'lib/rubix/models/model.rb', line 101
def after_update
true
end
|
#create ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/rubix/models/model.rb', line 74
def create
return false unless validate
response = create_request
if response.has_data?
@id = response.result[self.class.id_field + 's'].first.to_i
info("Created")
true
else
error("Could not create: #{response.error_message}")
false
end
end
|
#destroy ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/rubix/models/model.rb', line 109
def destroy
return false if new_record?
response = destroy_request
case
when response.has_data? && response.result.values.first.first.to_i == id
info("Destroyed")
true
when response.zabbix_error? && response.error_message =~ /does not exist/i
true
else
error("Could not destroy: #{response.error_message}")
false
end
end
|
#new_record? ⇒ Boolean
26
27
28
|
# File 'lib/rubix/models/model.rb', line 26
def new_record?
@id.nil?
end
|
#request(method, params) ⇒ Object
30
31
32
|
# File 'lib/rubix/models/model.rb', line 30
def request method, params
self.class.request(method, params)
end
|
#save ⇒ Object
105
106
107
|
# File 'lib/rubix/models/model.rb', line 105
def save
new_record? ? create : update
end
|
#update ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/rubix/models/model.rb', line 87
def update
return false unless validate
return create if new_record?
response = update_request
if response.has_data?
info("Updated")
after_update
else
error("Could not update: #{response.error_message}")
false
end
after_update
end
|
#validate ⇒ Object
70
71
72
|
# File 'lib/rubix/models/model.rb', line 70
def validate
true
end
|