options_model 0.0.12 → 0.0.13

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b29481d30d2e37360341fd4b6cf8c3be0c6823c30626025059549d26b4dba5a
4
- data.tar.gz: c1dc86ae7c246c954f9338af71234034c53f150e2c9bdce47df4cf39d9853424
3
+ metadata.gz: 8eaa7e2d11c4dd87d81036b30896555ffcc410b17c8ca79a291f37c33f50f7de
4
+ data.tar.gz: 4db3c76e03c75cd3666b44ad5303528cd237a5e14310806bc5f389284d1f57fc
5
5
  SHA512:
6
- metadata.gz: f7f1c6c8957d032681cc2d06a05847826de7ca5ab656b6b1c5b1e9d08bda49b9087a10eac79207c53920afbd58fbb93dd19f8d07982a0c24b7a3b919cc48ac33
7
- data.tar.gz: 87eb8bc873361551155f0675f0207e9fa9796e8462920d4123dfd060bfbfdae7017e25bc2a2a2fcf2cea38e82dfca0688e7ddb6168ad43e6d3bce25dd25ee141
6
+ metadata.gz: 4dfdd530b69791f07c5d4b6d62f4acb4dfb4145e1e6adae8a2ad438843cdb7b3d8568ce538b80b466f85d1873c663fd34d44b5ab1ad460a6f460e63d2c8516f8
7
+ data.tar.gz: 470b7b027c9a9bac80472fbc3635ebd5f8c150f5121956e6b5d31b3141e890c4a5fc64d36a3c51686f5c25e26f4800e8ae08de809e03d2fdd55a46481afc366e
@@ -10,9 +10,7 @@ module OptionsModel
10
10
  validate do
11
11
  self.class.attribute_names.each do |attribute_name|
12
12
  attribute = public_send(attribute_name)
13
- if attribute.is_a?(self.class) && attribute.invalid?
14
- errors.add attribute_name, :invalid
15
- end
13
+ errors.add attribute_name, :invalid if attribute.is_a?(self.class) && attribute.invalid?
16
14
  end
17
15
  end
18
16
 
@@ -22,14 +20,14 @@ module OptionsModel
22
20
  nested_attributes == other.nested_attributes &&
23
21
  unused_attributes == other.unused_attributes
24
22
  end
25
- alias :eql? :==
23
+ alias eql? ==
26
24
 
27
25
  def hash
28
26
  [attributes, nested_attributes, unused_attributes].hash
29
27
  end
30
28
 
31
29
  def inspect
32
- "#<#{self.class.name}:OptionsModel #{self.to_h}>"
30
+ "#<#{self.class.name}:OptionsModel #{to_h}>"
33
31
  end
34
32
 
35
33
  def self.inspect
@@ -15,12 +15,10 @@ module OptionsModel
15
15
  update_attributes(other)
16
16
  end
17
17
 
18
- def update_attributes(other)
18
+ def update(other)
19
19
  return unless other
20
20
 
21
- unless other.respond_to?(:to_h)
22
- raise ArgumentError, "#{other} must be respond to `to_h`"
23
- end
21
+ raise ArgumentError, "#{other} must be respond to `to_h`" unless other.respond_to?(:to_h)
24
22
 
25
23
  other.to_h.each do |k, v|
26
24
  if respond_to?("#{k}=")
@@ -31,6 +29,8 @@ module OptionsModel
31
29
  end
32
30
  end
33
31
 
32
+ alias update_attributes update
33
+
34
34
  def [](key)
35
35
  public_send(key) if respond_to?(key)
36
36
  end
@@ -45,9 +45,7 @@ module OptionsModel
45
45
  end
46
46
 
47
47
  def fetch(key, default = nil)
48
- if self.class.attribute_names.exclude?(key.to_sym) && default.nil? && !block_given?
49
- raise KeyError, "attribute not found"
50
- end
48
+ raise KeyError, "attribute not found" if self.class.attribute_names.exclude?(key.to_sym) && default.nil? && !block_given?
51
49
 
