deco_lite 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.reek.yml +18 -0
- data/.rspec +3 -0
- data/.rubocop.yml +188 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +105 -0
- data/LICENSE.txt +21 -0
- data/README.md +193 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/deco_lite.gemspec +57 -0
- data/lib/deco_lite/field_assignable.rb +25 -0
- data/lib/deco_lite/field_conflictable.rb +23 -0
- data/lib/deco_lite/field_creatable.rb +24 -0
- data/lib/deco_lite/field_informable.rb +81 -0
- data/lib/deco_lite/field_requireable.rb +35 -0
- data/lib/deco_lite/field_retrievable.rb +13 -0
- data/lib/deco_lite/fields_optionable.rb +16 -0
- data/lib/deco_lite/hash_loadable.rb +24 -0
- data/lib/deco_lite/model.rb +43 -0
- data/lib/deco_lite/model_nameable.rb +19 -0
- data/lib/deco_lite/namespace_optionable.rb +11 -0
- data/lib/deco_lite/optionable.rb +26 -0
- data/lib/deco_lite/options.rb +41 -0
- data/lib/deco_lite/options_defaultable.rb +17 -0
- data/lib/deco_lite/options_validatable.rb +49 -0
- data/lib/deco_lite/version.rb +6 -0
- data/lib/deco_lite.rb +5 -0
- metadata +273 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c4fa31da1d9880f0afe9d90f953772ca606551a159bce150d70eebe6875e63f2
|
4
|
+
data.tar.gz: cbf4dd4e1185c04df3a8463d1ec4984f3ee6c8a8250f6775f6d32c15bec7a1f8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3a5b8509030d6546bbfc79076f13ef5704deb8f00928ced048a665b63ff19954bd60980d2727c8a3a14b5ca577434f8caa0b5528716247e0959098be883d31a3
|
7
|
+
data.tar.gz: 04bd6397371f07659c78568b05efdeee3d5077ed54b3ccb54637f6936d8cae777f409e9877112a6861aee60274c9d8d61a4c4f44d1d76e82a81c3205ae06ca40
|
data/.gitignore
ADDED
data/.reek.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
exclude_paths:
|
2
|
+
- vendor
|
3
|
+
- spec
|
4
|
+
detectors:
|
5
|
+
# TooManyInstanceVariables:
|
6
|
+
# exclude:
|
7
|
+
# - "Class1"
|
8
|
+
# - "Class2"
|
9
|
+
# private methods do not have to depend on instance state
|
10
|
+
# https://github.com/troessner/reek/blob/master/docs/Utility-Function.md
|
11
|
+
UtilityFunction:
|
12
|
+
public_methods_only: true
|
13
|
+
# Check for variable name that doesn't communicate its intent well enough
|
14
|
+
# https://github.com/troessner/reek/blob/master/docs/Uncommunicative-Variable-Name.md
|
15
|
+
UncommunicativeVariableName:
|
16
|
+
accept:
|
17
|
+
- /^_$/
|
18
|
+
- /^e$/
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 3.0.1
|
7
|
+
NewCops: enable
|
8
|
+
Exclude:
|
9
|
+
- '.git/**/*'
|
10
|
+
- '.idea/**/*'
|
11
|
+
- 'init/*'
|
12
|
+
- 'Rakefile'
|
13
|
+
- '*.gemspec'
|
14
|
+
- 'spec/**/*'
|
15
|
+
- 'vendor/**/*'
|
16
|
+
- 'scratch.rb'
|
17
|
+
|
18
|
+
# Align the elements of a hash literal if they span more than one line.
|
19
|
+
Layout/HashAlignment:
|
20
|
+
EnforcedLastArgumentHashStyle: always_ignore
|
21
|
+
|
22
|
+
# Alignment of parameters in multi-line method definition.
|
23
|
+
# The `with_fixed_indentation` style aligns the following lines with one
|
24
|
+
# level of indentation relative to the start of the line with the method
|
25
|
+
# definition.
|
26
|
+
#
|
27
|
+
# def my_method(a,
|
28
|
+
# b)
|
29
|
+
Layout/ParameterAlignment:
|
30
|
+
EnforcedStyle: with_fixed_indentation
|
31
|
+
|
32
|
+
# Alignment of parameters in multi-line method call.
|
33
|
+
# The `with_fixed_indentation` style aligns the following lines with one
|
34
|
+
# level of indentation relative to the start of the line with the method call.
|
35
|
+
#
|
36
|
+
# my_method(a,
|
37
|
+
# b)
|
38
|
+
Layout/ArgumentAlignment:
|
39
|
+
EnforcedStyle: with_fixed_indentation
|
40
|
+
|
41
|
+
# a = case n
|
42
|
+
# when 0
|
43
|
+
# x * 2
|
44
|
+
# else
|
45
|
+
# y / 3
|
46
|
+
# end
|
47
|
+
Layout/CaseIndentation:
|
48
|
+
EnforcedStyle: end
|
49
|
+
|
50
|
+
# Enforces a configured order of definitions within a class body
|
51
|
+
Layout/ClassStructure:
|
52
|
+
Enabled: true
|
53
|
+
|
54
|
+
# Align `end` with the matching keyword or starting expression except for
|
55
|
+
# assignments, where it should be aligned with the LHS.
|
56
|
+
Layout/EndAlignment:
|
57
|
+
EnforcedStyleAlignWith: variable
|
58
|
+
AutoCorrect: true
|
59
|
+
|
60
|
+
# The `consistent` style enforces that the first element in an array
|
61
|
+
# literal where the opening bracket and the first element are on
|
62
|
+
# seprate lines is indented the same as an array literal which is not
|
63
|
+
# defined inside a method call.
|
64
|
+
Layout/FirstArrayElementIndentation:
|
65
|
+
EnforcedStyle: consistent
|
66
|
+
|
67
|
+
# The `consistent` style enforces that the first key in a hash
|
68
|
+
# literal where the opening brace and the first key are on
|
69
|
+
# seprate lines is indented the same as a hash literal which is not
|
70
|
+
# defined inside a method call.
|
71
|
+
Layout/FirstHashElementIndentation:
|
72
|
+
EnforcedStyle: consistent
|
73
|
+
|
74
|
+
# Indent multi-line methods instead of aligning with periods
|
75
|
+
Layout/MultilineMethodCallIndentation:
|
76
|
+
EnforcedStyle: indented
|
77
|
+
|
78
|
+
# Allow `debug` in tasks for now
|
79
|
+
Lint/Debugger:
|
80
|
+
Exclude:
|
81
|
+
- 'RakeFile'
|
82
|
+
|
83
|
+
# A calculated magnitude based on number of assignments, branches, and
|
84
|
+
# conditions.
|
85
|
+
# NOTE: This is temporarily disabled until we can eliminate existing Rubocop
|
86
|
+
# complaints
|
87
|
+
Metrics/AbcSize:
|
88
|
+
Enabled: false
|
89
|
+
|
90
|
+
# Avoid long blocks with many lines.
|
91
|
+
Metrics/BlockLength:
|
92
|
+
Exclude:
|
93
|
+
- 'RakeFile'
|
94
|
+
- 'db/seeds.rb'
|
95
|
+
- 'spec/**/*.rb'
|
96
|
+
|
97
|
+
# Avoid classes longer than 100 lines of code.
|
98
|
+
# NOTE: This is temporarily disabled until we can eliminate existing Rubocop
|
99
|
+
# complaints
|
100
|
+
Metrics/ClassLength:
|
101
|
+
Max: 200
|
102
|
+
Exclude:
|
103
|
+
- 'spec/**/*.rb'
|
104
|
+
|
105
|
+
# A complexity metric that is strongly correlated to the number of test cases
|
106
|
+
# needed to validate a method.
|
107
|
+
Metrics/CyclomaticComplexity:
|
108
|
+
Max: 9
|
109
|
+
|
110
|
+
# Limit lines to 80 characters
|
111
|
+
Layout/LineLength:
|
112
|
+
Exclude:
|
113
|
+
- 'RakeFile'
|
114
|
+
- 'spec/**/*.rb'
|
115
|
+
|
116
|
+
# Avoid methods longer than 15 lines of code.
|
117
|
+
Metrics/MethodLength:
|
118
|
+
Max: 20
|
119
|
+
IgnoredMethods:
|
120
|
+
- swagger_path
|
121
|
+
- operation
|
122
|
+
|
123
|
+
|
124
|
+
# A complexity metric geared towards measuring complexity for a human reader.
|
125
|
+
Metrics/PerceivedComplexity:
|
126
|
+
Max: 10
|
127
|
+
|
128
|
+
# Naming/FileName:
|
129
|
+
# Exclude:
|
130
|
+
# - 'lib/file.rb'
|
131
|
+
|
132
|
+
# Allow `downcase == ` instead of forcing `casecmp`
|
133
|
+
Performance/Casecmp:
|
134
|
+
Enabled: false
|
135
|
+
|
136
|
+
# Require children definitions to be nested or compact in classes and modules
|
137
|
+
Style/ClassAndModuleChildren:
|
138
|
+
Enabled: false
|
139
|
+
|
140
|
+
# Document classes and non-namespace modules.
|
141
|
+
# (Disabled for now, may revisit later)
|
142
|
+
Style/Documentation:
|
143
|
+
Enabled: false
|
144
|
+
|
145
|
+
# Checks the formatting of empty method definitions.
|
146
|
+
Style/EmptyMethod:
|
147
|
+
EnforcedStyle: expanded
|
148
|
+
|
149
|
+
# Add the frozen_string_literal comment to the top of files to help transition
|
150
|
+
# to frozen string literals by default.
|
151
|
+
Style/FrozenStringLiteralComment:
|
152
|
+
EnforcedStyle: always
|
153
|
+
|
154
|
+
# Check for conditionals that can be replaced with guard clauses
|
155
|
+
Style/GuardClause:
|
156
|
+
Enabled: false
|
157
|
+
|
158
|
+
Style/MixinUsage:
|
159
|
+
Exclude:
|
160
|
+
- 'RakeFile'
|
161
|
+
|
162
|
+
# Avoid multi-line method signatures.
|
163
|
+
Style/MultilineMethodSignature:
|
164
|
+
Enabled: true
|
165
|
+
|
166
|
+
# Don't use option hashes when you can use keyword arguments.
|
167
|
+
Style/OptionHash:
|
168
|
+
Enabled: true
|
169
|
+
|
170
|
+
# Use return instead of return nil.
|
171
|
+
Style/ReturnNil:
|
172
|
+
Enabled: true
|
173
|
+
|
174
|
+
# Allow code like `return x, y` as it's occasionally handy.
|
175
|
+
Style/RedundantReturn:
|
176
|
+
AllowMultipleReturnValues: true
|
177
|
+
|
178
|
+
# Prefer symbols instead of strings as hash keys.
|
179
|
+
Style/StringHashKeys:
|
180
|
+
Enabled: true
|
181
|
+
|
182
|
+
# Checks if configured preferred methods are used over non-preferred.
|
183
|
+
Style/StringMethods:
|
184
|
+
Enabled: true
|
185
|
+
|
186
|
+
# Checks for use of parentheses around ternary conditions.
|
187
|
+
Style/TernaryParentheses:
|
188
|
+
EnforcedStyle: require_parentheses_when_complex
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.0.1
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at web.gma@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
deco_lite (0.1.0)
|
5
|
+
activemodel (~> 7.0, >= 7.0.3.1)
|
6
|
+
activesupport (~> 7.0, >= 7.0.3.1)
|
7
|
+
immutable_struct_ex (~> 0.2.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activemodel (7.0.3.1)
|
13
|
+
activesupport (= 7.0.3.1)
|
14
|
+
activesupport (7.0.3.1)
|
15
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
16
|
+
i18n (>= 1.6, < 2)
|
17
|
+
minitest (>= 5.1)
|
18
|
+
tzinfo (~> 2.0)
|
19
|
+
ast (2.4.2)
|
20
|
+
byebug (11.1.3)
|
21
|
+
coderay (1.1.3)
|
22
|
+
concurrent-ruby (1.1.10)
|
23
|
+
diff-lcs (1.5.0)
|
24
|
+
docile (1.4.0)
|
25
|
+
i18n (1.12.0)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
immutable_struct_ex (0.2.1)
|
28
|
+
kwalify (0.7.2)
|
29
|
+
method_source (1.0.0)
|
30
|
+
minitest (5.16.2)
|
31
|
+
parallel (1.22.1)
|
32
|
+
parser (3.1.2.1)
|
33
|
+
ast (~> 2.4.1)
|
34
|
+
pry (0.13.1)
|
35
|
+
coderay (~> 1.1)
|
36
|
+
method_source (~> 1.0)
|
37
|
+
pry-byebug (3.9.0)
|
38
|
+
byebug (~> 11.0)
|
39
|
+
pry (~> 0.13.0)
|
40
|
+
rainbow (3.1.1)
|
41
|
+
rake (10.5.0)
|
42
|
+
reek (6.1.1)
|
43
|
+
kwalify (~> 0.7.0)
|
44
|
+
parser (~> 3.1.0)
|
45
|
+
rainbow (>= 2.0, < 4.0)
|
46
|
+
regexp_parser (2.5.0)
|
47
|
+
rexml (3.2.5)
|
48
|
+
rspec (3.11.0)
|
49
|
+
rspec-core (~> 3.11.0)
|
50
|
+
rspec-expectations (~> 3.11.0)
|
51
|
+
rspec-mocks (~> 3.11.0)
|
52
|
+
rspec-core (3.11.0)
|
53
|
+
rspec-support (~> 3.11.0)
|
54
|
+
rspec-expectations (3.11.0)
|
55
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
56
|
+
rspec-support (~> 3.11.0)
|
57
|
+
rspec-mocks (3.11.1)
|
58
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
59
|
+
rspec-support (~> 3.11.0)
|
60
|
+
rspec-support (3.11.0)
|
61
|
+
rubocop (1.9.1)
|
62
|
+
parallel (~> 1.10)
|
63
|
+
parser (>= 3.0.0.0)
|
64
|
+
rainbow (>= 2.2.2, < 4.0)
|
65
|
+
regexp_parser (>= 1.8, < 3.0)
|
66
|
+
rexml
|
67
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
68
|
+
ruby-progressbar (~> 1.7)
|
69
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
70
|
+
rubocop-ast (1.21.0)
|
71
|
+
parser (>= 3.1.1.0)
|
72
|
+
rubocop-performance (1.14.3)
|
73
|
+
rubocop (>= 1.7.0, < 2.0)
|
74
|
+
rubocop-ast (>= 0.4.0)
|
75
|
+
rubocop-rspec (2.4.0)
|
76
|
+
rubocop (~> 1.0)
|
77
|
+
rubocop-ast (>= 1.1.0)
|
78
|
+
ruby-progressbar (1.11.0)
|
79
|
+
simplecov (0.21.2)
|
80
|
+
docile (~> 1.1)
|
81
|
+
simplecov-html (~> 0.11)
|
82
|
+
simplecov_json_formatter (~> 0.1)
|
83
|
+
simplecov-html (0.12.3)
|
84
|
+
simplecov_json_formatter (0.1.4)
|
85
|
+
tzinfo (2.0.5)
|
86
|
+
concurrent-ruby (~> 1.0)
|
87
|
+
unicode-display_width (2.2.0)
|
88
|
+
|
89
|
+
PLATFORMS
|
90
|
+
x86_64-darwin-19
|
91
|
+
|
92
|
+
DEPENDENCIES
|
93
|
+
bundler (~> 2.2, >= 2.2.17)
|
94
|
+
deco_lite!
|
95
|
+
pry-byebug (~> 3.9)
|
96
|
+
rake (~> 10.0)
|
97
|
+
reek (~> 6.0, >= 6.0.4)
|
98
|
+
rspec (~> 3.10)
|
99
|
+
rubocop (~> 1.9.1)
|
100
|
+
rubocop-performance (~> 1.11, >= 1.11.3)
|
101
|
+
rubocop-rspec (~> 2.3)
|
102
|
+
simplecov (~> 0.21.2)
|
103
|
+
|
104
|
+
BUNDLED WITH
|
105
|
+
2.2.17
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 gangelo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,193 @@
|
|
1
|
+
# DecoLite
|
2
|
+
|
3
|
+
[![GitHub version](http://badge.fury.io/gh/gangelo%2Fdeco.svg)](https://badge.fury.io/gh/gangelo%2Fdeco)
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/deco_lite.svg)](https://badge.fury.io/rb/deco_lite)
|
6
|
+
|
7
|
+
[![](http://ruby-gem-downloads-badge.herokuapp.com/deco_lite?type=total)](http://www.rubydoc.info/gems/deco_lite/)
|
8
|
+
[![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg)](http://www.rubydoc.info/gems/deco_lite/)
|
9
|
+
|
10
|
+
[![Report Issues](https://img.shields.io/badge/report-issues-red.svg)](https://github.com/gangelo/deco_lite/issues)
|
11
|
+
|
12
|
+
[![License](http://img.shields.io/badge/license-MIT-yellowgreen.svg)](#license)
|
13
|
+
|
14
|
+
## Introduction
|
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.
|
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).
|
19
|
+
|
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
|
+
|
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.
|
23
|
+
|
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
|
+
|
26
|
+
```ruby
|
27
|
+
# NOTE: keys :name and :age are not unique across this Hash.
|
28
|
+
{
|
29
|
+
name: 'John Doe',
|
30
|
+
age: 35,
|
31
|
+
wife: {
|
32
|
+
name: 'Mary Doe',
|
33
|
+
age: 30,
|
34
|
+
},
|
35
|
+
dog: {
|
36
|
+
name: 'Brutus',
|
37
|
+
age: 7,
|
38
|
+
}
|
39
|
+
}
|
40
|
+
```
|
41
|
+
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:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
name=, name #=> 'John Doe'
|
45
|
+
age=, age #=> 35
|
46
|
+
wife_name=, wife_name #=> 'Mary Doe'
|
47
|
+
wife_age=, wife_age #=> 30
|
48
|
+
```
|
49
|
+
|
50
|
+
`DecoLite::Model#load` can be called _multiple times_, on the same model, with different Hashes. This could potentially cause `attr_accessor` name clashes. In order to ensure unique `attr_accessor` names, a _"namespace"_ may be _explicitly_ provided to ensure uniqueness. For example, continuing from the previous example; if we were to call `DecoLite::Model#load` a _second time_ with the following Hash, it would produce `attr_accessor` name clashes:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
{
|
54
|
+
name: 'Henry Doe',
|
55
|
+
age: 85,
|
56
|
+
}
|
57
|
+
```
|
58
|
+
|
59
|
+
However, passing a `namespace: :grandpa` option to the `DecoLite::Model#load` method, would produce the following `attr_accessors`, ensuring uniquess:
|
60
|
+
```ruby
|
61
|
+
# Unique now that the namespace "grandpa" has been applied.
|
62
|
+
grandpa_name=, grandpa_name #=> 'Henry Doe'
|
63
|
+
grandpa_age=, grandpa_age #=> 85
|
64
|
+
```
|
65
|
+
## Use Cases
|
66
|
+
|
67
|
+
### General
|
68
|
+
_Deco_ would _most likely_ thrive where the structure of the Hashe(s) consumed by the `DecoLite::Model#load` method is known. This is because of the way _Deco_ mangles loaded Hash key names to create unique `attr_accessor` names (see the Introduction section) although, I'm sure there are some metaprogramming geniuses out there that might prove me wrong. Assuming this is the case, _Deco_ would be ideal to handle Model attributes, Webservice JSON results (converted to Ruby Hash), JSON Web Token (JWT) payload, etc..
|
69
|
+
|
70
|
+
### Rails
|
71
|
+
Because `DecoLite::Model` includes `ActiveModel::Model`, it could also be ideal for use as a model in Rails applications, where a _decorator pattern_ can be used, and methods provided for use in Rails views; for example:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
class ViewModel < DecoLite::Model
|
75
|
+
validates :first, :last, presence: true
|
76
|
+
|
77
|
+
def salutation
|
78
|
+
"<span class='happy'>Hello <em>#{full_name}<em>, welcome back!</span>"
|
79
|
+
end
|
80
|
+
|
81
|
+
def full_name
|
82
|
+
"#{first} #{last}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
view_model = ViewModel.new
|
87
|
+
|
88
|
+
view_model.load(hash: { first: 'John', last: 'Doe' })
|
89
|
+
|
90
|
+
view_model.valid?
|
91
|
+
#=> true
|
92
|
+
|
93
|
+
view_model.full_name
|
94
|
+
=> "John Doe"
|
95
|
+
|
96
|
+
view_model.salutation
|
97
|
+
=> "<span class='happy'>Hello <em>John Doe<em>, welcome back!</span>"
|
98
|
+
```
|
99
|
+
### Etc., etc., etc.
|
100
|
+
|
101
|
+
Get creative. Please pop me an email and let me know how _you're_ using _Deco_.
|
102
|
+
|
103
|
+
## Installation
|
104
|
+
|
105
|
+
Add this line to your application's Gemfile:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
gem 'deco_lite'
|
109
|
+
```
|
110
|
+
|
111
|
+
And then execute:
|
112
|
+
|
113
|
+
$ bundle
|
114
|
+
|
115
|
+
Or install it yourself as:
|
116
|
+
|
117
|
+
$ gem install deco_lite
|
118
|
+
|
119
|
+
## Examples and Usage
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
require 'deco_lite'
|
123
|
+
|
124
|
+
husband = {
|
125
|
+
name: 'John Doe',
|
126
|
+
info: {
|
127
|
+
age: 21,
|
128
|
+
address: '1 street, boonton, nj 07005',
|
129
|
+
salary: 100_000,
|
130
|
+
},
|
131
|
+
}
|
132
|
+
|
133
|
+
wife = {
|
134
|
+
name: 'Mary Doe',
|
135
|
+
info: {
|
136
|
+
age: 20,
|
137
|
+
address: '1 street, boonton, nj 07005',
|
138
|
+
salary: 90_000,
|
139
|
+
},
|
140
|
+
}
|
141
|
+
|
142
|
+
class Couple < DecoLite::Model)
|
143
|
+
def live_together?
|
144
|
+
husband_info_address == wife_info_address
|
145
|
+
end
|
146
|
+
|
147
|
+
def breadwinner
|
148
|
+
case
|
149
|
+
when husband_info_salary > wife_info_salary
|
150
|
+
husband_name
|
151
|
+
when husband_info_salary < wife_info_salary
|
152
|
+
wife_name
|
153
|
+
else
|
154
|
+
"#{husband_name} and #{wife_name} make the same amount"
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
couple = Couple.new
|
160
|
+
|
161
|
+
couple.load(hash: husband, options: { namespace: :husband })
|
162
|
+
couple.load(hash: wife, options: { namespace: :wife })
|
163
|
+
|
164
|
+
# Will produce the following:
|
165
|
+
model.live_together? #=> true
|
166
|
+
model.breadwinner #=> John Doe
|
167
|
+
|
168
|
+
model.husband_name #=> John Doe
|
169
|
+
model.husband_info_age #=> 21
|
170
|
+
model.husband_info_address #=> 1 street, boonton, nj 07005
|
171
|
+
|
172
|
+
model.wife_name #=> Amy Doe
|
173
|
+
model.wife_info_age #=> 20
|
174
|
+
model.wife_info_address #=> 1 street, boonton, nj 07005
|
175
|
+
```
|
176
|
+
|
177
|
+
## Development
|
178
|
+
|
179
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
180
|
+
|
181
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
182
|
+
|
183
|
+
## Contributing
|
184
|
+
|
185
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/deco_lite. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
186
|
+
|
187
|
+
## License
|
188
|
+
|
189
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
190
|
+
|
191
|
+
## Code of Conduct
|
192
|
+
|
193
|
+
Everyone interacting in the DecoLite project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/deco_lite/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'deco_lite'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|