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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ebd460629d89807f4466c496083fd9a0a7be54d5
4
- data.tar.gz: 667ce55093567f11e2d0c4a5f108828d5c3808fa
3
+ metadata.gz: dc853b39e8007eefe46f071df1f509ec3bd82ba7
4
+ data.tar.gz: 75abb4222fda87e473b7532b8ec3ae6f65e19a39
5
5
  SHA512:
6
- metadata.gz: 7414d42602a180112990808d9ce895ad9da82ba6dc19dc9dc69d6967286b986260ae8d9ba8bfaa4c5842d508fc12fdfb902c56567e72ae4f8a1019afc81e137d
7
- data.tar.gz: ac6d7d3831e1854da332e0d6b14e0b2358f17e0a6e4baa0d1b326e995c5afc51ab76e7c257a38389673bb554692ee0600b32cd1610b9abcd48a0ea1dacd9090f
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 spec
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
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.0.0.pre.2 (2017-06-13)
4
+
5
+ * Bug fix for initialization with string attribute keys
6
+
3
7
  ## 1.0.0.pre.1 (2017-06-11)
4
8
 
5
9
  * **Backwards incompatible change**: dropped support for ruby 1.8.7
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
- raw_accessor = define_raw_accessor(accessor)
55
- bang_accessor = define_bang_accessor(accessor)
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
- typed_accessor = "_typed_#{accessor}".to_sym
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
- remove_method(to_safe) if method_defined?(to_safe)
68
- define_method(to_safe) do
69
- begin
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
- remove_method(accessor) if method_defined?(accessor)
78
- define_method(accessor) do
79
- val = send(typed_accessor)
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
- unless val || send(raw_accessor).nil?
82
- val = send(to_safe)
83
- send(typed_mutator, val)
84
- end
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
- val
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
- remove_method("#{accessor}=") if method_defined?("#{accessor}=")
90
- define_method("#{accessor}=") do |val|
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(typed_mutator, send(to_safe))
124
+ send("#{typed_accessor}=", send(safe_accessor))
93
125
  end
94
126
  end
95
127
  end
96
128
 
97
- def property_types
98
- @property_types ||= {}
129
+ def define_public_attribute(name)
130
+ define_public_reader(name)
131
+ define_public_writer(name)
99
132
  end
100
133
 
101
- private
134
+ def raw_accessor(name)
135
+ "raw_#{name}".to_sym
136
+ end
102
137
 
103
- def define_raw_accessor(name)
104
- accessor = name.to_sym
105
- raw_accessor = "raw_#{name}".to_sym
138
+ def define_raw_attribute_reader(name)
139
+ raw_accessor = raw_accessor(name)
106
140
 
107
- mutator = "#{name}=".to_sym
108
- raw_mutator = "raw_#{name}=".to_sym
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) && method_defined?(raw_mutator)
112
- if method_defined?(accessor) && method_defined?(mutator)
113
- alias_method(raw_accessor, 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
- attr_accessor raw_accessor
160
+ attr_writer raw_accessor
117
161
  end
118
162
  end
119
163
  end
164
+ end
120
165
 
121
- raw_accessor
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 define_bang_accessor(property_name)
125
- bang_accessor = "#{property_name}!".to_sym
126
- converter = value_converter(property_types[property_name.to_sym])
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("raw_#{property_name}")
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Modelish
4
- VERSION = '1.0.0-1'
4
+ VERSION = '1.0.0.pre.2'
5
5
  end
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
- it { is_expected.to respond_to(property_name) }
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) { nil }
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.1
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 00:00:00.000000000 Z
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