deco_lite 0.2.4 → 0.2.5

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: 2d360b0607d72711264a19f807ad4367237f0e5e69efb385728ccbda66e72e05
4
- data.tar.gz: f90e61477cc9dfa1ba8d19070c1490fcccfce413533436ed93b532d4933fad08
3
+ metadata.gz: 74286df21784817e45f4d615081bfeac44d90f13d8200bc8f066107e8d0f773f
4
+ data.tar.gz: 57dcc52147494eb3caf59159cbb21bc23bb6788d0b6315ebc1ddd8cf1868c779
5
5
  SHA512:
6
- metadata.gz: 87d78e241cd8aada8c09c8adbcb20cdb5997e96ff4f17cc8dd3a494e973fd10f13fe73927edb604e2125209119bd881522c79995753768fc0f64d049632d1751
7
- data.tar.gz: d4ae03338d636d82d6a67996cea9e2143a07b0319a9ad6f3a1f1e4a508b2f9a4262ec5cb7e68c8cb2b7d29a8f0e046413ad7f9470f4a20c6fa470c20d9e3c907
6
+ metadata.gz: b79af6a65df899227821e4a02bf17587f1f423042a4280a54525c8469c1c3344f3dd2c3b16bd0de477c711d27ed04527295ca37e811c1c829037379509971ac6
7
+ data.tar.gz: 7d1b5dea80d0af672713d18675166207e2d62ab51aff84045411d02344529a32de513df76d394e3769dfb14a69d0742f18e850398dc9b4cc6bac10eb5c72ecec
data/CHANGELOG.md CHANGED
@@ -1,24 +1,32 @@
1
+ ### 0.2.5
2
+ * Changes
3
+ * Remove init of `@field_names = []` in `Model#initialize` as unnecessary - FieldNamesPersistable takes care of this.
4
+ * Bug fixes
5
+ * Fix but that does not take into account option :namespace when determining whether or not a field name conflicts with an existing attribute (already exists).
6
+
1
7
  ### 0.2.4
2
8
  * Changes
3
9
  * Change DecoLite::Model#load to #load! as it alters the object, give deprecation warning when calling #load.
4
10
  * FieldConflictable now expliticly prohibits loading fields that conflict with attributes that are native to the receiver. In other words, you cannot load fields with names like :to_s, :tap, :hash, etc.
5
11
  * FieldCreatable now creates attr_accessors on the instance using #define_singleton_method, not at the class level (i.e. self.class.attr_accessor) (see bug fixes).
6
- * bug fixes
12
+ * Bug fixes
7
13
  * Fix bug that used self.class.attr_accessor in DecoLite::FieldCreatable to create attributes, which forced every object of that class subsequently created have the accessors created which caused field name conflicts across DecoLite::Model objects.
8
14
 
9
15
  ### 0.2.3
10
- * Fix bug that added duplcate field names to Model#field_names.
16
+ * Bug fixes
17
+ * Fix bug that added duplcate field names to Model#field_names.
11
18
 
12
19
  ### 0.2.2
13
- * Fix bug requiring support codez in lib/deco_lite.rb.
20
+ * Bug fixes
21
+ * Fix bug requiring support codez in lib/deco_lite.rb.
14
22
 
15
23
  ### 0.2.1
16
- * changes
24
+ * Changes
17
25
  * Add mad_flatter gem runtime dependency.
18
26
  * Refactor to let mad_flatter handle the Hash flattening.
19
27
 
20
28
  ### 0.1.1
21
- * changes
29
+ * Changes
22
30
  * Update gems and especially rake gem version to squash CVE-2020-8130, see https://github.com/advisories/GHSA-jppv-gw3r-w3q8.
23
31
  * Fix rubocop violations.
24
32
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- deco_lite (0.2.4)
4
+ deco_lite (0.2.5)
5
5
  activemodel (~> 7.0, >= 7.0.3.1)
6
6
  activesupport (~> 7.0, >= 7.0.3.1)
7
7
  immutable_struct_ex (~> 0.2.0)
@@ -1,43 +1,49 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'field_name_namespaceable'
3
4
  require_relative 'fields_optionable'
