static_model 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,14 @@
1
+ == 0.3.0 2010-05-06
2
+
3
+ * Attributes can now be defined within the class with
4
+
5
+ attribute :name, options
6
+
7
+ These create the actual methods to ensure that this model/instance always
8
+ responds to the accessors for this attribute
9
+
10
+ * Mock out destroyed? for AR 2.3.5
11
+
1
12
  == 0.2.4 2009-12-14
2
13
 
3
14
  * Finds don't care if attribute doesn't exist on certain records
data/Rakefile CHANGED
@@ -6,6 +6,8 @@ begin
6
6
  Jeweler::Tasks.new do |s|
7
7
  s.name = %q{static_model}
8
8
  s.version = StaticModel::VERSION
9
+ s.email = %{aaron at quirkey dot com}
10
+ s.homepage = "http://github.com/quirkey/static_model"
9
11
  s.authors = ["Aaron Quint"]
10
12
  s.date = %q{2009-12-03}
11
13
  s.summary = 'ActiveRecord like functionalities for reading from YAML with a simple class implementation'
@@ -6,6 +6,9 @@ module StaticModel
6
6
  false
7
7
  end
8
8
 
9
+ def destroyed?
10
+ false
11
+ end
9
12
 
10
13
  end
11
14
  end
@@ -8,7 +8,6 @@ module StaticModel
8
8
 
9
9
  attr_reader :id
10
10
 
11
-
12
11
  def initialize(attribute_hash = {}, force_load = true)
13
12
  self.class.load if force_load
14
13
  raise(StaticModel::BadOptions, "Initializing a model is done with a Hash {} given #{attribute_hash.inspect}") unless attribute_hash.is_a?(Hash)
@@ -21,6 +20,35 @@ module StaticModel
21
20
  self.inspect
22
21
  end
23
22
 
23
+ def self.attribute(name, options = {})
24
+ options = {
25
+ :default => nil,
26
+ :freeze => false
27
+ }.merge(options)
28
+ @defined_attributes ||= {}
29
+ @defined_attributes[name.to_s] = options
30
+
31
+ module_eval <<-EOT
32
+ def #{name}
33
+ @attributes['#{name}'] || #{options[:default].inspect}
34
+ end
35
+
36
+ def #{name}=(value)
37
+ if !#{options[:freeze].inspect}
38
+ @attributes['#{name}'] = value
39
+ end
40
+ end
41
+
42
+ def #{name}?
43
+ !!#{name}
44
+ end
45
+ EOT
46
+ end
47
+
48
+ def self.defined_attributes
49
+ @defined_attributes || {}
50
+ end
51
+
24
52
  def attributes
25
53
  @attributes ||= HashWithIndifferentAccess.new
26
54
  end
@@ -30,7 +58,7 @@ module StaticModel
30
58
  end
31
59
 
32
60
  def attribute_names
33
- (attributes.keys | self.class.class_attributes.keys).collect {|k| k.to_s }
61
+ (attributes.keys | self.class.class_attributes.keys | self.class.defined_attributes.keys).collect {|k| k.to_s }
34
62
  end
35
63
 
36
64
  def has_attribute?(name)
@@ -88,7 +116,6 @@ module StaticModel
88
116
 
89
117
  def load(reload = false)
90
118
  return if loaded? && !reload
91
- raise(StaticModel::DataFileNotFound, "You must set a data file to load from") unless File.readable?(data_file)
92
119
  begin
93
120
  raw_data = File.open(data_file) {|f| f.read }
94
121
  parsed_data = ERB.new(raw_data).result
@@ -157,7 +184,7 @@ module StaticModel
157
184
  def last_id=(new_last_id)
158
185
  @last_id = new_last_id if new_last_id > self.last_id
159
186
  end
160
-
187
+
161
188
  protected
162
189
  def default_data_file_path
163
190
  File.join(@@load_path, "#{self.to_s.tableize}.yml")
@@ -202,4 +229,4 @@ module StaticModel
202
229
  end
203
230
 
204
231
  end
205
- end
232
+ end
data/lib/static_model.rb CHANGED
@@ -7,7 +7,7 @@ require 'erb' unless defined?(ERB)
7
7
  require 'active_support'
8
8
 
9
9
  module StaticModel
10
- VERSION = '0.2.4'
10
+ VERSION = '0.3.0'
11
11
  end
12
12
 
13
13
  require 'static_model/errors'
data/static_model.gemspec CHANGED
@@ -5,12 +5,13 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{static_model}
8
- s.version = "0.2.4"
8
+ s.version = "0.3.0"
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{2009-12-14}
12
+ s.date = %q{2010-05-06}
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
+ s.email = %q{aaron at quirkey dot com}
14
15
  s.extra_rdoc_files = [
15
16
  "README.rdoc"
16
17
  ]
