deco_lite 0.3.1 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a87d5a79f738b49d45b51c0128b4c252853c683363e7525df836fc7c46e4953
4
- data.tar.gz: 1f07c897d0f88cd3609a8e6a880d0ce91b71210ee8952eb29148370b2f49f092
3
+ metadata.gz: b5b5ed040bd367962dd57760a6a0e572b60485ba1d2d3630980ff1a090ca9075
4
+ data.tar.gz: 76402762f17d93fd53495ddabf86487803b0014971db007f61f505727e933f56
5
5
  SHA512:
6
- metadata.gz: afc6320e0a1e9060dda231249fe16e62bf62d56f11ad7ffa9c4c6a0f094ef866173655c4ee57a419b3c4b83d0d7b7639230db22459f3c2d81d78cc6c4da2dde2
7
- data.tar.gz: 6db8e3cb9a634669667eb288f4a4343106ad9be71630c1a9e95af366649b86e88459d0b79f1c71e09345a75805e643aa1e1e3a2836e2045bd1191bd78484685c
6
+ metadata.gz: 584e1c07a160370bc9548e7a41dbaa9adcbee757a35a37925d02accf4ebe71d24cd9d2932e706724f5d8d64e8d98ff77f223224de27e5a0d817eb2675fea81ca
7
+ data.tar.gz: 6a49a3ab8bdcb947341697577e130dd667c508095578536e8d003cfa1424f8e090f3c40e9ddc1376a3337cdcc9a26a02e836bc901c0ae36da39a8acaaae8cc46
data/.gitignore CHANGED
@@ -16,5 +16,5 @@
16
16
  /.vscode/
17
17
  *.code-workspace
18
18
 
19
- scratch.rb
19
+ scratch*.rb
20
20
  readme.txt
data/.reek.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  exclude_paths:
2
2
  - vendor
3
3
  - spec
4
- - scratch.rb
4
+ - scratch*.rb
5
5
  detectors:
6
6
  # TooManyInstanceVariables:
7
7
  # exclude:
data/.rubocop.yml CHANGED
@@ -13,7 +13,7 @@ AllCops:
13
13
  - '*.gemspec'
14
14
  - 'spec/**/*'
15
15
  - 'vendor/**/*'
16
- - 'scratch.rb'
16
+ - 'scratch*.rb'
17
17
 
18
18
  # Align the elements of a hash literal if they span more than one line.
19
19
  Layout/HashAlignment:
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ### 0.3.2
2
+ * Changes
3
+ * Refactor FieldAssignable to remove call to FieldCreatable#create_field_accessor as this breaks single responsibility rule; which, in this case, makes sense to remove. FieldCreatable#create_field_accessor can be called wherever creation of a attr_accessor is needed.
4
+ * Refactor specs in keeping with above changes.
5
+ * README.md changes.
6
+ * Bugs
7
+ * Fix bug where loading fields with the options: { fields: :strict } option raises an error for field that already exists.
8
+
1
9
  ### 0.3.1
2
10
  * Changes
3
11
  * Added `DecoLite::FieldRequireable::MISSING_REQUIRED_FIELD_ERROR_TYPE` for required field type errors.
data/README.md CHANGED
@@ -13,13 +13,13 @@
13
13
 
14
14
  ## Introduction
15
15
 
16
- DecoLite is in development. I wouldn't expect breaking changes before v1.0.0; however, I can't completely rule this out. Currently, DecoLite only supports Hashes whose keys are `Symbols`, contain no embedded spaces, and conform to Ruby `attr_accessor` naming conventions. However, I'll certainly work out a solution for all this in future releases.
16
+ DecoLite is in development. I wouldn't expect breaking changes before v1.0.0; however, I can't completely rule this out. Currently, DecoLite only supports Hashes whose keys are `Symbols`, contain no embedded spaces, and conform to Ruby `attr_accessor` naming conventions. However, I might try work out a reasonable solution for all this in future releases if the need is there.
17
17
 
18
- TBD: Documentation regarding `DecoLite::Model` options, `DecoLite::Model#load!` options: how these work, and how they play together (in the meantime, see the specs).
18
+ TBD: Documentation regarding `DecoLite::Model` options, `DecoLite::Model#load!` options: how these work, and how they play together (e.g. options `fields: :merge` and `fields: :strict` for example; in the meantime, see the specs).
19
19
 
20
20
  _Deco_ is a little gem that allows you to use the provided `DecoLite::Model` class (`include ActiveModel::Model`) to create Decorator classes which can be instantiated and used. Inherit from `DecoLite::Model` to create your own unique classes with custom functionality. A `DecoLite::Model` includes `ActiveModel::Model`, so validation can be applied using [ActiveModel validation helpers](https://api.rubyonrails.org/v6.1.3/classes/ActiveModel/Validations/HelperMethods.html) you are familiar with; or, you can roll your own - just like any other ActiveModel.
21
21
 
22
- A `DecoLite::Model` will allow you to consume a Ruby Hash that you supply via the `DecoLite::Model#load!` method. Your supplied Ruby Hashes are used to create `attr_accessor` attributes (_"fields"_) on the model. Each attribute created, is then assigned its value from the Hash loaded.
22
+ A `DecoLite::Model` will allow you to consume a Ruby Hash that you supply via the initializer (`DecoLite::Model#new`) or via the `DecoLite::Model#load!` method. Your supplied Ruby Hashes are used to create `attr_accessor` attributes (_"fields"_) on the model. Each attribute created, is then assigned its value from the Hash loaded.
23
23
 
24
24
  `attr_accessor` names created are _mangled_ to include namespacing. This creates unique attribute names for nested Hashes that may include non-unique keys. For example:
25
25
 
@@ -34,10 +34,11 @@ family = {
34
34
  }
35
35
  }