4
5
 
5
6
  module DecoLite
6
7
  # Defines methods to to manage fields that conflict with
7
8
  # existing model attributes.
8
9
  module FieldConflictable
10
+ include FieldNameNamespaceable
9
11
  include FieldsOptionable
10
12
 
11
13
  def validate_field_conflicts!(field_name:, options:)
12
14
  return unless field_conflict?(field_name: field_name, options: options)
13
15
 
16
+ field_name = field_name_or_field_name_with_namespace field_name: field_name, options: options
17
+
14
18
  raise "Field :#{field_name} conflicts with existing method(s) " \
15
- ":#{field_name} and/or :#{field_name}=; " \
16
- 'this will raise an error when loading using strict mode ' \
17
- "(i.e. options: { #{OPTION_FIELDS}: :#{OPTION_FIELDS_STRICT} }) " \
18
- 'or if the method(s) are native to the object (e.g :to_s, :==, etc.).'
19
+ ":#{field_name} and/or :#{field_name}=; " \
20
+ 'this will raise an error when loading using strict mode ' \
21
+ "(i.e. options: { #{OPTION_FIELDS}: :#{OPTION_FIELDS_STRICT} }) " \
22
+ 'or if the method(s) are native to the object (e.g :to_s, :==, etc.).'
19
23
  end
20
24
 
21
25
  # This method returns true
22
26
  def field_conflict?(field_name:, options:)
23
27
  # If field_name was already added using Model#load, there is only a
24
28
  # conflict if options.strict? is true.
25
- if field_names_include?(field_name: field_name)
26
- return options.strict?
27
- end
29
+ return options.strict? if field_names_include?(field_name: field_name, options: options)
28
30
 
29
31
  # If we get here, we know that :field_name does not exist as an
30
32
  # attribute on the model. If the attribute already exists on the
31
33
  # model, this is a conflict because we cannot override an attribute
32
34
  # that already exists on the model
33
- attr_accessor_exist?(field_name: field_name)
35
+ attr_accessor_exist?(field_name: field_name, options: options)
34
36
  end
35
37
 
36
- def field_names_include?(field_name:)
38
+ def field_names_include?(field_name:, options:)
39
+ field_name = field_name_or_field_name_with_namespace field_name: field_name, options: options
40
+
37
41
  field_names.include? field_name
38
42
  end
39
43
 
40
- def attr_accessor_exist?(field_name:)
44
+ def attr_accessor_exist?(field_name:, options:)
45
+ field_name = field_name_or_field_name_with_namespace field_name: field_name, options: options
46
+
41
47
  respond_to?(field_name) || respond_to?(:"#{field_name}=")
42
48
  end
43
49
  end
@@ -30,6 +30,7 @@ module DecoLite
30
30
 
31
31
  private
32
32
 
33
+ # rubocop:disable Lint/UnusedMethodArgument
33
34
  def create_field_getter(field_name:, options:)
34
35
  define_singleton_method(field_name) do
35
36
  instance_variable_get "@#{field_name}"
@@ -41,5 +42,6 @@ module DecoLite
41
42
  instance_variable_set "@#{field_name}", value
42
43
  end
43
44
  end
45
+ # rubocop:enable Lint/UnusedMethodArgument
44
46
  end
45
47
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DecoLite
4
+ # Defines methods to transform a field name into a field name
5
+ # with a namespace.
6
+ module FieldNameNamespaceable
7
+ def field_name_or_field_name_with_namespace(field_name:, options:)
8
+ return field_name unless options.namespace?
9
+
10
+ field_name_with_namespace(field_name: field_name, namespace: options.namespace)
11
+ end
12
+
13
+ def field_name_with_namespace(field_name:, namespace:)
14
+ "#{namespace}_#{field_name}"
15
+ end
16
+ end
17
+ end
@@ -3,14 +3,14 @@
3
3
  module DecoLite
4
4
  # Defines methods validate field (attribute) names.
5
5
  module FieldValidatable