@@ -49,10 +50,11 @@ Gem::Specification.new do |s|
49
50
  "test/test_static_model_generator.rb",
50
51
  "test/test_static_model_scope.rb"
51
52
  ]
53
+ s.homepage = %q{http://github.com/quirkey/static_model}
52
54
  s.rdoc_options = ["--charset=UTF-8"]
53
55
  s.require_paths = ["lib"]
54
56
  s.rubyforge_project = %q{quirkey}
55
- s.rubygems_version = %q{1.3.5}
57
+ s.rubygems_version = %q{1.3.6}
56
58
  s.summary = %q{ActiveRecord like functionalities for reading from YAML with a simple class implementation}
57
59
  s.test_files = [
58
60
  "test/test_generator_helper.rb",
data/test/data/books.yml CHANGED
@@ -4,6 +4,8 @@
4
4
  author: Michael Pollan
5
5
  author_id: 1
6
6
  genre: Non-Fiction
7
+ rating: 5
8
+ read: true
7
9
  - id: 2
8
10
  title: In Defense of Food
9
11
  author: Michael Pollan
data/test/test_helper.rb CHANGED
@@ -7,6 +7,9 @@ require 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
+
11
+ attribute :rating, :default => 3
12
+ attribute :read, :default => false, :freeze => true
10
13
  end
11
14
 
12
15
  unless defined?(ActiveRecord::Base)
@@ -34,6 +34,32 @@ class TestStaticModel < Test::Unit::TestCase
34
34
  assert_equal 'New Title', @book.title
35
35
  end
36
36
  end
37
+
38
+ context "attribute" do
39
+ should "define methods for the attribute" do
40
+ book = Book[1]
41
+ assert book.respond_to?(:rating)
42
+ assert book.respond_to?(:rating=)
43
+ assert book.respond_to?(:rating?)
44
+ assert_equal 5, book.rating
45
+ end
46
+
47
+ should "return the default if attribute is not set" do
48
+ book = Book[2]
49
+ assert book.respond_to?(:rating)
50
+ assert book.respond_to?(:rating=)
51
+ assert book.respond_to?(:rating?)
52
+ assert_equal 3, book.rating
53
+ end
54
+
55
+ should "freeze the attribute" do
56
+ book = Book[1]
57
+ assert book.read?
58
+ book.read = false
59
+ assert book.read?
60
+ end
61
+
62
+ end
37
63
 
38
64
  context "to_s" do
39
65
  should "inspect" do
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: static_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Aaron Quint
@@ -9,41 +14,53 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-12-14 00:00:00 -05:00
17
+ date: 2010-05-06 00:00:00 -04:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: activesupport
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 2
30
+ - 0
23
31
  version: 2.2.0
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: rubigen
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ">="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 5
44
+ - 1
33
45
  version: 1.5.1
34
- version:
46
+ type: :runtime
47
+ version_requirements: *id002
35
48
  - !ruby/object:Gem::Dependency
36
49
  name: Shoulda
37
- type: :development
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
40
52
  requirements:
41
53
  - - ">="
42
54
  - !ruby/object:Gem::Version
55
+ segments:
56
+ - 1
57
+ - 2
58
+ - 0
43
59
  version: 1.2.0
44
- version:
60
+ type: :development
61
+ version_requirements: *id003
45
62
  description: StaticModel provides a Base class much like ActiveRecord which supports reading from a YAML file and basic associations to ActiveRecord
46
- email:
63
+ email: aaron at quirkey dot com
47
64
  executables: []
48
65
 
49
66
  extensions: []
@@ -85,7 +102,7 @@ files:
85
102
  - test/test_static_model_generator.rb
86
103
  - test/test_static_model_scope.rb
87
104
  has_rdoc: true
88
- homepage:
105
+ homepage: http://github.com/quirkey/static_model
89
106
  licenses: []
90
107
 
91
108
  post_install_message:
@@ -97,18 +114,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
114
  requirements:
98
115
  - - ">="
99
116
  - !ruby/object:Gem::Version
117
+ segments:
118
+ - 0
100
119
  version: "0"
101
- version:
102
120
  required_rubygems_version: !ruby/object:Gem::Requirement
103
121
  requirements:
104
122
  - - ">="
105
123
  - !ruby/object:Gem::Version
124
+ segments:
125
+ - 0
106
126
  version: "0"
107
- version:
108
127
  requirements: []
109
128
 
110
129
  rubyforge_project: quirkey
111
- rubygems_version: 1.3.5
130
+ rubygems_version: 1.3.6
112
131
  signing_key:
113
132
  specification_version: 3
114
133
  summary: ActiveRecord like functionalities for reading from YAML with a simple class implementation