36
36
  ```
37
- Given the above example, DecoLite will produce the following `attr_accessors` on the `DecoLite::Model` object when loaded (`DecoLite::Model#load!`), and assign the values:
37
+ Given the above example, DecoLite will produce the following `attr_accessors` on the `DecoLite::Model` object when loaded, and assign the values:
38
38
 
39
39
  ```ruby
40
- model = DecoLite::Model.new.load!(hash: family)
40
+ # Or DecoLite::Model.new.load!(hash: family)
41
+ model = DecoLite::Model.new(hash: family)
41
42
 
42
43
  model.name #=> 'John Doe'
43
44
  model.respond_to? :name= #=> true
@@ -59,7 +60,7 @@ grandpa = {
59
60
  name: 'Henry Doe',
60
61
  age: 85,
61
62
  }
62
- # The :name and :age Hash keys above will produce :name/:name= and :age/:age= attr_accessors and clash because these were already added to the model when "John Doe" was loaded with the first call to DecoLite::Model#load!.
63
+ # The :name and :age Hash keys above will produce :name/:name= and :age/:age= attr_accessors and clash because these were already added to the model when "John Doe" was loaded with the first call to DecoLite::Model.new(hash: family).
63
64
  ```
64
65
 
65
66
  However, passing a `namespace:` option (for example `namespace: :grandpa`) to the `DecoLite::Model#load!` method, would produce the following `attr_accessors`, ensuring their uniqueness:
@@ -108,9 +109,7 @@ class ViewModel < DecoLite::Model
108
109
  end
109
110
  end
110
111
 
111
- view_model = ViewModel.new
112
-
113
- view_model.load!(hash: { first: 'John', last: 'Doe' })
112
+ view_model = ViewModel.new(hash: { first: 'John', last: 'Doe' })
114
113
 
115
114
  view_model.valid?
116
115
  #=> true
@@ -1,12 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'field_creatable'
4
3
  require_relative 'field_retrievable'
5
4
 
6
5
  module DecoLite
7
6
  # Defines methods to assign model field values dynamically.
8
7
  module FieldAssignable
9
- include FieldCreatable
10
8
  include FieldRetrievable
11
9
 
12
10
  def set_field_values(hash:, field_info:, options:)
@@ -16,10 +14,10 @@ module DecoLite
16
14
  end
17
15
  end
18
16
 
17
+ # rubocop:disable Lint/UnusedMethodArgument
19
18
  def set_field_value(field_name:, value:, options:)
20
- # Create our fields before we send.
21
- create_field_accessor field_name: field_name, options: options
22
19
  send("#{field_name}=", value)
23
20
  end
21
+ # rubocop:enable Lint/UnusedMethodArgument
24
22
  end
25
23
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'field_name_namespaceable'
4
+ require_relative 'field_requireable'
4
5
  require_relative 'fields_optionable'
5
6
 
6
7
  module DecoLite
@@ -9,6 +10,7 @@ module DecoLite
9
10
  module FieldConflictable
10
11
  include FieldNameNamespaceable
11
12
  include FieldsOptionable
13
+ include FieldRequireable
12
14
 
13
15
  def validate_field_conflicts!(field_name:, options:)
14
16
  return unless field_conflict?(field_name: field_name, options: options)
@@ -2,11 +2,13 @@
2
2
 
3
3
  require 'mad_flatter'
4
4
  require_relative 'field_assignable'
5
+ require_relative 'field_creatable'
5
6
 
6
7
  module DecoLite
7
8
  # Provides methods to load and return information about a given hash.
8
9
  module HashLoadable
9
10
  include FieldAssignable
11
+ include FieldCreatable
10
12
 
11
13
  private
12
14
 
@@ -5,7 +5,11 @@ module DecoLite
5
5
  module Hashable
6
6
  def to_h
7
7
  field_names.each_with_object({}) do |field_name, hash|
8
- hash[field_name] = public_send field_name
8
+ field_value = public_send(field_name)
9
+
10
+ field_name, field_value = yield [field_name, field_value] if block_given?
11
+
12
+ hash[field_name] = field_value
9
13
  end
10
14
  end
11
15
  end
@@ -47,7 +47,6 @@ module DecoLite
47
47
  # options while loading, but also provide option customization
48
48
  # of options when needed.
49
49
  options = Options.with_defaults(options, defaults: self.options)
50
-
51
50
  load_hash(hash: hash, deco_lite_options: options)
52
51
 
53
52
  self
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the version of this gem.
4
4
  module DecoLite
5
- VERSION = '0.3.1'
5
+ VERSION = '0.3.2'
6
6
  end
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.3.1
4
+ version: 0.3.2
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-27 00:00:00.000000000 Z
11
+ date: 2022-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel