static_model 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.3.1 2010-10-22
2
+
3
+ * Ruby 1.9.2 Compatibility fixes
4
+
1
5
  == 0.3.0 2010-05-06
2
6
 
3
7
  * Attributes can now be defined within the class with
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ begin
13
13
  s.summary = 'ActiveRecord like functionalities for reading from YAML with a simple class implementation'
14
14
  s.description = %q{StaticModel provides a Base class much like ActiveRecord which supports reading from a YAML file and basic associations to ActiveRecord}
15
15
  s.rubyforge_project = %q{quirkey}
16
- s.add_runtime_dependency(%q<activesupport>, [">= 2.2.0"])
16
+ s.add_runtime_dependency(%q<activesupport>, ["~>2.3.8"])
17
17
  s.add_runtime_dependency(%q<rubigen>, [">= 1.5.1"])
18
18
  s.add_development_dependency(%q<Shoulda>, [">= 1.2.0"])
19
19
  end
@@ -1,13 +1,13 @@
1
- module StaticModel
1
+ module StaticModel
2
2
  class Base
3
3
  include StaticModel::Associations
4
4
  include StaticModel::ActiveRecord
5
5
  include StaticModel::Comparable
6
-
6
+
7
7
  @@load_path = File.join('config', 'data')
8
8
 
9
9
  attr_reader :id
10
-
10
+
11
11
  def initialize(attribute_hash = {}, force_load = true)
12
12
  self.class.load if force_load
13
13
  raise(StaticModel::BadOptions, "Initializing a model is done with a Hash {} given #{attribute_hash.inspect}") unless attribute_hash.is_a?(Hash)
@@ -17,7 +17,7 @@ module StaticModel
17
17
  end
18
18
 
19
19
  def to_s
20
- self.inspect
20
+ "<#{self.class} #{@attributes.inspect}>"
21
21
  end
22
22
 
23
23
  def self.attribute(name, options = {})
@@ -27,24 +27,24 @@ module StaticModel
27
27
  }.merge(options)
28
28
  @defined_attributes ||= {}
29
29
  @defined_attributes[name.to_s] = options
30
-
30
+
31
31
  module_eval <<-EOT
32
32
  def #{name}
33
33
  @attributes['#{name}'] || #{options[:default].inspect}
34
34
  end
35
-
35
+
36
36
  def #{name}=(value)
37
37
  if !#{options[:freeze].inspect}
38
38
  @attributes['#{name}'] = value
39
39
  end
40
40
  end
41
-
41
+
42
42
  def #{name}?
43
43
  !!#{name}
44
44
  end
45
45
  EOT
46
46
  end
47
-
47
+
48
48
  def self.defined_attributes
49
49
  @defined_attributes || {}
50
50
  end
@@ -60,7 +60,7 @@ module StaticModel
60
60
  def attribute_names
61
61
  (attributes.keys | self.class.class_attributes.keys | self.class.defined_attributes.keys).collect {|k| k.to_s }
62
62
  end
63
-
63
+
64
64
  def has_attribute?(name)
65
65
  name.to_s == 'id' || attribute_names.include?(name.to_s)
66
66
  end
@@ -95,7 +95,7 @@ module StaticModel
95
95
  records[0]
96
96
  end
97
97
  alias_method :first, :find_first
98
-
98
+
99
99
  def find_last
100
100
  records[records.length-1]
101
101
  end
@@ -109,7 +109,7 @@ module StaticModel
109
109
  records.find {|r| r.has_attribute?(attribute) ? (r.send(attribute) == value) : false }
110
110
  end
111
111
  alias_method :find_by, :find_first_by
112
-
112
+
113
113
  def find_last_by(attribute, value)
114
114
  records.reverse.find {|r| r.has_attribute?(attribute) ? (r.send(attribute) == value) : false }
115
115
  end
@@ -134,7 +134,7 @@ module StaticModel
134
134
  @records = records && !records.empty? ? records.dup.collect {|r| new(r, false) } : []
135
135
  @loaded = true
136
136
  end
137
-
137
+
138
138
  def reload!
139
139
  load(true)
140
140
  end
@@ -154,29 +154,29 @@ module StaticModel
154
154
  @loaded = false
155
155
  @records = nil
156
156
  end
157
-
157
+
158
158
  def count
159
159
  records.length
160
160
  end
161
-
161
+
162
162
  def class_attributes
163
163
  load
164
164
  @class_attributes ||= {}
165
165
  end
