class_composer 1.0.0 → 1.0.1
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/README.md +114 -7
- data/lib/class_composer/generator.rb +1 -1
- data/lib/class_composer/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: 32a9c6f0ff1d02646977e79cc67c746ae3a24a4fdce5b22003e6c9d0035ea0df
|
4
|
+
data.tar.gz: 55bc12b96385d9772980f9348b8b0d42f1bb0436736fcaaec018fa7f5a681418
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7dfffaa0dbe370e01dfa4e77a666e49fad3ff855b43b839e4d8ff0b31cd0ba547217d834bca792559147bb12aba55f1b0d14e7b18d751b20a893ae34c3ca736
|
7
|
+
data.tar.gz: d4f565781f952e89f2f5234ea1179736888ccf889f6aa94d231933beb96a752146029bc77c9a2afb637feb419075d00e15787af2fab15473da042928e22dbf08
|
data/README.md
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
# ClassComposer
|
2
2
|
|
3
|
-
|
4
|
-
able to package up your Ruby library into a gem. Put your Ruby code in the file
|
5
|
-
`lib/class_composer`. To experiment with that code, run
|
6
|
-
`bin/console` for an interactive prompt.
|
7
|
-
|
8
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Basic configuration is relatively simple but tedious to do if done multiple times. throughout a project. The intention of `ClassComposer` is to DRY up as much of that configuration as possible to allow you to just write code.
|
9
4
|
|
10
5
|
## Installation
|
11
6
|
|
7
|
+
`ClassComposer` is hosted on RubyGems https://rubygems.org/gems/class_composer
|
8
|
+
|
12
9
|
Add this line to your application's Gemfile:
|
13
10
|
|
14
11
|
```ruby
|
@@ -25,7 +22,117 @@ Or install it yourself as:
|
|
25
22
|
|
26
23
|
## Usage
|
27
24
|
|
28
|
-
|
25
|
+
### Basic
|
26
|
+
|
27
|
+
`add_composer` is the driving method behind the composer gem. It will
|
28
|
+
- Add a setter Method
|
29
|
+
- Add a getter Method
|
30
|
+
- Add instance variable
|
31
|
+
- Add validation Method
|
32
|
+
|
33
|
+
In short, Composer will behave similarly to `attr_accessor`
|
34
|
+
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
require 'class_composer'
|
38
|
+
|
39
|
+
class MyConfigurableClass
|
40
|
+
include ClassComposer::Generator
|
41
|
+
|
42
|
+
ALLOWED_FIBONACCI = [0, 2, 8, 13, 34]
|
43
|
+
|
44
|
+
add_composer :status, allowed: Integer, default: 35
|
45
|
+
add_composer :number, allowed: Integer, default: 0, validator: -> (val) { }
|
46
|
+
# when no default is provided, nil will be returned
|
47
|
+
add_composer :fibbonacci, allowed: Array, validator: ->(val) { val.all? {|i| i.is_a?(Integer) } && (val - ALLOWED_FIBONACCI) > 0 }, invalid_message: ->(val) { "We only allow #{ALLOWED_FIBONACCI} numbers. Received #{val}" }
|
48
|
+
# Allowed can be passed an array of allowed class types
|
49
|
+
add_composer :type, allowed: [Proc, Integer], default: 35
|
50
|
+
```
|
51
|
+
|
52
|
+
### KWarg Options
|
53
|
+
|
54
|
+
```
|
55
|
+
allowed
|
56
|
+
- Required: True
|
57
|
+
- What: Expected value of the name of the composed method
|
58
|
+
- Type: Array of Class types or Single Class type
|
59
|
+
|
60
|
+
validator
|
61
|
+
- Required: False
|
62
|
+
- What: Custom way to validate the value of the composed method
|
63
|
+
- Type: Proc
|
64
|
+
- Default: ->(_) { true }
|
65
|
+
- By default validation happens on the `allowed` KWARG first and then the passed in validator function. Proc should expect that the type passed in is one of `allowed`
|
66
|
+
|
67
|
+
validation_error_klass
|
68
|
+
- Required: false
|
69
|
+
- What: Class to raise when a validation error occurs from `allowed` KWarg or from the passed in `validator` proc
|
70
|
+
- Type: Class
|
71
|
+
- Default: ClassComposer::ValidatorError
|
72
|
+
|
73
|
+
validation_error_klass
|
74
|
+
- Required: false
|
75
|
+
- What: Class to raise when a errors occur outside of validation. This can be for composer method errors or proc errors during validation
|
76
|
+
- Type: Class
|
77
|
+
- Default: ClassComposer::Error
|
78
|
+
|
79
|
+
default
|
80
|
+
- Required: false
|
81
|
+
- What: This is the default value to set for the composed method
|
82
|
+
- Type: Should match the `allowed` KWarg
|
83
|
+
- Default: nil
|
84
|
+
- Note: When no default value is provided, the return value from the getter will be `nil`. However, this does not mean that NilClass will be an acceptable value during the setter method
|
85
|
+
|
86
|
+
invalid_message
|
87
|
+
- Required: False
|
88
|
+
- What: Message to add to the base invalid setter method
|
89
|
+
- Type: Proc or String
|
90
|
+
- Proc: ->(val) { } # where val is the failed value of the setter method
|
91
|
+
|
92
|
+
```
|
93
|
+
|
94
|
+
### Advanced
|
95
|
+
|
96
|
+
#### Usage with Array as Allowed
|
97
|
+
Arrays are treated special with the composed methods. `ClassComposer` will inject a custom method `<<` so that it can be treated as a regular array with the added benefit of validation still occuring.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class CustomArrayClass
|
101
|
+
include ClassComposer::Generator
|
102
|
+
|
103
|
+
add_composer :array, allowed: Array, default: [], validator: ->(val) { val.sum < 40 }, invalid_message: ->(val) { "Array sum of [#{val.sum}] must be less than 40" }
|
104
|
+
end
|
105
|
+
|
106
|
+
instance = CustomArrayClass.new
|
107
|
+
instance.array << 1
|
108
|
+
instance.array << 2
|
109
|
+
instance.array
|
110
|
+
=> [1, 2]
|
111
|
+
instance.array << 50
|
112
|
+
ClassComposer::ValidatorError: CustomArrayClass.array failed validation. array is expected to be Array. Array sum of [53] must be less than 40
|
113
|
+
|
114
|
+
```
|
115
|
+
|
116
|
+
#### Usage with complex configuration
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
class ComplexDependencies
|
120
|
+
include ClassComposer::Generator
|
121
|
+
|
122
|
+
add_composer :use_scope, allowed: [TrueClass, FalseClass], default: false
|
123
|
+
add_composer :scope, allowed: Proc
|
124
|
+
|
125
|
+
def scope
|
126
|
+
# skip unless use_scope is explicitly set
|
127
|
+
return -> {} unless @use_scope
|
128
|
+
|
129
|
+
# use passed in scope if present
|
130
|
+
# Otherwise default to blank default
|
131
|
+
@scope || -> {}
|
132
|
+
end
|
133
|
+
end
|
134
|
+
```
|
135
|
+
Adding custom methods allows for higher level of complexity. The methods can be used and accessed just as an `attr_accessor` would.
|
29
136
|
|
30
137
|
## Development
|
31
138
|
|
@@ -13,7 +13,7 @@ module ClassComposer
|
|
13
13
|
COMPOSER_ASSIGNED_ATTR_NAME = ->(name) { :"@__composer_#{name}_value_assigned__" }
|
14
14
|
COMPOSER_ASSIGNED_ARRAY_METHODS = ->(name) { :"@__composer_#{name}_array_methods_set__" }
|
15
15
|
|
16
|
-
def add_composer(name, allowed:, accessor: true, validator: ->(_) { true }, validation_error_klass: ::ClassComposer::ValidatorError, error_klass: ::ClassComposer::Error
|
16
|
+
def add_composer(name, allowed:, accessor: true, validator: ->(_) { true }, validation_error_klass: ::ClassComposer::ValidatorError, error_klass: ::ClassComposer::Error, **params)
|
17
17
|
default =
|
18
18
|
if params.has_key?(:default)
|
19
19
|
params[:default]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: class_composer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Taylor
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry-byebug
|