modelish 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +5 -0
- data/lib/modelish/property_types.rb +38 -29
- data/lib/modelish/version.rb +1 -1
- data/lib/modelish.rb +2 -2
- data/spec/modelish/property_types_spec.rb +34 -0
- data/spec/support/typed_property_examples.rb +5 -1
- metadata +4 -4
data/CHANGELOG.md
CHANGED
@@ -38,38 +38,45 @@ module Modelish
|
|
38
38
|
# passing in the raw value as an argument
|
39
39
|
def add_property_type(property_name, property_type=String)
|
40
40
|
accessor = property_name.to_sym
|
41
|
-
property_types[accessor] = property_type
|
42
41
|
|
43
|
-
|
44
|
-
|
42
|
+
# TODO: Refactor. This method is getting unwieldy as more
|
43
|
+
# corner cases are discovered. A few well-placed design
|
44
|
+
# refinements should take care of it (now we just need to figure
|
45
|
+
# out what those are.)
|
46
|
+
unless property_types[accessor] == property_type
|
47
|
+
property_types[accessor] = property_type
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
to_safe = "_to_safe_#{accessor}".to_sym
|
49
|
+
raw_accessor = define_raw_accessor(accessor)
|
50
|
+
bang_accessor = define_bang_accessor(accessor)
|
49
51
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
define_method(to_safe) do
|
55
|
-
self.send(bang_accessor) rescue self.send(raw_accessor)
|
56
|
-
end
|
57
|
-
private to_safe
|
52
|
+
typed_accessor = "_typed_#{accessor}".to_sym
|
53
|
+
typed_mutator = "#{typed_accessor}=".to_sym
|
54
|
+
to_safe = "_to_safe_#{accessor}".to_sym
|
58
55
|
|
59
|
-
|
60
|
-
|
56
|
+
class_eval do
|
57
|
+
attr_accessor typed_accessor
|
58
|
+
private typed_accessor, typed_mutator
|
61
59
|
|
62
|
-
|
63
|
-
|
64
|
-
self.send(typed_mutator, val)
|
60
|
+
define_method(to_safe) do
|
61
|
+
self.send(bang_accessor) rescue self.send(raw_accessor)
|
65
62
|
end
|
63
|
+
private to_safe
|
66
64
|
|
67
|
-
|
68
|
-
|
65
|
+
define_method(accessor) do
|
66
|
+
val = self.send(typed_accessor)
|
67
|
+
|
68
|
+
unless val || self.send(raw_accessor).nil?
|
69
|
+
val = self.send(to_safe)
|
70
|
+
self.send(typed_mutator, val)
|
71
|
+
end
|
69
72
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
+
val
|
74
|
+
end
|
75
|
+
|
76
|
+
define_method("#{accessor}=") do |val|
|
77
|
+
self.send("#{raw_accessor}=", val)
|
78
|
+
self.send(typed_mutator, self.send(to_safe))
|
79
|
+
end
|
73
80
|
end
|
74
81
|
end
|
75
82
|
end
|
@@ -87,11 +94,13 @@ module Modelish
|
|
87
94
|
raw_mutator = "raw_#{name}=".to_sym
|
88
95
|
|
89
96
|
class_eval do
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
97
|
+
unless method_defined?(raw_accessor) && method_defined?(raw_mutator)
|
98
|
+
if method_defined?(accessor) && method_defined?(mutator)
|
99
|
+
alias_method(raw_accessor, accessor)
|
100
|
+
alias_method(raw_mutator, mutator)
|
101
|
+
else
|
102
|
+
attr_accessor raw_accessor
|
103
|
+
end
|
95
104
|
end
|
96
105
|
end
|
97
106
|
|
data/lib/modelish/version.rb
CHANGED
data/lib/modelish.rb
CHANGED
@@ -134,5 +134,39 @@ describe Modelish::PropertyTypes do
|
|
134
134
|
let(:invalid_value) { Object.new }
|
135
135
|
end
|
136
136
|
end
|
137
|
+
|
138
|
+
context "when a property is defined more than once" do
|
139
|
+
before { model_class.add_property_type(property_name, other_property_type) }
|
140
|
+
|
141
|
+
let(:property_type) { Integer }
|
142
|
+
|
143
|
+
context "with the same property_type" do
|
144
|
+
let(:other_property_type) { property_type }
|
145
|
+
|
146
|
+
it "should not raise an error when the property is accessed" do
|
147
|
+
expect { model.send(property_name) }.to_not raise_error
|
148
|
+
end
|
149
|
+
|
150
|
+
it_should_behave_like 'a typed property', :my_property, Integer do
|
151
|
+
let(:valid_string) { '112358' }
|
152
|
+
let(:valid_typed_value) { 112358 }
|
153
|
+
let(:invalid_value) { 'blah' }
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
context "with different property_types" do
|
158
|
+
let(:other_property_type) { Float }
|
159
|
+
|
160
|
+
it "should not raise an error when the property is accessed" do
|
161
|
+
expect { model.send(property_name) }.to_not raise_error
|
162
|
+
end
|
163
|
+
|
164
|
+
it_should_behave_like 'a typed property', :my_property, Float do
|
165
|
+
let(:valid_string) { '112358.13' }
|
166
|
+
let(:valid_typed_value) { 112358.13 }
|
167
|
+
let(:invalid_value) { 'blah' }
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
137
171
|
end
|
138
172
|
end
|
@@ -29,7 +29,11 @@ shared_examples_for "a typed property" do |prop_name, prop_type|
|
|
29
29
|
it { should be }
|
30
30
|
it { should have_key(prop_name) }
|
31
31
|
|
32
|
-
|
32
|
+
if prop_type
|
33
|
+
its([prop_name]) { should == prop_type }
|
34
|
+
else
|
35
|
+
its([prop_name]) { should be_a Proc }
|
36
|
+
end
|
33
37
|
end
|
34
38
|
|
35
39
|
describe "assignment" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modelish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Maeve Revels
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-31 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|