166
-
166
+
167
167
  def class_attribute(name)
168
168
  class_attributes[name]
169
169
  end
170
-
170
+
171
171
  def records
172
172
  load
173
173
  @records
174
174
  end
175
-
175
+
176
176
  def next_id
177
177
  last_id + 1
178
178
  end
179
-
179
+
180
180
  def last_id
181
181
  @last_id ||= 0
182
182
  end
@@ -184,12 +184,12 @@ module StaticModel
184
184
  def last_id=(new_last_id)
185
185
  @last_id = new_last_id if new_last_id > self.last_id
186
186
  end
187
-
187
+
188
188
  protected
189
189
  def default_data_file_path
190
190
  File.join(@@load_path, "#{self.to_s.tableize}.yml")
191
191
  end
192
-
192
+
193
193
  private
194
194
  def method_missing(meth, *args)
195
195
  meth_name = meth.to_s
@@ -202,13 +202,13 @@ module StaticModel
202
202
  end
203
203
  super
204
204
  end
205
- end
205
+ end
206
206
 
207
207
  protected
208
208
  def set_attribute(name, value)
209
209
  self.attributes[name] = value
210
210
  end
211
-
211
+
212
212
  def get_attribute(name)
213
213
  attributes.has_key?(name) ? attributes[name] : self.class.class_attribute(name)
214
214
  end
@@ -227,6 +227,6 @@ module StaticModel
227
227
  end
228
228
  super
229
229
  end
230
-
230
+
231
231
  end
232
- end
232
+ end
data/lib/static_model.rb CHANGED
@@ -4,10 +4,11 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'yaml' unless defined?(YAML)
5
5
  require 'erb' unless defined?(ERB)
6
6
 
7
+ gem 'activesupport', '~>2.3.8'
7
8
  require 'active_support'
8
9
 
9
10
  module StaticModel
10
- VERSION = '0.3.0'
11
+ VERSION = '0.3.1'
11
12
  end
12
13
 
13
14
  require 'static_model/errors'
@@ -16,4 +17,3 @@ require 'static_model/active_record'
16
17
  require 'static_model/comparable'
17
18
  require 'static_model/base'
18
19
  require 'static_model/rails'
19
-
data/static_model.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{static_model}
8
- s.version = "0.3.0"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Aaron Quint"]
12
- s.date = %q{2010-05-06}
12
+ s.date = %q{2010-10-22}
13
13
  s.description = %q{StaticModel provides a Base class much like ActiveRecord which supports reading from a YAML file and basic associations to ActiveRecord}
14
14
  s.email = %q{aaron at quirkey dot com}