6
- FIELD_NAME_REGEX = /\A(?:[a-z_]\w*[?!=]?|\[\]=?|<<|>>|\*\*|[!~+\*\/%&^|-]|[<>]=?|<=>|={2,3}|![=~]|=~)\z/i.freeze
6
+ FIELD_NAME_REGEX = %r{\A(?:[a-z_]\w*[?!=]?|\[\]=?|<<|>>|\*\*|[!~+*/%&^|-]|[<>]=?|<=>|={2,3}|![=~]|=~)\z}i
7
7
 
8
8
  module_function
9
9
 
10
+ # rubocop:disable Lint/UnusedMethodArgument
10
11
  def validate_field_name!(field_name:, options: nil)
11
- unless field_name =~ FIELD_NAME_REGEX
12
- raise "field_name '#{field_name}' is not a valid field name."
13
- end
12
+ raise "field_name '#{field_name}' is not a valid field name." unless FIELD_NAME_REGEX.match?(field_name)
14
13
  end
14
+ # rubocop:enable Lint/UnusedMethodArgument
15
15
  end
16
16
  end
@@ -25,8 +25,6 @@ module DecoLite
25
25
  validate :validate_required_fields
26
26
 
27
27
  def initialize(options: {})
28
- @field_names = []
29
-
30
28
  # Accept whatever options are sent, but make sure
31
29
  # we have defaults set up. #options_with_defaults
32
30
  # will merge options into OptionsDefaultable::DEFAULT_OPTIONS
@@ -49,10 +47,10 @@ module DecoLite
49
47
  end
50
48
 
51
49
  def load(hash:, options: {})
52
- puts 'WARNING: DecoLite::Model#load will be deprecated in a future release;' \
53
- ' use DecoLite::Model#load! instead!'
50
+ puts 'WARNING: DecoLite::Model#load will be deprecated in a future release; ' \
51
+ 'use DecoLite::Model#load! instead!'
54
52
 
55
- load!(hash: hash, options: options)
53
+ load!(hash: hash, options: options)
56
54
  end
57
55
  end
58
56
  end
@@ -35,7 +35,7 @@ module DecoLite
35
35
 
36
36
  raise ArgumentError,
37
37
  "option :fields value or type is invalid. #{OPTION_FIELDS_VALUES} (Symbol) " \
38
- "was expected, but '#{fields}' (#{fields.class}) was received."
38
+ "was expected, but '#{fields}' (#{fields.class}) was received."
39
39
  end
40
40
 
41
41
  def validate_option_namespace!(namespace:)
@@ -43,7 +43,7 @@ module DecoLite
43
43
  return if namespace.blank? || namespace.is_a?(Symbol)
44
44
 
45
45
  raise ArgumentError, 'option :namespace value or type is invalid. A Symbol was expected, ' \
46
- "but '#{namespace}' (#{namespace.class}) was received."
46
+ "but '#{namespace}' (#{namespace.class}) was received."
47
47
  end
48
48
  end
49
49
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the version of this gem.
4
4
  module DecoLite
5
- VERSION = '0.2.4'
5
+ VERSION = '0.2.5'
6
6
  end
data/lib/deco_lite.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative 'deco_lite/field_assignable'
4
4
  require_relative 'deco_lite/field_conflictable'
5
5
  require_relative 'deco_lite/field_creatable'
6
+ require_relative 'deco_lite/field_name_namespaceable'
6
7
  require_relative 'deco_lite/field_names_persistable'
7
8
  require_relative 'deco_lite/field_requireable'
8
9
  require_relative 'deco_lite/field_retrievable'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deco_lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gene M. Angelo, Jr.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-21 00:00:00.000000000 Z
11
+ date: 2022-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -261,6 +261,7 @@ files:
261
261
  - lib/deco_lite/field_assignable.rb
262
262
  - lib/deco_lite/field_conflictable.rb
263
263
  - lib/deco_lite/field_creatable.rb
264
+ - lib/deco_lite/field_name_namespaceable.rb
264
265
  - lib/deco_lite/field_names_persistable.rb
265
266
  - lib/deco_lite/field_requireable.rb
266
267
  - lib/deco_lite/field_retrievable.rb