Class: StaticModel::Base
Constant Summary
collapse
- @@load_path =
File.join(Rails.root,'config', 'data')
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Comparable
#<=>
#new_record?
included
Constructor Details
#initialize(attribute_hash = {}, force_load = true) ⇒ Base
Returns a new instance of Base.
12
13
14
15
16
17
18
|
# File 'lib/static_model/base.rb', line 12
def initialize(attribute_hash = {}, force_load = true)
self.class.load if force_load
raise(StaticModel::BadOptions, "Initializing a model is done with a Hash {} given #{attribute_hash.inspect}") unless attribute_hash.is_a?(Hash)
@id = attribute_hash.delete('id') || attribute_hash.delete(:id) || self.class.next_id
self.attributes = attribute_hash
self.class.last_id = @id
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/static_model/base.rb', line 175
def method_missing(meth, *args)
attribute_name = meth.to_s.gsub(/=$/,'')
if attribute_names.include?(attribute_name)
if meth.to_s =~ /=$/
return set_attribute(attribute_name, args[0])
else
return get_attribute(attribute_name)
end
end
super
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
9
10
11
|
# File 'lib/static_model/base.rb', line 9
def id
@id
end
|
Class Method Details
.[](id) ⇒ Object
53
54
55
|
# File 'lib/static_model/base.rb', line 53
def [](id)
find_by_id(id)
end
|
.class_attribute(name) ⇒ Object
125
126
127
|
# File 'lib/static_model/base.rb', line 125
def class_attribute(name)
class_attributes[name]
end
|
.class_attributes ⇒ Object
120
121
122
123
|
# File 'lib/static_model/base.rb', line 120
def class_attributes
load
@class_attributes ||= {}
end
|
.count ⇒ Object
116
117
118
|
# File 'lib/static_model/base.rb', line 116
def count
records.length
end
|
.data_file ⇒ Object
104
105
106
|
# File 'lib/static_model/base.rb', line 104
def data_file
@data_file ||= default_data_file_path
end
|
.find(what, *args, &block) ⇒ Object
38
39
40
41
42
43
44
45
|
# File 'lib/static_model/base.rb', line 38
def find(what, *args, &block)
case what
when Symbol
send("find_#{what}")
when Integer
find_by_id(what)
end
end
|
.find_all ⇒ Object
Also known as:
all
57
58
59
|
# File 'lib/static_model/base.rb', line 57
def find_all
records
end
|
.find_all_by(attribute, value) ⇒ Object
67
68
69
|
# File 'lib/static_model/base.rb', line 67
def find_all_by(attribute, value)
records.find_all {|r| r.send(attribute) == value }
end
|
.find_by_id(id) ⇒ Object
47
48
49
50
51
|
# File 'lib/static_model/base.rb', line 47
def find_by_id(id)
record = records.detect {|r| r.id == id }
raise(StaticModel::RecordNotFound, "Could not find record with id = #{id}") unless record
record
end
|
.find_first ⇒ Object
Also known as:
first
62
63
64
|
# File 'lib/static_model/base.rb', line 62
def find_first
records[0]
end
|
.find_first_by(attribute, value) ⇒ Object
Also known as:
find_by
71
72
73
|
# File 'lib/static_model/base.rb', line 71
def find_first_by(attribute, value)
records.find {|r| r.send(attribute) == value }
end
|
.last_id ⇒ Object
138
139
140
|
# File 'lib/static_model/base.rb', line 138
def last_id
@last_id ||= 0
end
|
.last_id=(new_last_id) ⇒ Object
142
143
144
|
# File 'lib/static_model/base.rb', line 142
def last_id=(new_last_id)
@last_id = new_last_id if new_last_id > self.last_id
end
|
.load(reload = false) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/static_model/base.rb', line 76
def load(reload = false)
return if loaded? && !reload
raise(StaticModel::DataFileNotFound, "You must set a data file to load from") unless File.readable?(data_file)
begin
data = YAML::load_file(data_file)
rescue
raise(StaticModel::BadDataFile, "The data file you specified '#{data_file}' was not in a readable format.")
end
records = []
if data.is_a?(Hash) && data.has_key?('records')
records = data.delete('records')
@class_attributes = HashWithIndifferentAccess.new(data)
elsif data.is_a?(Array)
records = data
end
@last_id = 0
@records = records && !records.empty? ? records.dup.collect {|r| new(r, false) } : []
@loaded = true
end
|
.loaded? ⇒ Boolean
100
101
102
|
# File 'lib/static_model/base.rb', line 100
def loaded?
@loaded ||= false
end
|
.next_id ⇒ Object
134
135
136
|
# File 'lib/static_model/base.rb', line 134
def next_id
last_id + 1
end
|
.records ⇒ Object
129
130
131
132
|
# File 'lib/static_model/base.rb', line 129
def records
load
@records
end
|
.reload! ⇒ Object
96
97
98
|
# File 'lib/static_model/base.rb', line 96
def reload!
load(true)
end
|
.set_data_file(file_path) ⇒ Object
108
109
110
111
112
113
114
|
# File 'lib/static_model/base.rb', line 108
def set_data_file(file_path)
raise(StaticModel::DataFileNotFound, "Could not find data file #{file_path}") unless File.readable?(file_path)
@data_file = file_path
@loaded = false
@records = nil
end
|
Instance Method Details
#attribute_names ⇒ Object
32
33
34
|
# File 'lib/static_model/base.rb', line 32
def attribute_names
(attributes.keys | self.class.class_attributes.keys).collect {|k| k.to_s }
end
|
#attributes ⇒ Object
24
25
26
|
# File 'lib/static_model/base.rb', line 24
def attributes
@attributes ||= HashWithIndifferentAccess.new
end
|
#attributes=(attribute_hash) ⇒ Object
28
29
30
|
# File 'lib/static_model/base.rb', line 28
def attributes=(attribute_hash)
attribute_hash.each {|k,v| set_attribute(k,v) }
end
|
#to_s ⇒ Object
20
21
22
|
# File 'lib/static_model/base.rb', line 20
def to_s
self.inspect
end
|