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 +4 -4
- data/.gitignore +1 -1
- data/.reek.yml +1 -1
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +8 -0
- data/README.md +8 -9
- data/lib/deco_lite/field_assignable.rb +2 -4
- data/lib/deco_lite/field_conflictable.rb +2 -0
- data/lib/deco_lite/hash_loadable.rb +2 -0
- data/lib/deco_lite/hashable.rb +5 -1
- data/lib/deco_lite/model.rb +0 -1
- data/lib/deco_lite/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b5b5ed040bd367962dd57760a6a0e572b60485ba1d2d3630980ff1a090ca9075
|
|
4
|
+
data.tar.gz: 76402762f17d93fd53495ddabf86487803b0014971db007f61f505727e933f56
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 584e1c07a160370bc9548e7a41dbaa9adcbee757a35a37925d02accf4ebe71d24cd9d2932e706724f5d8d64e8d98ff77f223224de27e5a0d817eb2675fea81ca
|
|
7
|
+
data.tar.gz: 6a49a3ab8bdcb947341697577e130dd667c508095578536e8d003cfa1424f8e090f3c40e9ddc1376a3337cdcc9a26a02e836bc901c0ae36da39a8acaaae8cc46
|
data/.gitignore
CHANGED
data/.reek.yml
CHANGED
data/.rubocop.yml
CHANGED
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
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
data/lib/deco_lite/hashable.rb
CHANGED
|
@@ -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
|
-
|
|
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
|
data/lib/deco_lite/model.rb
CHANGED
data/lib/deco_lite/version.rb
CHANGED
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.
|
|
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-
|
|
11
|
+
date: 2022-08-29 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activemodel
|