15
15
  s.extra_rdoc_files = [
@@ -54,7 +54,7 @@ Gem::Specification.new do |s|
54
54
  s.rdoc_options = ["--charset=UTF-8"]
55
55
  s.require_paths = ["lib"]
56
56
  s.rubyforge_project = %q{quirkey}
57
- s.rubygems_version = %q{1.3.6}
57
+ s.rubygems_version = %q{1.3.7}
58
58
  s.summary = %q{ActiveRecord like functionalities for reading from YAML with a simple class implementation}
59
59
  s.test_files = [
60
60
  "test/test_generator_helper.rb",
@@ -69,17 +69,17 @@ Gem::Specification.new do |s|
69
69
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
70
70
  s.specification_version = 3
71
71
 
72
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
73
- s.add_runtime_dependency(%q<activesupport>, [">= 2.2.0"])
72
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
73
+ s.add_runtime_dependency(%q<activesupport>, ["~> 2.3.8"])
74
74
  s.add_runtime_dependency(%q<rubigen>, [">= 1.5.1"])
75
75
  s.add_development_dependency(%q<Shoulda>, [">= 1.2.0"])
76
76
  else
77
- s.add_dependency(%q<activesupport>, [">= 2.2.0"])
77
+ s.add_dependency(%q<activesupport>, ["~> 2.3.8"])
78
78
  s.add_dependency(%q<rubigen>, [">= 1.5.1"])
79
79
  s.add_dependency(%q<Shoulda>, [">= 1.2.0"])
80
80
  end
81
81
  else
82
- s.add_dependency(%q<activesupport>, [">= 2.2.0"])
82
+ s.add_dependency(%q<activesupport>, ["~> 2.3.8"])
83
83
  s.add_dependency(%q<rubigen>, [">= 1.5.1"])
84
84
  s.add_dependency(%q<Shoulda>, [">= 1.2.0"])
85
85
  end
@@ -1,8 +1,4 @@
1
- begin
2
- require File.dirname(__FILE__) + '/test_helper'
3
- rescue LoadError
4
- require 'test/unit'
5
- end
1
+ require 'test_helper'
6
2
  require 'fileutils'
7
3
 
8
4
  # Must set before requiring generator libs.
data/test/test_helper.rb CHANGED
@@ -3,11 +3,11 @@ require 'rubygems'
3
3
  require 'shoulda'
4
4
  require 'mocha'
5
5
 
6
- require File.dirname(__FILE__) + '/../lib/static_model'
6
+ require File.expand_path(File.join(File.dirname(__FILE__), '../lib/static_model'))
7
7
 
8
8
  class Book < StaticModel::Base
9
9
  set_data_file File.join(File.dirname(__FILE__), 'data', 'books.yml')
10
-
10
+
11
11
  attribute :rating, :default => 3
12
12
  attribute :read, :default => false, :freeze => true
13
13
  end
@@ -26,7 +26,7 @@ end
26
26
 
27
27
  class Publisher < StaticModel::Base
28
28
  set_data_file File.join(File.dirname(__FILE__), 'data', 'publishers.yml')
29
-
29
+
30
30
  has_many :authors
31
31
  end
32
32
 
@@ -39,12 +39,12 @@ end
39
39
 
40
40
  class Page < StaticModel::Base
41
41
  set_data_file File.join(File.dirname(__FILE__), 'data', 'pages.yml')
42
-
42
+
43
43
  end
44
-
44
+
45
45
  class Store < StaticModel::Base
46
46
  set_data_file File.join(File.dirname(__FILE__), 'data', 'stores.yml')
47
-
47
+
48
48
  end
49
49
 
50
50
  class Project < StaticModel::Base
@@ -53,7 +53,7 @@ class Project < StaticModel::Base
53
53
  end
54
54
 
55
55
  class Test::Unit::TestCase
56
-
56
+
57
57
  def assert_all(collection)
58
58
  collection.each do |one|
59
59
  assert yield(one), "#{one} is not true"
@@ -82,9 +82,9 @@ class Test::Unit::TestCase
82
82
  i += 1
83
83
  end
84
84
  end
85
-
85
+
86
86
  def assert_set_of(klass, set)
87
87
  assert set.respond_to?(:each), "#{set} is not a set (does not include Enumerable)"
88
88
  assert_all(set) {|a| a.is_a?(klass) }
89
89
  end
90
- end
90
+ end
@@ -1,9 +1,9 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
1
+ require 'test_helper'
2
2
 
3
3
  class TestStaticModel < Test::Unit::TestCase
4
4
 
5
5
  context "StaticModel" do
6
- context "A class that inherits from Base" do
6
+ context "A class that inherits from Base" do
7
7
  context "an instance" do
8
8
 
9
9
  setup do
@@ -20,7 +20,7 @@ class TestStaticModel < Test::Unit::TestCase
20
20
  assert_raise(StaticModel::BadOptions) do
21
21
  Book.new("im bad")
22
22
  end
23
- end
23
+ end
24
24
  end
25
25
 
26
26
  context "attributes" do
@@ -34,8 +34,8 @@ class TestStaticModel < Test::Unit::TestCase
34
34
  assert_equal 'New Title', @book.title
35
35
  end
36
36
  end
37
-
38
- context "attribute" do
37
+
38
+ context "attribute" do
39
39
  should "define methods for the attribute" do
40
40
  book = Book[1]
41
41
  assert book.respond_to?(:rating)
@@ -43,7 +43,7 @@ class TestStaticModel < Test::Unit::TestCase
43
43
  assert book.respond_to?(:rating?)
44
44
  assert_equal 5, book.rating
45
45
  end
46
-
46
+
47
47
  should "return the default if attribute is not set" do
48
48
  book = Book[2]
49
49
  assert book.respond_to?(:rating)
@@ -51,27 +51,21 @@ class TestStaticModel < Test::Unit::TestCase
51
51
  assert book.respond_to?(:rating?)
52
52
  assert_equal 3, book.rating
53
53
  end
54
-
54
+
55
55
  should "freeze the attribute" do
56
56
  book = Book[1]
57
57
  assert book.read?
58
58
  book.read = false
59
59
  assert book.read?
60
60
  end
61
-
62
- end
63
61
 
64
- context "to_s" do
65
- should "inspect" do
66
- assert_equal @book.inspect, @book.to_s
67
- end
68
62
  end
69
-
63
+
70
64
  context "has_attribute?" do
71
65
  should "return false if attribute doesn't exist" do
72
66
  assert !@book.has_attribute?(:blurgh)
73
67
  end
74
-
68
+
75
69
  should "return true if attribute exists" do
76
70
  assert @book.has_attribute?(:id)
77
71
  assert @book.has_attribute?(:title)
@@ -113,7 +107,7 @@ class TestStaticModel < Test::Unit::TestCase
113
107
  end
114
108
 
115
109
  should "reload with next find" do
116
- assert @author.attributes, @new_book.attributes
110
+ assert_equal @author.attributes, @new_book.attributes
117
111
  end
118
112
 
119
113
  teardown do
@@ -127,9 +121,9 @@ class TestStaticModel < Test::Unit::TestCase
127
121
  should "be length + 1" do
128
122
  Store.load
129
123
  assert_equal Store.count + 1, Store.next_id
130
- end
124
+ end
131
125
  end
132
-
126
+
133
127
  context "if ids were manualy assigned" do
134
128
  should "be 1 after the last id" do
135
129
  Publisher.load
@@ -137,63 +131,63 @@ class TestStaticModel < Test::Unit::TestCase
137
131
  end
138
132
  end
139
133
  end
140
-
134
+
141
135
  context "where the records are defined without ids" do
142
136
  setup do
143
137
  Store.reload!
144
138
  end
145
-
139
+
146
140
  context "loading" do
147
141
  setup do
148
142
  @stores = Store.all
149
143
  end
150
-
144
+
151
145
  should "automaticaly assign ids" do
152
146
  @stores.each do |store|
153
147
  assert store.id
154
148
  assert store.id.is_a?(Fixnum)
155
149
  end
156
150
  end
157
-
151
+
158
152
  should "assign next id" do
159
153
  assert Store.next_id
160
154
  assert_equal 3, Store.next_id
161
155
  end
162
156
  end
163
-
157
+
164
158
  context "initializing without id" do
165
159
  setup do
166
160
  @store = Store.new({:name => 'Metro Comics', :city => 'New York'})
167
161
  end
168
-
162
+
169
163
  should "return instance" do
170
164
  assert @store.is_a?(Store)
171
165
  end
172
-
166
+
173
167
  should "set attributes" do
174
168
  assert_equal 'New York', @store.city
175
169
  end
176
-
170
+
177
171
  should "assign id from next id" do
178
172
  assert_equal 3, @store.id
179
173
  end
180
-
174
+
181
175
  should "increment next_id" do
182
176
  assert_equal 4, Store.next_id
183
177
  end
184
-
178
+
185
179
  end
186
180
  end
187
-
181
+
188
182
  context "loading a data_file with embedded erb" do
189
183
  setup do
190
184
  @projects = Project.all
191
185
  end
192
-
186
+
193
187
  should "evaluate erb expressions at load time" do
194
188
  assert_equal 1.day.ago.strftime('%m/%d/%Y %I:%M%p'), @projects.first.created_at
195
189
  end
196
-
190
+
197
191
  should "evaluate erb in current context" do
198
192
  assert_equal Author.first, @projects[1].author
199
193
  end
@@ -282,11 +276,11 @@ class TestStaticModel < Test::Unit::TestCase
282
276
  setup do
283
277
  @book = Book.find_last
284
278
  end
285
-
279
+
286
280
  should "return the last instance from all records" do
287
281
  assert_equal Book.find_all.last, @book
288
282
  end
289
-
283
+
290
284
  should "return an instance of klass" do
291
285
  assert @book.is_a?(Book)
292
286
  end
@@ -349,21 +343,21 @@ class TestStaticModel < Test::Unit::TestCase
349
343
  end
350
344
  end
351
345
  end
352
-
346
+
353
347
  context "find_last_by" do
354
348
  setup do
355
349
  @author = 'Chuck Palahniuk'
356
350
  @book = Book.find_last_by(:author, @author)
357
351
  end
358
-
352
+
359
353
  should "return an instance of klass" do
360
354
  assert @book.is_a?(Book)
361
355
  end
362
-
356
+
363
357
  should "return record matching search" do
364
358
  assert @author, @book.author
365
359
  end
366
-
360
+
367
361
  context "when there is no match" do
368
362
  should "return nil" do
369
363
  assert_nil Book.find_last_by(:author, 'Michael R. Bernstein')
@@ -454,40 +448,40 @@ class TestStaticModel < Test::Unit::TestCase
454
448
  assert_equal Book.all.length, Book.count
455
449
  end
456
450
  end
457
-
451
+
458
452
  context "with a class with yaml class vars" do
459
453
  setup do
460
454
  @pages = Page.all
461
455
  end
462
-
456
+
463
457
  should "load :records into @records" do
464
458
  assert_set_of Page, @pages
465
459
  assert Page.loaded?
466
460
  end
467
-
461
+
468
462
  should "give access to top level attributes as class methods" do
469
463
  assert_equal 'http://www.quirkey.com', Page.url
470
464
  assert_equal 'The Best Ever', Page.title
471
465
  end
472
-
466
+
473
467
  should "return a hash for class attribute" do
474
468
  assert Page.settings.is_a?(Hash)
475
469
  assert_equal 'test', Page.settings['username']
476
470
  end
477
-
471
+
478
472
  should "have class attributes appear as record accessor defaults if none exist" do
479
473
  assert_equal 'http://www.quirkey.com', Page[1].url
480
474
  end
481
-
475
+
482
476
  should "not overwrite record specific methods" do
483
477
  assert_equal 'http://github.com', Page[2].url
484
478
  end
485
-
479
+
486
480
  context "class_attributes" do
487
481
  setup do
488
482
  @attributes = Page.class_attributes
489
483
  end
490
-
484
+
491
485
  should "return a hash of all top level settings" do
492
486
  assert @attributes.is_a?(Hash)
493
487
  assert_equal 3, @attributes.length
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
1
+ require 'test_helper'
2
2
 
3
3
  class TestStaticModelAssociations < Test::Unit::TestCase
4
4
 
@@ -90,4 +90,4 @@ class TestStaticModelAssociations < Test::Unit::TestCase
90
90
  end
91
91
 
92
92
  end
93
- end
93
+ end
@@ -1,5 +1,4 @@
1
- require File.join(File.dirname(__FILE__), "test_generator_helper.rb")
2
-
1
+ require 'test_helper'
3
2
 
4
3
  class TestStaticModelGenerator < Test::Unit::TestCase
5
4
  include RubiGen::GeneratorTestHelper
@@ -1,14 +1,14 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
1
+ require 'test_helper'
2
2
 
3
3
  class TestStaticModelScope < Test::Unit::TestCase
4
4
 
5
5
  # context "a static model class" do
6
6
  # context "A class with scope" do
7
- #
7
+ #
8
8
  # end
9
9
  # end
10
-
10
+
11
11
  def test_truth
12
12
  assert true
13
13
  end
14
- end
14
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Aaron Quint
@@ -14,27 +14,29 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-06 00:00:00 -04:00
17
+ date: 2010-10-22 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activesupport
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
- - - ">="
26
+ - - ~>
26
27
  - !ruby/object:Gem::Version
27
28
  segments:
28
29
  - 2
29
- - 2
30
- - 0
31
- version: 2.2.0
30
+ - 3
31
+ - 8
32
+ version: 2.3.8
32
33
  type: :runtime
33
34
  version_requirements: *id001
34
35
  - !ruby/object:Gem::Dependency
35
36
  name: rubigen
36
37
  prerelease: false
37
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
38
40
  requirements:
39
41
  - - ">="
40
42
  - !ruby/object:Gem::Version
@@ -49,6 +51,7 @@ dependencies:
49
51
  name: Shoulda
50
52
  prerelease: false
51
53
  requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
52
55
  requirements:
53
56
  - - ">="
54
57
  - !ruby/object:Gem::Version
@@ -111,6 +114,7 @@ rdoc_options:
111
114
  require_paths:
112
115
  - lib
113
116
  required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
114
118
  requirements:
115
119
  - - ">="
116
120
  - !ruby/object:Gem::Version
@@ -118,6 +122,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
122
  - 0
119
123
  version: "0"
120
124
  required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
121
126
  requirements:
122
127
  - - ">="
123
128
  - !ruby/object:Gem::Version
@@ -127,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
132
  requirements: []
128
133
 
129
134
  rubyforge_project: quirkey
130
- rubygems_version: 1.3.6
135
+ rubygems_version: 1.3.7
131
136
  signing_key:
132
137
  specification_version: 3
133
138
  summary: ActiveRecord like functionalities for reading from YAML with a simple class implementation