52
50
  value = respond_to?(key) ? public_send(key) : nil
53
51
  return value if value
@@ -16,10 +16,9 @@ module OptionsModel
16
16
 
17
17
  attribute_defaults[name] = default
18
18
  default_extractor =
19
- case
20
- when default.respond_to?(:call)
19
+ if default.respond_to?(:call)
21
20
  ".call"
22
- when default.duplicable?
21
+ elsif default.duplicable?
23
22
  ".deep_dup"
24
23
  else
25
24
  ""
@@ -55,13 +54,11 @@ module OptionsModel
55
54
  end
56
55
  STR
57
56
 
58
- if cast_type == :boolean
59
- generated_attribute_methods.send :alias_method, :"#{name}?", name
60
- end
57
+ generated_attribute_methods.send :alias_method, :"#{name}?", name if cast_type == :boolean
61
58
  end
62
59
  end
63
60
 
64
- self.attribute_names_for_inlining << name
61
+ attribute_names_for_inlining << name
65
62
 
66
63
  self
67
64
  end
@@ -69,9 +66,8 @@ module OptionsModel
69
66
  def enum_attribute(name, enum, default: nil, allow_nil: false)
70
67
  check_not_finalized!
71
68
 
72
- unless enum.is_a?(Array) && enum.any?
73
- raise ArgumentError, "enum should be an Array and can't empty"
74
- end
69
+ raise ArgumentError, "enum should be an Array and can't empty" unless enum.is_a?(Array) && enum.any?
70
+
75
71
  enum = enum.map(&:to_s)
76
72
 
77
73
  attribute name, :string, default: default
@@ -80,11 +76,11 @@ module OptionsModel
80
76
  generated_class_methods.synchronize do
81
77
  generated_class_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
82
78
  def #{pluralized_name}
