simple_model 1.2.12 → 1.2.13
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -0
- data/LICENSE.txt +1 -1
- data/README.md +1 -1
- data/lib/simple_model.rb +0 -1
- data/lib/simple_model/attributes.rb +7 -8
- data/lib/simple_model/base.rb +3 -3
- data/lib/simple_model/error_helpers.rb +0 -1
- data/lib/simple_model/validation.rb +0 -1
- data/lib/simple_model/version.rb +1 -1
- data/spec/attributes_spec.rb +10 -5
- data/spec/extend_core_spec.rb +3 -3
- data/spec/simple_model_spec.rb +3 -3
- metadata +2 -2
data/.travis.yml
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -10,7 +10,7 @@ SimpleModel implements:
|
|
10
10
|
* ActiveModel::Dirty
|
11
11
|
* ActiveModel::Naming
|
12
12
|
* ActiveModel::Callbacks
|
13
|
-
* ActiveSupport core
|
13
|
+
* ActiveSupport core extensions (see [SimpleModel::Base](https://github.com/JoshMcKin/simple_model/blob/master/lib/simple_model/base.rb))
|
14
14
|
|
15
15
|
Additionally SimpleModel implements basic model actions
|
16
16
|
|
data/lib/simple_model.rb
CHANGED
@@ -46,7 +46,7 @@ module SimpleModel
|
|
46
46
|
arg
|
47
47
|
end
|
48
48
|
|
49
|
-
# Returns attribute that have defaults in a hash: {:
|
49
|
+
# Returns attribute that have defaults in a hash: {:attribute => "default value"}
|
50
50
|
# Checks for alias attributes to ensure they are not overwritten
|
51
51
|
def attributes_with_for_init(attrs)
|
52
52
|
d = attrs.with_indifferent_access
|
@@ -86,7 +86,7 @@ module SimpleModel
|
|
86
86
|
b
|
87
87
|
end
|
88
88
|
|
89
|
-
# Rails 3.2 + required when searching for attributes in from inherited classes/
|
89
|
+
# Rails 3.2 + required when searching for attributes in from inherited classes/models
|
90
90
|
def attribute(name)
|
91
91
|
attributes[name.to_sym]
|
92
92
|
end
|
@@ -94,7 +94,7 @@ module SimpleModel
|
|
94
94
|
module ClassMethods
|
95
95
|
# Creates a new instance where the attributes store is set to object
|
96
96
|
# provided, which allows one to pass a session store hash or any other
|
97
|
-
# hash-like object to be used for
|
97
|
+
# hash-like object to be used for persistence. Typically used for modeling
|
98
98
|
# session stores for authorization or shopping carts
|
99
99
|
# EX:
|
100
100
|
# class ApplicationController < ActionController::Base
|
@@ -138,7 +138,7 @@ module SimpleModel
|
|
138
138
|
# * :on_get - accepts a lambda that is run when you get/read an attribute
|
139
139
|
# * :default - the default value for the attribute, can be a symbol that is sent for a method
|
140
140
|
# * :initialize - informations the object whether or not it should initialize the attribute with :default value, defaults to true
|
141
|
-
# ** If :
|
141
|
+
# ** If :initialize is set to false you must set :allow_blank to false or it will never set the default value
|
142
142
|
# * :allow_blank - when set to false, if an attributes value is blank attempts to set the default value, defaults to true
|
143
143
|
def default_attribute_settings
|
144
144
|
@default_attribute_settings ||= {:attributes_method => :attributes,
|
@@ -262,25 +262,24 @@ module SimpleModel
|
|
262
262
|
|
263
263
|
# A hook to perform actions after all attributes have been initialized
|
264
264
|
# Expects an lambda that accept the object and the pending attributes hash
|
265
|
-
# EX: lambda{|obj| puts "initialized"}
|
265
|
+
# EX: lambda {|obj| puts "initialized"}
|
266
266
|
def after_initialize
|
267
267
|
@after_initialize
|
268
268
|
end
|
269
269
|
|
270
270
|
# Expects an lambda that accept the object and the pending attributes hash
|
271
|
-
# EX: lambda{|obj| puts "initialized"}
|
271
|
+
# EX: lambda {|obj| puts "initialized"}
|
272
272
|
def after_initialize=after_initialize
|
273
273
|
raise TypeError "after_initalize must be a Proc" unless after_initialize.is_a?(Proc)
|
274
274
|
@after_initialize = after_initialize
|
275
275
|
end
|
276
276
|
|
277
|
-
|
278
|
-
|
279
277
|
# Must inherit super's defined_attributes and alias_attributes
|
280
278
|
# Rails 3.0 does some weird stuff with ActiveModel::Dirty so we need a
|
281
279
|
# hack to keep things working when a class inherits from a super that
|
282
280
|
# has ActiveModel::Dirty included
|
283
281
|
def inherited(base)
|
282
|
+
base.alias_attributes = base.alias_attributes.merge(self.alias_attributes)
|
284
283
|
super
|
285
284
|
# Rails 3.0 Hack
|
286
285
|
if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
|
data/lib/simple_model/base.rb
CHANGED
@@ -18,9 +18,9 @@ require 'active_support/core_ext/object/blank'
|
|
18
18
|
|
19
19
|
# == SimpleModel::Base
|
20
20
|
#
|
21
|
-
# Provides an interface for any class to build
|
21
|
+
# Provides an interface for any class to build table-less models.
|
22
22
|
#
|
23
|
-
# Implements Validations, Callbacks and Dirty from ActiveModel, and
|
23
|
+
# Implements Validations, Callbacks and Dirty from ActiveModel, and data-type specific
|
24
24
|
# attribute definitions with default options. SimpleModel::Base is intended as
|
25
25
|
# an example, while it may be used in production, which it is on many of my apps
|
26
26
|
# today, it is recommend you use SimpleModel::Base as an example to implement your
|
@@ -39,7 +39,7 @@ require 'active_support/core_ext/object/blank'
|
|
39
39
|
# # save
|
40
40
|
# # update
|
41
41
|
# # create
|
42
|
-
# #
|
42
|
+
# # destroy
|
43
43
|
#
|
44
44
|
# ==== Example
|
45
45
|
#
|
data/lib/simple_model/version.rb
CHANGED
data/spec/attributes_spec.rb
CHANGED
@@ -24,7 +24,7 @@ describe SimpleModel::Attributes do
|
|
24
24
|
before(:all) do
|
25
25
|
class TestInit
|
26
26
|
include SimpleModel::Attributes
|
27
|
-
# Do not
|
27
|
+
# Do not initialize blank attributes
|
28
28
|
self.before_initialize = lambda {|obj,attrs| attrs.select{|k,v| !v.blank?}}
|
29
29
|
has_attribute :far
|
30
30
|
end
|
@@ -47,7 +47,7 @@ describe SimpleModel::Attributes do
|
|
47
47
|
before(:all) do
|
48
48
|
class TestInit
|
49
49
|
include SimpleModel::Attributes
|
50
|
-
# Do not
|
50
|
+
# Do not initialize blank attributes
|
51
51
|
self.after_initialize = lambda { |obj| obj.car = "test" if obj.car.blank?}
|
52
52
|
has_attribute :car
|
53
53
|
end
|
@@ -136,7 +136,7 @@ describe SimpleModel::Attributes do
|
|
136
136
|
@default.attributes[:bar].should eql("bar")
|
137
137
|
end
|
138
138
|
|
139
|
-
it "should set the
|
139
|
+
it "should set the default to the supplied symbol, if the method does not exist" do
|
140
140
|
@default.attributes[:fab].should eql(:some_symbol)
|
141
141
|
end
|
142
142
|
|
@@ -234,7 +234,7 @@ describe SimpleModel::Attributes do
|
|
234
234
|
has_boolean :bar
|
235
235
|
has_attribute :str
|
236
236
|
has_dates :some, :thing, :default => :fetch_date, :allow_blank => false, :initialize => false
|
237
|
-
|
237
|
+
alias_attribute :other, :bar
|
238
238
|
def fetch_date
|
239
239
|
Date.today
|
240
240
|
end
|
@@ -245,7 +245,7 @@ describe SimpleModel::Attributes do
|
|
245
245
|
has_int :str
|
246
246
|
end
|
247
247
|
end
|
248
|
-
it "should merge defined attributes when class are
|
248
|
+
it "should merge defined attributes when class are inherited" do
|
249
249
|
NewerBase.attribute_defined?(:bar).should be_true
|
250
250
|
n = NewerBase.new
|
251
251
|
n.respond_to?(:bar_will_change!).should be_true
|
@@ -264,6 +264,11 @@ describe SimpleModel::Attributes do
|
|
264
264
|
n.str = '1'
|
265
265
|
n.str.should eql(1)
|
266
266
|
end
|
267
|
+
|
268
|
+
it "should set attribute from alias" do
|
269
|
+
MyBase.new(:other => true).bar?.should be_true
|
270
|
+
NewerBase.new(:other => true).bar?.should be_true
|
271
|
+
end
|
267
272
|
end
|
268
273
|
|
269
274
|
after(:all) do
|
data/spec/extend_core_spec.rb
CHANGED
@@ -59,7 +59,7 @@ describe SimpleModel::ExtendCore, 'String.rb' do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
context '#to_date' do
|
62
|
-
it "should
|
62
|
+
it "should handle US formatted date strings" do
|
63
63
|
lambda {"12/31/2010".to_date}.should_not raise_error
|
64
64
|
"12/31/2010".to_date.class.should eql(Date)
|
65
65
|
"12/31/2010".to_date.should eql(Date.parse("2010-12-31"))
|
@@ -72,7 +72,7 @@ describe SimpleModel::ExtendCore, 'String.rb' do
|
|
72
72
|
end
|
73
73
|
|
74
74
|
context '#to_time' do
|
75
|
-
it "should
|
75
|
+
it "should handle US formatted date strings" do
|
76
76
|
lambda {"12/31/2010 12:00:00".to_time}.should_not raise_error
|
77
77
|
"12/31/2010 12:00:00".to_time.should be_kind_of(Time)
|
78
78
|
"12/31/2010 12:00:00".to_time.should eql(Time.parse("2010-12-31 12:00:00"))
|
@@ -84,7 +84,7 @@ describe SimpleModel::ExtendCore, 'String.rb' do
|
|
84
84
|
end
|
85
85
|
|
86
86
|
context '#to_f' do
|
87
|
-
it "return a
|
87
|
+
it "return a Float from a string that may contain non-numeric values" do
|
88
88
|
"$5,000.006".to_f.should eql(5000.006)
|
89
89
|
end
|
90
90
|
end
|
data/spec/simple_model_spec.rb
CHANGED
@@ -125,7 +125,7 @@ describe SimpleModel do
|
|
125
125
|
|
126
126
|
end
|
127
127
|
|
128
|
-
it 'Should
|
128
|
+
it 'Should perform validation callbacks' do
|
129
129
|
class TestStuff < SimpleModel::Base
|
130
130
|
before_validation :set_foo
|
131
131
|
after_validation :set_bar
|
@@ -217,11 +217,11 @@ describe SimpleModel do
|
|
217
217
|
end
|
218
218
|
|
219
219
|
end
|
220
|
-
it "should merge defined attributes when class are
|
220
|
+
it "should merge defined attributes when class are inherited" do
|
221
221
|
NewTestStuff.attribute_defined?(:bar).blank?.should be_false
|
222
222
|
NewTestStuff.attribute_defined?(:foo).blank?.should be_false
|
223
223
|
end
|
224
|
-
it "should merge defined attributes when class are
|
224
|
+
it "should merge defined attributes when class are inherited" do
|
225
225
|
TestStuff.new.respond_to?(:bar_will_change!).should be_true
|
226
226
|
t = OtherStuff.new
|
227
227
|
t.bar = [1,2,4]
|
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: 1.2.
|
4
|
+
version: 1.2.13
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|