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 = {}) ⇒ Base
Returns a new instance of Base.
12
13
14
15
16
|
# File 'lib/static_model/base.rb', line 12
def initialize(attribute_hash = {})
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.count + 1)
self.attributes = attribute_hash
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/static_model/base.rb', line 154
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
51
52
53
|
# File 'lib/static_model/base.rb', line 51
def [](id)
find_by_id(id)
end
|
.class_attribute(name) ⇒ Object
116
117
118
|
# File 'lib/static_model/base.rb', line 116
def class_attribute(name)
class_attributes[name]
end
|
.class_attributes ⇒ Object
111
112
113
114
|
# File 'lib/static_model/base.rb', line 111
def class_attributes
load
@class_attributes ||= {}
end
|
.count ⇒ Object
107
108
109
|
# File 'lib/static_model/base.rb', line 107
def count
records.length
end
|
.data_file ⇒ Object
95
96
97
|
# File 'lib/static_model/base.rb', line 95
def data_file
@data_file ||= default_data_file_path
end
|
.find(what, *args, &block) ⇒ Object
36
37
38
39
40
41
42
43
|
# File 'lib/static_model/base.rb', line 36
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
55
56
57
|
# File 'lib/static_model/base.rb', line 55
def find_all
records
end
|
.find_all_by(attribute, value) ⇒ Object
65
66
67
|
# File 'lib/static_model/base.rb', line 65
def find_all_by(attribute, value)
records.find_all {|r| r.send(attribute) == value }
end
|
.find_by_id(id) ⇒ Object
45
46
47
48
49
|
# File 'lib/static_model/base.rb', line 45
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
60
61
62
|
# File 'lib/static_model/base.rb', line 60
def find_first
records[0]
end
|
.find_first_by(attribute, value) ⇒ Object
Also known as:
find_by
69
70
71
|
# File 'lib/static_model/base.rb', line 69
def find_first_by(attribute, value)
records.find {|r| r.send(attribute) == value }
end
|
.load(reload = false) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/static_model/base.rb', line 74
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)
data = YAML::load_file(data_file)
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
@records = records && !records.empty? ? records.dup.collect {|r| new(r) } : []
@loaded = true
end
|
.loaded? ⇒ Boolean
91
92
93
|
# File 'lib/static_model/base.rb', line 91
def loaded?
@loaded ||= false
end
|
.records ⇒ Object
120
121
122
123
|
# File 'lib/static_model/base.rb', line 120
def records
load
@records
end
|
.set_data_file(file_path) ⇒ Object
99
100
101
102
103
104
105
|
# File 'lib/static_model/base.rb', line 99
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
30
31
32
|
# File 'lib/static_model/base.rb', line 30
def attribute_names
(attributes.keys | self.class.class_attributes.keys).collect {|k| k.to_s }
end
|
#attributes ⇒ Object
22
23
24
|
# File 'lib/static_model/base.rb', line 22
def attributes
@attributes ||= HashWithIndifferentAccess.new
end
|
#attributes=(attribute_hash) ⇒ Object
26
27
28
|
# File 'lib/static_model/base.rb', line 26
def attributes=(attribute_hash)
attribute_hash.each {|k,v| set_attribute(k,v) }
end
|
#to_s ⇒ Object
18
19
20
|
# File 'lib/static_model/base.rb', line 18
def to_s
self.inspect
end
|