modelish 1.0.0.pre.1 → 1.0.0.pre.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +11 -1
- data/CHANGELOG.md +4 -0
- data/lib/modelish/base.rb +2 -2
- data/lib/modelish/property_types.rb +98 -48
- data/lib/modelish/version.rb +1 -1
- data/modelish.gemspec +1 -0
- data/spec/support/property_examples.rb +21 -3
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc853b39e8007eefe46f071df1f509ec3bd82ba7
|
4
|
+
data.tar.gz: 75abb4222fda87e473b7532b8ec3ae6f65e19a39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e603be6452f75d8b29b2857748e57b6b75d56fbc5b081657a194a90f9cbc32c1bad233ce81aea9188eecfccd9d90200706a3d80c0c8797bad4ed69a3ea7766f8
|
7
|
+
data.tar.gz: 31112fc64f9002444293ca183c1f453b35ceb94fe12f875cef77001958c2cf4b2fa00aaf86496fb55244b5dc1bcfa40f0351d65bd17f9957293d3b41f228e90b
|
data/.travis.yml
CHANGED
@@ -8,7 +8,17 @@ gemfile:
|
|
8
8
|
- gemfiles/hashie_1.gemfile
|
9
9
|
- gemfiles/hashie_2.gemfile
|
10
10
|
- gemfiles/hashie_3.gemfile
|
11
|
+
dist: trusty
|
12
|
+
sudo: false
|
13
|
+
cache:
|
14
|
+
bundler: true
|
11
15
|
before_install:
|
12
16
|
- gem install bundler
|
13
17
|
script:
|
14
|
-
- bundle exec rspec
|
18
|
+
- bundle exec rspec
|
19
|
+
after_script:
|
20
|
+
- bundle exec codeclimate-test-reporter
|
21
|
+
addons:
|
22
|
+
code_climate:
|
23
|
+
repo_token:
|
24
|
+
secure: "YXAGhdc20mDicXunFasltPCTg3ekBPdyBdsUYml2iRiCdGx4ZjqEORPlg6AixgGnP0D2JHsYMaAPlAKYx3bOULv3lUH1JORTNa76hhaOOzLFAdIMcU2qbLi+8oHudZucojKqC4ppVMTJBS2P1WH+FUfxXCs7HNkUBlqCmmQdZ/g="
|
data/CHANGELOG.md
CHANGED
data/lib/modelish/base.rb
CHANGED
@@ -43,13 +43,13 @@ module Modelish
|
|
43
43
|
def init_attributes(attributes)
|
44
44
|
attributes.delete_if do |k, v|
|
45
45
|
if self.class.translations.keys.include?(k.to_sym)
|
46
|
-
self[k] = v
|
46
|
+
self[k.to_sym] = v
|
47
47
|
true
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
51
|
attributes.each_pair do |att, value|
|
52
|
-
self[att] = value
|
52
|
+
self[att.to_sym] = value
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -45,95 +45,145 @@ module Modelish
|
|
45
45
|
accessor = property_name.to_sym
|
46
46
|
return if property_types[accessor] == property_type
|
47
47
|
|
48
|
-
# TODO: Refactor. This method is getting unwieldy as more
|
49
|
-
# corner cases are discovered. A few well-placed design
|
50
|
-
# refinements should take care of it (now we just need to figure
|
51
|
-
# out what those are.)
|
52
48
|
property_types[accessor] = property_type
|
53
49
|
|
54
|
-
|
55
|
-
|
50
|
+
# Protected attributes used during type conversion
|
51
|
+
define_raw_attribute(accessor)
|
52
|
+
define_bang_attribute(accessor)
|
53
|
+
define_typed_attribute(accessor)
|
54
|
+
define_safe_attribute(accessor)
|
56
55
|
|
57
|
-
|
56
|
+
define_public_attribute(accessor)
|
57
|
+
end
|
58
|
+
|
59
|
+
def property_types
|
60
|
+
@property_types ||= {}
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def typed_accessor(name)
|
66
|
+
"_typed_#{name}".to_sym
|
67
|
+
end
|
68
|
+
|
69
|
+
def define_typed_attribute(name)
|
70
|
+
typed_accessor = typed_accessor(name)
|
58
71
|
typed_mutator = "#{typed_accessor}=".to_sym
|
59
|
-
to_safe = "_to_safe_#{accessor}".to_sym
|
60
72
|
|
61
73
|
class_eval do
|
62
74
|
remove_method(typed_accessor) if method_defined?(typed_accessor)
|
63
75
|
remove_method(typed_mutator) if method_defined?(typed_mutator)
|
76
|
+
|
64
77
|
attr_accessor typed_accessor
|
65
78
|
protected typed_accessor, typed_mutator
|
79
|
+
end
|
80
|
+
end
|
66
81
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
send(bang_accessor)
|
71
|
-
rescue
|
72
|
-
send(raw_accessor)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
protected to_safe
|
82
|
+
def safe_accessor(name)
|
83
|
+
"_to_safe_#{name}".to_sym
|
84
|
+
end
|
76
85
|
|
77
|
-
|
78
|
-
|
79
|
-
|
86
|
+
def define_safe_attribute(name)
|
87
|
+
safe_accessor = safe_accessor(name)
|
88
|
+
bang_accessor = bang_accessor(name)
|
89
|
+
raw_accessor = raw_accessor(name)
|
80
90
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
91
|
+
class_eval do
|
92
|
+
remove_method(safe_accessor) if method_defined?(safe_accessor)
|
93
|
+
define_method(safe_accessor) do
|
94
|
+
# Yes, we really do want to use inline rescue here, as this
|
95
|
+
# method should always try to return *something*
|
96
|
+
send(bang_accessor) rescue send(raw_accessor)
|
97
|
+
end
|
98
|
+
protected safe_accessor
|
99
|
+
end
|
100
|
+
end
|
85
101
|
|
86
|
-
|
102
|
+
def define_public_reader(name)
|
103
|
+
typed_accessor = typed_accessor(name)
|
104
|
+
safe_accessor = safe_accessor(name)
|
105
|
+
|
106
|
+
class_eval do
|
107
|
+
remove_method(name) if method_defined?(name)
|
108
|
+
define_method(name) do
|
109
|
+
send(typed_accessor) ||
|
110
|
+
send("#{typed_accessor}=", send(safe_accessor))
|
87
111
|
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def define_public_writer(name)
|
116
|
+
typed_accessor = typed_accessor(name)
|
117
|
+
safe_accessor = safe_accessor(name)
|
118
|
+
raw_accessor = raw_accessor(name)
|
88
119
|
|
89
|
-
|
90
|
-
|
120
|
+
class_eval do
|
121
|
+
remove_method("#{name}=") if method_defined?("#{name}=")
|
122
|
+
define_method("#{name}=") do |val|
|
91
123
|
send("#{raw_accessor}=", val)
|
92
|
-
send(
|
124
|
+
send("#{typed_accessor}=", send(safe_accessor))
|
93
125
|
end
|
94
126
|
end
|
95
127
|
end
|
96
128
|
|
97
|
-
def
|
98
|
-
|
129
|
+
def define_public_attribute(name)
|
130
|
+
define_public_reader(name)
|
131
|
+
define_public_writer(name)
|
99
132
|
end
|
100
133
|
|
101
|
-
|
134
|
+
def raw_accessor(name)
|
135
|
+
"raw_#{name}".to_sym
|
136
|
+
end
|
102
137
|
|
103
|
-
def
|
104
|
-
|
105
|
-
raw_accessor = "raw_#{name}".to_sym
|
138
|
+
def define_raw_attribute_reader(name)
|
139
|
+
raw_accessor = raw_accessor(name)
|
106
140
|
|
107
|
-
|
108
|
-
|
141
|
+
class_eval do
|
142
|
+
unless method_defined?(raw_accessor)
|
143
|
+
if method_defined?(name)
|
144
|
+
alias_method(raw_accessor, name)
|
145
|
+
else
|
146
|
+
attr_reader raw_accessor
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def define_raw_attribute_writer(name)
|
153
|
+
raw_accessor = raw_accessor(name)
|
109
154
|
|
110
155
|
class_eval do
|
111
|
-
unless method_defined?(raw_accessor)
|
112
|
-
if method_defined?(
|
113
|
-
alias_method(raw_accessor,
|
114
|
-
alias_method(raw_mutator, mutator)
|
156
|
+
unless method_defined?("#{raw_accessor}=")
|
157
|
+
if method_defined?("#{name}=")
|
158
|
+
alias_method("#{raw_accessor}=", "#{name}=")
|
115
159
|
else
|
116
|
-
|
160
|
+
attr_writer raw_accessor
|
117
161
|
end
|
118
162
|
end
|
119
163
|
end
|
164
|
+
end
|
120
165
|
|
121
|
-
|
166
|
+
def define_raw_attribute(name)
|
167
|
+
define_raw_attribute_reader(name)
|
168
|
+
define_raw_attribute_writer(name)
|
122
169
|
end
|
123
170
|
|
124
|
-
def
|
125
|
-
|
126
|
-
|
171
|
+
def bang_accessor(name)
|
172
|
+
"#{name}!".to_sym
|
173
|
+
end
|
174
|
+
|
175
|
+
def define_bang_attribute(name)
|
176
|
+
bang_accessor = bang_accessor(name)
|
177
|
+
raw_accessor = raw_accessor(name)
|
178
|
+
converter = value_converter(property_types[name])
|
127
179
|
|
128
180
|
class_eval do
|
129
181
|
remove_method(bang_accessor) if method_defined?(bang_accessor)
|
130
182
|
define_method(bang_accessor) do
|
131
|
-
value = send(
|
183
|
+
value = send(raw_accessor)
|
132
184
|
converter && value ? converter.call(value) : value
|
133
185
|
end
|
134
186
|
end
|
135
|
-
|
136
|
-
bang_accessor
|
137
187
|
end
|
138
188
|
|
139
189
|
def value_converter(property_type)
|
data/lib/modelish/version.rb
CHANGED
data/modelish.gemspec
CHANGED
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
|
|
23
23
|
s.add_dependency('hashie', '>= 1.0.0')
|
24
24
|
|
25
25
|
s.add_development_dependency('appraisal')
|
26
|
+
s.add_development_dependency('codeclimate-test-reporter')
|
26
27
|
s.add_development_dependency('fuubar')
|
27
28
|
s.add_development_dependency('rspec', '~> 3.6')
|
28
29
|
s.add_development_dependency('rspec-its')
|
@@ -10,18 +10,36 @@ RSpec.shared_examples_for 'a modelish property' do
|
|
10
10
|
|
11
11
|
let(:init_options) { { property_name => property_value } }
|
12
12
|
|
13
|
-
|
13
|
+
context 'when initialized with symbol key' do
|
14
|
+
let(:init_options) { { property_name.to_sym => property_value } }
|
15
|
+
|
16
|
+
it { is_expected.to respond_to(property_name) }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when initialized with a string key' do
|
20
|
+
let(:init_options) { { property_name.to_s => property_value } }
|
21
|
+
|
22
|
+
it { is_expected.to respond_to(property_name) }
|
23
|
+
end
|
14
24
|
|
15
25
|
describe 'getter' do
|
16
26
|
subject { model.send(property_name) }
|
17
27
|
|
18
28
|
context 'without init options' do
|
19
|
-
let(:init_options) {
|
29
|
+
let(:init_options) {}
|
20
30
|
|
21
31
|
it { is_expected.to eq(default_value) }
|
22
32
|
end
|
23
33
|
|
24
|
-
context 'with init options' do
|
34
|
+
context 'with symbol init options' do
|
35
|
+
let(:init_options) { { property_name.to_sym => property_value } }
|
36
|
+
|
37
|
+
it { is_expected.to eq(property_value) }
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'with string init options' do
|
41
|
+
let(:init_options) { { property_name.to_s => property_value } }
|
42
|
+
|
25
43
|
it { is_expected.to eq(property_value) }
|
26
44
|
end
|
27
45
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modelish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.pre.
|
4
|
+
version: 1.0.0.pre.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maeve Revels
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: codeclimate-test-reporter
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: fuubar
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|