83
- %w(#{enum.join(" ")}).freeze
79
+ %w(#{enum.join(' ')}).freeze
84
80
  end
85
81
  STR
86
82
 
87
- validates name, inclusion: {in: enum}, allow_nil: allow_nil
83
+ validates name, inclusion: { in: enum }, allow_nil: allow_nil
88
84
  end
89
85
 
90
86
  self
@@ -93,17 +89,15 @@ module OptionsModel
93
89
  def embeds_one(name, class_name: nil, anonymous_class: nil)
94
90
  check_not_finalized!
95
91
 
96
- if class_name.blank? && anonymous_class.nil?
97
- raise ArgumentError, "must provide at least one of `class_name` or `anonymous_class`"
98
- end
92
+ raise ArgumentError, "must provide at least one of `class_name` or `anonymous_class`" if class_name.blank? && anonymous_class.nil?
99
93
 
100
94
  name = name.to_sym
101
95
  check_name_validity! name
102
96
 
103
- if class_name.present?
104
- nested_classes[name] = class_name.constantize
97
+ nested_classes[name] = if class_name.present?
98
+ class_name.constantize
105
99
  else
106
- nested_classes[name] = anonymous_class
100
+ anonymous_class
107
101
  end
108
102
 
109
103
  generated_attribute_methods.synchronize do
@@ -130,7 +124,7 @@ module OptionsModel
130
124
  STR
131
125
  end
132
126
 
133
- self.attribute_names_for_nesting << name
127
+ attribute_names_for_nesting << name
134
128
 
135
129
  self
136
130
  end
@@ -160,60 +154,56 @@ module OptionsModel
160
154
  end
161
155
 
162
156
  def finalize!(nested = true)
163
- if nested
164
- nested_classes.values.each(&:finalize!)
165
- end
157
+ nested_classes.values.each(&:finalize!) if nested
166
158
 
167
159
  @finalized = true
168
160
  end
169
161
 
170
162
  protected
171
163
 
172
- def check_name_validity!(symbolized_name)
173
- if dangerous_attribute_method?(symbolized_name)
174
- raise ArgumentError, "#{symbolized_name} is defined by #{OptionsModel::Base}. Check to make sure that you don't have an attribute or method with the same name."
175
- end
164
+ def check_name_validity!(symbolized_name)
165
+ if dangerous_attribute_method?(symbolized_name)
166
+ raise ArgumentError, "#{symbolized_name} is defined by #{OptionsModel::Base}. Check to make sure that you don't have an attribute or method with the same name."
167
+ end
176
168
 
177
- if attribute_names_for_inlining.include?(symbolized_name) || attribute_names_for_nesting.include?(symbolized_name)
178
- raise ArgumentError, "duplicate define attribute `#{symbolized_name}`"
169
+ if attribute_names_for_inlining.include?(symbolized_name) || attribute_names_for_nesting.include?(symbolized_name)
170
+ raise ArgumentError, "duplicate define attribute `#{symbolized_name}`"
171
+ end
179
172
  end
180
- end
181
173
 
182
- def check_not_finalized!
183
- if finalized?
184
- raise "can't modify finalized #{self}"
174
+ def check_not_finalized!
175
+ raise "can't modify finalized #{self}" if finalized?
185
176
  end
186
- end
187
177
 
188
- # A method name is 'dangerous' if it is already (re)defined by OptionsModel, but
189
- # not by any ancestors. (So 'puts' is not dangerous but 'save' is.)
190
- def dangerous_attribute_method?(name) # :nodoc:
191
- method_defined_within?(name, OptionsModel::Base)
192
- end
178
+ # A method name is 'dangerous' if it is already (re)defined by OptionsModel, but
179
+ # not by any ancestors. (So 'puts' is not dangerous but 'save' is.)
180
+ def dangerous_attribute_method?(name) # :nodoc:
181
+ method_defined_within?(name, OptionsModel::Base)
182
+ end
193
183
 
194
- def method_defined_within?(name, klass, superklass = klass.superclass) # :nodoc:
195
- if klass.method_defined?(name) || klass.private_method_defined?(name)
196
- if superklass.method_defined?(name) || superklass.private_method_defined?(name)
197
- klass.instance_method(name).owner != superklass.instance_method(name).owner
184
+ def method_defined_within?(name, klass, superklass = klass.superclass) # :nodoc:
185
+ if klass.method_defined?(name) || klass.private_method_defined?(name)
186
+ if superklass.method_defined?(name) || superklass.private_method_defined?(name)
187
+ klass.instance_method(name).owner != superklass.instance_method(name).owner
188
+ else
189
+ true
190
+ end
198
191
  else
199
- true
192
+ false
200
193
  end
201
- else
202
- false
203
194
  end
204
- end
205
195
 
206
- def generated_attribute_methods
207
- @generated_attribute_methods ||= Module.new {
208
- extend Mutex_m
209
- }.tap { |mod| include mod }
210
- end
196
+ def generated_attribute_methods
197
+ @generated_attribute_methods ||= Module.new do
198
+ extend Mutex_m
199
+ end.tap { |mod| include mod }
200
+ end
211
201
 
212
- def generated_class_methods
213
- @generated_class_methods ||= Module.new {
214
- extend Mutex_m
215
- }.tap { |mod| extend mod }
216
- end
202
+ def generated_class_methods
203
+ @generated_class_methods ||= Module.new do
204
+ extend Mutex_m
205
+ end.tap { |mod| extend mod }
206
+ end
217
207
  end
218
208
  end
219
209
  end
@@ -35,7 +35,7 @@ module OptionsModel
35
35
  return new unless yaml
36
36
  return new unless yaml.is_a?(String) && /^---/.match?(yaml)
37
37
 
38
- hash = YAML.load(yaml) || Hash.new
38
+ hash = YAML.safe_load(yaml) || {}
39
39
 
40
40
  unless hash.is_a? Hash
41
41
  raise ArgumentError,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OptionsModel
4
- VERSION = "0.0.12"
4
+ VERSION = "0.0.13"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: options_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - jasl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-11 00:00:00.000000000 Z
11
+ date: 2019-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel