simple_model 0.2.6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -2,32 +2,38 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  simple_model (0.2.6)
5
- activemodel (~> 3.0.5)
6
- activesupport (~> 3.0.5)
5
+ activemodel (~> 3.0)
6
+ activesupport (~> 3.0)
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
- activemodel (3.0.10)
12
- activesupport (= 3.0.10)
13
- builder (~> 2.1.2)
14
- i18n (~> 0.5.0)
15
- activesupport (3.0.10)
16
- builder (2.1.2)
11
+ ZenTest (4.6.2)
12
+ activemodel (3.1.1)
13
+ activesupport (= 3.1.1)
14
+ builder (~> 3.0.0)
15
+ i18n (~> 0.6)
16
+ activesupport (3.1.1)
17
+ multi_json (~> 1.0)
18
+ autotest (4.4.6)
19
+ ZenTest (>= 4.4.1)
20
+ builder (3.0.0)
17
21
  diff-lcs (1.1.3)
18
- i18n (0.5.0)
19
- rspec (2.6.0)
20
- rspec-core (~> 2.6.0)
21
- rspec-expectations (~> 2.6.0)
22
- rspec-mocks (~> 2.6.0)
23
- rspec-core (2.6.4)
24
- rspec-expectations (2.6.0)
22
+ i18n (0.6.0)
23
+ multi_json (1.0.3)
24
+ rspec (2.7.0)
25
+ rspec-core (~> 2.7.0)
26
+ rspec-expectations (~> 2.7.0)
27
+ rspec-mocks (~> 2.7.0)
28
+ rspec-core (2.7.1)
29
+ rspec-expectations (2.7.0)
25
30
  diff-lcs (~> 1.1.2)
26
- rspec-mocks (2.6.0)
31
+ rspec-mocks (2.7.0)
27
32
 
28
33
  PLATFORMS
29
34
  ruby
30
35
 
31
36
  DEPENDENCIES
37
+ autotest
32
38
  rspec
33
39
  simple_model!
data/README.md CHANGED
@@ -16,15 +16,42 @@ SimpleModel is available through [Rubygems](http://rubygems.org/gems/simple_mode
16
16
  require 'simple_model'
17
17
 
18
18
  class Item < SimpleModel::Base
19
+ save :save_item, :rollback => :undo_save
19
20
  has_booleans :active, :default => true
20
21
  has_booleans :paid
21
22
  has_currency :price, :default => 10.0
22
- has_dates :created_at
23
+ has_times :created_at, :default => :now
23
24
  validates_inclusion_of :price, :in => 10..25
25
+
26
+ def now
27
+ Time.now
28
+ end
29
+
30
+ def file_name
31
+ @file_name ||= "receipt-#{self.created_at.to_i}.txt"
32
+ end
33
+
34
+ def save_item
35
+ begin
36
+ File.open(self.file_name, 'w') do |receipt|
37
+ receipt.puts self.created_at
38
+ receipt.puts "price:#{self.price}"
39
+ receipt.puts "paid:#{self.paid}"
40
+ end
41
+ rescue
42
+ return false
43
+ end
44
+ true
45
+ end
46
+
47
+ def undo_save
48
+ File.delete(file_name)
49
+ end
24
50
  end
25
51
 
26
- item = Item.new(:created_at => "12/30/2010")
27
- item.created_at # => #<Date: 2010-12-30 (4911121/2,0,2299161)>
52
+ item = Item.new
53
+ item.created_at # => 2011-10-23 21:56:07 -0500
54
+ item.created_at # => 2011-10-23 21:56:08 -0500
28
55
  item.active # => true
29
56
  item.paid # => nil
30
57
  item.paid? # => false
@@ -32,6 +59,9 @@ SimpleModel is available through [Rubygems](http://rubygems.org/gems/simple_mode
32
59
  item.price = '$1,024.00'
33
60
  item.price # => #<BigDecimal:100c989d8,'0.1024E4',9(27)>
34
61
  item.valid? # => false
62
+ item.price = 15
63
+ item.save # => true
64
+
35
65
 
36
66
 
37
67
  ## Contributing to simple_model
@@ -6,29 +6,11 @@ module SimpleModel
6
6
  module Attributes
7
7
  include ExtendCore
8
8
 
9
- attr_accessor :id, :saved
10
9
  #Set attribute values to those supplied at initialization
11
10
  def initialize(*attrs)
12
11
  set_attributes(attrs.extract_options!)
13
12
  end
14
13
 
15
- def new_record
16
- @new_record = true if @new_record.nil?
17
- @new_record
18
- end
19
-
20
- def new_record?
21
- new_record
22
- end
23
-
24
- def new_record=(new_record)
25
- @new_record = new_record
26
- end
27
-
28
- def persisted?
29
- saved.to_b
30
- end
31
-
32
14
  # Place to store set attributes and their values
33
15
  def attributes
34
16
  @attributes ||= {}
@@ -40,10 +22,8 @@ module SimpleModel
40
22
  self.send("#{attr[0].to_sym}=",attr[1])
41
23
  end
42
24
  end
43
-
44
25
  alias :update_attributes :set_attributes
45
26
 
46
-
47
27
  def self.included(base)
48
28
  base.extend(ClassMethods)
49
29
  end
@@ -77,7 +57,6 @@ module SimpleModel
77
57
  instance_variable_set("@#{attr}", val.to_s.to_b)
78
58
  attributes[attr] = val
79
59
  val
80
-
81
60
  end
82
61
 
83
62
  define_method ("#{attr.to_s}?") do
@@ -106,7 +85,7 @@ module SimpleModel
106
85
 
107
86
  # Creates setter and getter methods for currency attributes
108
87
  # attributes are cast to BigDecimal and rounded to nearest cent
109
- # Warning, rounding occurs on all sets, so if you need to keep higher prescsion
88
+ # #Warning, rounding occurs on all sets, so if you need to keep higher prescsion
110
89
  # use has_decimals
111
90
  def has_currency(*attrs)
112
91
  options = attrs.extract_options!
@@ -184,7 +163,6 @@ module SimpleModel
184
163
  end
185
164
  end
186
165
 
187
-
188
166
  def fetch_alias_name(attr)
189
167
  alias_name = (attr.to_s << "_old=").to_sym
190
168
  self.module_eval("alias #{alias_name} #{attr}")
@@ -197,8 +175,9 @@ module SimpleModel
197
175
  def define_reader_with_options(attr,options)
198
176
  unless options[:default].blank?
199
177
  define_method (attr.to_s) do
200
- val = instance_variable_get("@#{attr.to_s}")
201
- val = options[:default] if val.nil?
178
+ default = options[:default].is_a?(Symbol) ? self.send(options[:default]) : options[:default]
179
+ val = instance_variable_get("@#{attr.to_s}")
180
+ val = default if val.nil?
202
181
  val
203
182
  end
204
183
  end
@@ -1,5 +1,5 @@
1
- module SimpleModel
2
- # require all that active support we know and love
1
+ module SimpleModel
2
+ # Require all that active support we know and love
3
3
  require 'active_support/time'
4
4
  require 'active_support/core_ext/class/attribute'
5
5
  require 'active_support/core_ext/class/attribute_accessors'
@@ -16,13 +16,127 @@ module SimpleModel
16
16
  require 'active_support/core_ext/object/duplicable'
17
17
  require 'active_support/core_ext/object/blank'
18
18
 
19
+ # == SimpleModel::Base
20
+ #
21
+ # Provides an interface for any class to build tabless models.
22
+ #
23
+ # Implements Validations and Callsbacks from ActiveModel and attribute datatype specific
24
+ # attribute definition with default options
25
+ #
26
+ # == SimpleModel Actions:
27
+ #
28
+ # Model actions provide a tool for making use of Active Model callbacks. Each
29
+ # action creates an instance method representing the action, which calls the
30
+ # method(s) listed as symbolswhen defining the actions. Model actions also accept
31
+ # a rollback option, which is called if the action fails. If you plan to
32
+ # implement SimpleModel's actions make avoid naming you own methods "save", "destory",
33
+ # "create", and "update", as this will override the methods defined by action.
34
+ #
35
+ # Available Actions:
36
+ # # save
37
+ # # update
38
+ # # create
39
+ # # destory
40
+ #
41
+ # ==== Example
42
+ #
43
+ # class MyModel < SimpleModel::Base
44
+ # save :my_save, :rollback => :undo_save
45
+ # update :my_update, :rollback => :undo_update
46
+ # destroy :my_destory, :rollback => :undo_destory
47
+ # end
48
+ #
49
+ # A basic SimpleModel implementation might resemble
50
+ #
51
+ # class MyModel < SimpleModel::Base
52
+ #
53
+ # has_integers :first_int, :second_int, :default => 1
54
+ # has_times :now, :default => :get_now
55
+ #
56
+ # save :save_record, :rollback => :rollback
57
+ #
58
+ # def save_record
59
+ # puts "saved"
60
+ # true
61
+ # end
62
+ #
63
+ # def get_today
64
+ # Time.now
65
+ # end
66
+ #
67
+ # def rollback
68
+ # puts "rolled back"
69
+ # end
70
+ # end
71
+ #
72
+ #
73
+ #
74
+
19
75
  class Base
20
76
  include SimpleModel::Attributes
21
77
  include SimpleModel::ErrorHelpers
22
78
 
23
- #User ActiveModel Resources
79
+ #Use ActiveModel Resources
24
80
  include ActiveModel::Validations
25
81
  include ActiveModel::Conversion
26
82
  extend ActiveModel::Naming
83
+ extend ActiveModel::Callbacks
84
+ include ActiveModel::Validations::Callbacks
85
+ define_model_callbacks :save, :update, :create, :destroy
86
+
87
+ has_boolean :saved
88
+ has_boolean :new_record, :default => true
89
+
90
+ attr_accessor :id
91
+
92
+ def persisted?
93
+ saved?
94
+ end
95
+
96
+
97
+ def self.save(*methods)
98
+ define_model_action(methods,:save)
99
+ end
100
+
101
+ def self.create(*methods)
102
+ define_model_action(methods,:create)
103
+ end
104
+
105
+ def self.update(*methods)
106
+ define_model_action(methods,:update)
107
+ end
108
+
109
+ def self.destroy(*methods)
110
+ define_model_action(methods,:destroy)
111
+ end
112
+
113
+ private
114
+
115
+ def run_model_action(methods,options)
116
+ completed = true
117
+ if self.valid?
118
+ methods.each do |method|
119
+ ran = self.send(method)
120
+ completed = ran unless ran
121
+ end
122
+ if completed
123
+ self.saved = true
124
+ else
125
+ self.send(options[:rollback]) unless options[:rollback].blank?
126
+ end
127
+ else
128
+ completed = false
129
+ end
130
+ completed
131
+ end
132
+
133
+ def self.define_model_action(methods,action)
134
+ options = methods.extract_options!
135
+ define_method(action) do
136
+ self.run_callbacks(action) do
137
+ run_model_action(methods,options)
138
+ end
139
+ end
140
+ end
27
141
  end
28
142
  end
@@ -145,8 +145,14 @@ module SimpleModel
145
145
  :middle_name => (parts * " " unless parts.empty?) }
146
146
  end
147
147
  end
148
+
149
+ BigDecimal.class_eval do
150
+ def to_currency_s(symbol="$")
151
+ self.to_f.to_currency_s(symbol)
152
+ end
153
+ end
154
+
148
155
  Fixnum.class_eval do
149
-
150
156
  #Any value greater than 0 is true
151
157
  def to_b
152
158
  self > 0
@@ -1,3 +1,3 @@
1
1
  module SimpleModel
2
- VERSION = "0.2.6"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/simple_model.rb CHANGED
@@ -1,3 +1,25 @@
1
+ #--
2
+ # Copyright (c) 2010-2011 Joshua Thomas Mckinney
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
1
23
  module SimpleModel
2
24
 
3
25
  #Get those rails goodies
data/simple_model.gemspec CHANGED
@@ -2,7 +2,6 @@
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
3
  require "simple_model/version"
4
4
 
5
-
6
5
  Gem::Specification.new do |s|
7
6
  s.name = "simple_model"
8
7
  s.version = SimpleModel::VERSION
@@ -13,10 +12,11 @@ Gem::Specification.new do |s|
13
12
  s.summary = %q{Simpifies building tableless models or models backed by webservices}
14
13
  s.description = %q{Simpifies building tableless models or models backed by webservices. Create data type specific attributes with default if values.}
15
14
 
16
- s.add_runtime_dependency 'activesupport','~> 3.0.5'
17
- s.add_runtime_dependency 'activemodel',"~> 3.0.5"
15
+ s.add_runtime_dependency 'activesupport','~> 3.0'
16
+ s.add_runtime_dependency 'activemodel','~> 3.0'
18
17
 
19
18
  s.add_development_dependency 'rspec'
19
+ s.add_development_dependency 'autotest'
20
20
 
21
21
  s.rubyforge_project = "simple_model"
22
22
 
@@ -24,7 +24,7 @@ describe SimpleModel::Attributes do
24
24
 
25
25
  end
26
26
  describe SimpleModel::Attributes, 'define_reader_with_options' do
27
- before(:all) do
27
+ before(:each) do
28
28
  class TestDefault
29
29
  include SimpleModel::Attributes
30
30
  attr_accessor :test
@@ -41,6 +41,23 @@ describe SimpleModel::Attributes, 'define_reader_with_options' do
41
41
  default.test = "New"
42
42
  default.test.should eql("New")
43
43
  end
44
+
45
+ context 'default value is a symbol' do
46
+ it "should call the method it describes" do
47
+ class TestDefault
48
+ include SimpleModel::Attributes
49
+ attr_accessor :test
50
+ define_reader_with_options :test, :default => :default_value
51
+ def default_value
52
+ "test"
53
+ end
54
+ end
55
+
56
+ default = TestDefault.new
57
+ default.test.should eql("test")
58
+ end
59
+ end
60
+
44
61
 
45
62
  end
46
63
  describe SimpleModel::Attributes, 'has_booleans' do
@@ -1,6 +1,28 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe SimpleModel do
4
+
5
+ describe "save" do
6
+
7
+ it "should perform the supplied methods" do
8
+ class TestStuff < SimpleModel::Base
9
+ save :test
10
+
11
+ attr_accessor :foo
12
+
13
+ def test
14
+ self.foo = "test"
15
+ return true
16
+ end
17
+ end
18
+
19
+ t = TestStuff.new
20
+ t.save
21
+ t.foo.should eql("test")
22
+ end
23
+ end
24
+
25
+
4
26
  it 'Should add a boolean setter' do
5
27
  class TestStuff < SimpleModel::Base
6
28
  has_booleans :test_boolean
@@ -12,10 +34,10 @@ describe SimpleModel do
12
34
  class TestStuff < SimpleModel::Base
13
35
  has_booleans :test_boolean
14
36
  end
15
- t = TestStuff.new
16
- t.methods.include?(:test_boolean).should be_true
17
- t.test_boolean = true
18
- t.test_boolean.should be_true
37
+ t = TestStuff.new
38
+ t.methods.include?(:test_boolean).should be_true
39
+ t.test_boolean = true
40
+ t.test_boolean.should be_true
19
41
  #a.test.should be_false
20
42
  end
21
43
  it 'Should add a error setter' do
@@ -28,6 +50,71 @@ describe SimpleModel do
28
50
  a.errors.add(:test_attr, "test")
29
51
  a.errors?.should be_true
30
52
  end
53
+
54
+ it 'Should include validation callbacks' do
55
+ class TestStuff < SimpleModel::Base
56
+ end
57
+ TestStuff.respond_to?(:before_validation).should be_true
58
+ TestStuff.respond_to?(:after_save).should be_true
59
+
60
+ end
61
+
62
+ it 'Should include peform validation callbacks' do
63
+ class TestStuff < SimpleModel::Base
64
+ before_validation :set_foo
65
+ after_validation :set_bar
66
+ attr_accessor :foo,:bar
67
+ validates :foo, :presence => true
68
+
69
+
70
+ private
71
+ def set_foo
72
+ self.foo = "foo"
73
+ end
74
+
75
+ def set_bar
76
+ self.bar = "bar"
77
+ end
78
+ end
79
+
80
+ t = TestStuff.new
81
+ t.valid?
82
+ t.foo.should eql('foo')
83
+ t.bar.should eql('bar')
84
+ end
85
+
86
+ it "should run call backs on save" do
87
+ class TestStuff < SimpleModel::Base
88
+ save do
89
+ puts "saved"
90
+ true
91
+ end
92
+ before_save :set_foo
93
+ after_validation :set_bar
94
+ attr_accessor :foo,:bar
95
+ validates :foo, :presence => true
96
+
97
+
98
+ private
99
+
100
+ def my_save_method
101
+ true
102
+ end
103
+
104
+ def set_foo
105
+ self.foo = "foo"
106
+ end
107
+
108
+ def set_bar
109
+ self.bar = "bar"
110
+ end
111
+ end
112
+
113
+ t = TestStuff.new
114
+ t.save
115
+ t.foo.should eql('foo')
116
+ t.bar.should eql('bar')
117
+ end
31
118
  end
32
119
 
33
120
  #describe SimpleModel::Errors do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,33 +9,33 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-15 00:00:00.000000000Z
12
+ date: 2011-10-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70363326271940 !ruby/object:Gem::Requirement
16
+ requirement: &70349578811140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 3.0.5
21
+ version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70363326271940
24
+ version_requirements: *70349578811140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activemodel
27
- requirement: &70363326271380 !ruby/object:Gem::Requirement
27
+ requirement: &70349578810640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: 3.0.5
32
+ version: '3.0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70363326271380
35
+ version_requirements: *70349578810640
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70363326270960 !ruby/object:Gem::Requirement
38
+ requirement: &70349578810260 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,18 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70363326270960
46
+ version_requirements: *70349578810260
47
+ - !ruby/object:Gem::Dependency
48
+ name: autotest
49
+ requirement: &70349578809800 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70349578809800
47
58
  description: Simpifies building tableless models or models backed by webservices.
48
59
  Create data type specific attributes with default if values.
49
60
  email: