static_model 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -1
- data/lib/static_model/base.rb +28 -7
- data/lib/static_model/version.rb +1 -1
- data/test/test_helper.rb +4 -0
- data/test/test_static_model.rb +63 -0
- metadata +5 -6
data/History.txt
CHANGED
data/lib/static_model/base.rb
CHANGED
@@ -9,10 +9,12 @@ module StaticModel
|
|
9
9
|
attr_reader :id
|
10
10
|
|
11
11
|
|
12
|
-
def initialize(attribute_hash = {})
|
12
|
+
def initialize(attribute_hash = {}, force_load = true)
|
13
|
+
self.class.load if force_load
|
13
14
|
raise(StaticModel::BadOptions, "Initializing a model is done with a Hash {} given #{attribute_hash.inspect}") unless attribute_hash.is_a?(Hash)
|
14
|
-
@id = attribute_hash.delete('id') || attribute_hash.delete(:id) ||
|
15
|
+
@id = attribute_hash.delete('id') || attribute_hash.delete(:id) || self.class.next_id
|
15
16
|
self.attributes = attribute_hash
|
17
|
+
self.class.last_id = @id
|
16
18
|
end
|
17
19
|
|
18
20
|
def to_s
|
@@ -74,7 +76,11 @@ module StaticModel
|
|
74
76
|
def load(reload = false)
|
75
77
|
return if loaded? && !reload
|
76
78
|
raise(StaticModel::DataFileNotFound, "You must set a data file to load from") unless File.readable?(data_file)
|
77
|
-
|
79
|
+
begin
|
80
|
+
data = YAML::load_file(data_file)
|
81
|
+
rescue
|
82
|
+
raise(StaticModel::BadDataFile, "The data file you specified '#{data_file}' was not in a readable format.")
|
83
|
+
end
|
78
84
|
records = []
|
79
85
|
if data.is_a?(Hash) && data.has_key?('records')
|
80
86
|
records = data.delete('records')
|
@@ -82,10 +88,13 @@ module StaticModel
|
|
82
88
|
elsif data.is_a?(Array)
|
83
89
|
records = data
|
84
90
|
end
|
85
|
-
@
|
91
|
+
@last_id = 0
|
92
|
+
@records = records && !records.empty? ? records.dup.collect {|r| new(r, false) } : []
|
86
93
|
@loaded = true
|
87
|
-
|
88
|
-
|
94
|
+
end
|
95
|
+
|
96
|
+
def reload!
|
97
|
+
load(true)
|
89
98
|
end
|
90
99
|
|
91
100
|
def loaded?
|
@@ -121,12 +130,24 @@ module StaticModel
|
|
121
130
|
load
|
122
131
|
@records
|
123
132
|
end
|
133
|
+
|
134
|
+
def next_id
|
135
|
+
last_id + 1
|
136
|
+
end
|
137
|
+
|
138
|
+
def last_id
|
139
|
+
@last_id ||= 0
|
140
|
+
end
|
124
141
|
|
142
|
+
def last_id=(new_last_id)
|
143
|
+
@last_id = new_last_id if new_last_id > self.last_id
|
144
|
+
end
|
145
|
+
|
125
146
|
protected
|
126
147
|
def default_data_file_path
|
127
148
|
File.join(@@load_path, "#{self.to_s.tableize}.yml")
|
128
149
|
end
|
129
|
-
|
150
|
+
|
130
151
|
private
|
131
152
|
def method_missing(meth, *args)
|
132
153
|
meth_name = meth.to_s
|
data/lib/static_model/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
data/test/test_static_model.rb
CHANGED
@@ -85,6 +85,69 @@ class TestStaticModel < Test::Unit::TestCase
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
+
context "next_id" do
|
89
|
+
context "if id's were automaticaly assigned" do
|
90
|
+
should "be length + 1" do
|
91
|
+
Store.load
|
92
|
+
assert_equal Store.count + 1, Store.next_id
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "if ids were manualy assigned" do
|
97
|
+
should "be 1 after the last id" do
|
98
|
+
Publisher.load
|
99
|
+
assert_equal 8, Publisher.next_id
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context "where the records are defined without ids" do
|
105
|
+
setup do
|
106
|
+
Store.reload!
|
107
|
+
end
|
108
|
+
|
109
|
+
context "loading" do
|
110
|
+
setup do
|
111
|
+
@stores = Store.all
|
112
|
+
end
|
113
|
+
|
114
|
+
should "automaticaly assign ids" do
|
115
|
+
@stores.each do |store|
|
116
|
+
assert store.id
|
117
|
+
assert store.id.is_a?(Fixnum)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
should "assign next id" do
|
122
|
+
assert Store.next_id
|
123
|
+
assert_equal 3, Store.next_id
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "initializing without id" do
|
128
|
+
setup do
|
129
|
+
@store = Store.new({:name => 'Metro Comics', :city => 'New York'})
|
130
|
+
end
|
131
|
+
|
132
|
+
should "return instance" do
|
133
|
+
assert @store.is_a?(Store)
|
134
|
+
end
|
135
|
+
|
136
|
+
should "set attributes" do
|
137
|
+
assert_equal 'New York', @store.city
|
138
|
+
end
|
139
|
+
|
140
|
+
should "assign id from next id" do
|
141
|
+
assert_equal 3, @store.id
|
142
|
+
end
|
143
|
+
|
144
|
+
should "increment next_id" do
|
145
|
+
assert_equal 4, Store.next_id
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
88
151
|
context "find" do
|
89
152
|
|
90
153
|
context "with an integer" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: static_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Quint
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-12-03 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 1.8.
|
33
|
+
version: 1.8.2
|
34
34
|
version:
|
35
35
|
description: ActiveRecord like functionalities for reading from YAML with a simple class implementation
|
36
36
|
email:
|
@@ -43,7 +43,6 @@ extra_rdoc_files:
|
|
43
43
|
- History.txt
|
44
44
|
- License.txt
|
45
45
|
- Manifest.txt
|
46
|
-
- README.rdoc
|
47
46
|
files:
|
48
47
|
- History.txt
|
49
48
|
- License.txt
|
@@ -67,7 +66,7 @@ homepage: http://quirkey.rubyforge.org
|
|
67
66
|
post_install_message: ""
|
68
67
|
rdoc_options:
|
69
68
|
- --main
|
70
|
-
- README.
|
69
|
+
- README.txt
|
71
70
|
require_paths:
|
72
71
|
- lib
|
73
72
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -85,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
84
|
requirements: []
|
86
85
|
|
87
86
|
rubyforge_project: quirkey
|
88
|
-
rubygems_version: 1.
|
87
|
+
rubygems_version: 1.2.0
|
89
88
|
signing_key:
|
90
89
|
specification_version: 2
|
91
90
|
summary: ActiveRecord like functionalities for reading from YAML with a simple class implementation
|