paramore 2.0.0 → 3.2.0
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 +4 -5
- data/lib/paramore/cast_parameters.rb +22 -38
- data/lib/paramore/errors.rb +1 -1
- data/lib/paramore/extension.rb +6 -8
- data/lib/paramore/field.rb +38 -0
- data/lib/paramore/permitted_parameter_argument.rb +16 -12
- data/lib/paramore/validate.rb +19 -8
- data/lib/paramore.rb +3 -3
- metadata +4 -4
- data/lib/paramore/field_schema.rb +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8620a326873694bcc720cd186b9f4998fde3ab3fc102e2cb81b55822accc1e1
|
4
|
+
data.tar.gz: '07288ff15f31bb8480fc30662da17cd6aba34617afff96cd488c205b2c4c4660'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 753ede1706d8dc9563bfa8cfad77683586ac319aefd4f84f80b6247d0daa7054710809a4ae24f812196402a83628d0760992ba8f281de79197e9b69cd39f9570
|
7
|
+
data.tar.gz: '078b73a5da242dc7fb75d1175232af4d52928d1cd5db1af9366ca2e95fec990c7ec0e37eaf48c3560e8f27f47a351d03bd448589354bd1f335cc3951763702f5'
|
data/README.md
CHANGED
@@ -75,15 +75,15 @@ class ItemsController < ApplicationController
|
|
75
75
|
end
|
76
76
|
|
77
77
|
param_schema :item_params,
|
78
|
-
item: {
|
78
|
+
item: Paramore.field({
|
79
79
|
name: Paramore.field(Paramore::SanitizedString),
|
80
80
|
description: Paramore.field(Paramore::StrippedString, null: true),
|
81
|
-
for_sale: Paramore.field(Paramore::Boolean),
|
81
|
+
for_sale: Paramore.field(Paramore::Boolean, default: false),
|
82
82
|
price: Paramore.field(Paramore::Decimal),
|
83
83
|
metadata: Paramore.field({
|
84
84
|
tags: Paramore.field([Types::ItemTag], compact: true)
|
85
85
|
})
|
86
|
-
}
|
86
|
+
})
|
87
87
|
end
|
88
88
|
```
|
89
89
|
|
@@ -133,8 +133,7 @@ This can be disabled for any type by declaring `Paramore.field(Paramore::Int, nu
|
|
133
133
|
|
134
134
|
nils will usually not reach any of the type classes - if some parameter is nullable, the class will not be called.
|
135
135
|
If a parameter is non-nullable, then a `Paramore::NilParameter` error will be raised before calling the class.
|
136
|
-
If
|
137
|
-
nils inside arrays can still be passed to type classes by declaring `Paramore.field([Paramore::Int], empty: true)`.
|
136
|
+
If an incoming array contains nils, they will get passed to type classes.
|
138
137
|
If you wish to get rid of empty array elements, declare `Paramore.field(Paramore::Int, compact: true)`.
|
139
138
|
|
140
139
|
<h3>Configuration</h3>
|
@@ -3,58 +3,42 @@ require_relative 'errors'
|
|
3
3
|
module Paramore
|
4
4
|
module CastParameters
|
5
5
|
module_function
|
6
|
-
def run(
|
7
|
-
|
8
|
-
recursive_typecast(
|
9
|
-
types_definition, permitted_params
|
10
|
-
)
|
11
|
-
)
|
6
|
+
def run(field, data)
|
7
|
+
cast(field, data, 'data')
|
12
8
|
end
|
13
9
|
|
14
|
-
def
|
15
|
-
|
16
|
-
if
|
17
|
-
|
10
|
+
def cast(field, value, name = nil)
|
11
|
+
if value.nil?
|
12
|
+
if field.nullable? || field.default
|
13
|
+
return field.default
|
18
14
|
else
|
19
|
-
|
15
|
+
raise Paramore::NilParameter, name
|
20
16
|
end
|
21
|
-
end.reduce(:merge)
|
22
|
-
end
|
23
|
-
|
24
|
-
def recursive_typecast(types_definition, permitted_params)
|
25
|
-
types_definition.map do |param_name, definition|
|
26
|
-
value = permitted_params[param_name]
|
27
|
-
|
28
|
-
if value.nil?
|
29
|
-
if definition.nullable?
|
30
|
-
next { param_name => nil }
|
31
|
-
else
|
32
|
-
raise Paramore::NilParameter, param_name
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
{ param_name => cast(definition, value) }
|
37
17
|
end
|
38
|
-
end
|
39
18
|
|
40
|
-
|
41
|
-
case definition.type
|
19
|
+
case field.type
|
42
20
|
when Hash
|
43
|
-
|
21
|
+
typecast_hash(field.type, value || {})
|
44
22
|
when Array
|
45
|
-
typecast_array(
|
23
|
+
typecast_array(field, value)
|
46
24
|
else
|
47
|
-
|
25
|
+
if value == '' && !field.allow_empty? && field.nullable?
|
26
|
+
nil
|
27
|
+
else
|
28
|
+
typecast_value(field.type, value)
|
29
|
+
end
|
48
30
|
end
|
49
31
|
end
|
50
32
|
|
51
|
-
def
|
33
|
+
def typecast_hash(field, hash)
|
34
|
+
field.to_h { |name, field| [name, cast(field, hash[name], name)] }
|
35
|
+
end
|
36
|
+
|
37
|
+
def typecast_array(field, array)
|
52
38
|
array
|
53
|
-
.reject { |unit| unit.to_s == '' &&
|
39
|
+
.reject { |unit| unit.to_s == '' && field.compact? }
|
54
40
|
.map do |unit|
|
55
|
-
|
56
|
-
typecast_value(definition.type.first, unit)
|
57
|
-
end
|
41
|
+
cast(Paramore.field(field.type.first, null: true), unit)
|
58
42
|
end
|
59
43
|
end
|
60
44
|
|
data/lib/paramore/errors.rb
CHANGED
@@ -4,7 +4,7 @@ class Paramore::NilParameter < StandardError
|
|
4
4
|
end
|
5
5
|
end
|
6
6
|
|
7
|
-
class Paramore::
|
7
|
+
class Paramore::NonField < StandardError
|
8
8
|
def initialize(param_name, type)
|
9
9
|
super("`#{param_name}` defined as a `#{type.class}`, expected a call of `Paramore.field()`! Perhaps you declared a plain hash instead of Paramore.field({})?")
|
10
10
|
end
|
data/lib/paramore/extension.rb
CHANGED
@@ -8,9 +8,7 @@ module Paramore
|
|
8
8
|
default
|
9
9
|
].freeze
|
10
10
|
|
11
|
-
def param_schema(accessor_name,
|
12
|
-
parameter_configuration = configuration.except(*OPTIONS)
|
13
|
-
|
11
|
+
def param_schema(accessor_name, parameter_configuration)
|
14
12
|
unless parameter_configuration.keys.size == 1
|
15
13
|
raise ArgumentError,
|
16
14
|
"Paramore: exactly one required attribute allowed! Given: #{parameter_configuration.keys}"
|
@@ -19,10 +17,10 @@ module Paramore
|
|
19
17
|
required_parameter_name = parameter_configuration.keys.first
|
20
18
|
types_definition = parameter_configuration.values.first
|
21
19
|
|
22
|
-
Paramore::Validate.run(types_definition) if types_definition.is_a?(
|
20
|
+
Paramore::Validate.run(types_definition) if types_definition.is_a?(Paramore::Field)
|
23
21
|
|
24
22
|
permitted_parameter_argument =
|
25
|
-
if types_definition.is_a?(
|
23
|
+
if types_definition.is_a?(Paramore::Field)
|
26
24
|
Paramore::PermittedParameterArgument.parse(types_definition)
|
27
25
|
else
|
28
26
|
types_definition
|
@@ -31,8 +29,8 @@ module Paramore
|
|
31
29
|
define_method(accessor_name) do |rails_parameters = params|
|
32
30
|
return instance_variable_get("@#{accessor_name}") if instance_variable_defined?("@#{accessor_name}")
|
33
31
|
|
34
|
-
if rails_parameters[required_parameter_name].nil? &&
|
35
|
-
instance_variable_set("@#{accessor_name}",
|
32
|
+
if rails_parameters[required_parameter_name].nil? && types_definition.default
|
33
|
+
instance_variable_set("@#{accessor_name}", types_definition.default)
|
36
34
|
return instance_variable_get("@#{accessor_name}")
|
37
35
|
end
|
38
36
|
|
@@ -41,7 +39,7 @@ module Paramore
|
|
41
39
|
.permit(permitted_parameter_argument)
|
42
40
|
|
43
41
|
parameter_values =
|
44
|
-
if types_definition.is_a?(
|
42
|
+
if types_definition.is_a?(Paramore::Field)
|
45
43
|
permitted_params.merge(
|
46
44
|
Paramore::CastParameters.run(types_definition, permitted_params)
|
47
45
|
).permit!
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Paramore
|
2
|
+
class Field
|
3
|
+
DEFAULT_OPTIONS = {
|
4
|
+
null: false,
|
5
|
+
compact: false,
|
6
|
+
default: nil,
|
7
|
+
empty: true,
|
8
|
+
}.freeze
|
9
|
+
|
10
|
+
def initialize(given_type, null:, compact:, default:, empty:)
|
11
|
+
@given_type = given_type
|
12
|
+
@nullable = null
|
13
|
+
@compact = compact
|
14
|
+
@allow_empty = empty
|
15
|
+
@default = default
|
16
|
+
end
|
17
|
+
|
18
|
+
def allow_empty?
|
19
|
+
@allow_empty
|
20
|
+
end
|
21
|
+
|
22
|
+
def default
|
23
|
+
@default
|
24
|
+
end
|
25
|
+
|
26
|
+
def compact?
|
27
|
+
@compact
|
28
|
+
end
|
29
|
+
|
30
|
+
def nullable?
|
31
|
+
@nullable
|
32
|
+
end
|
33
|
+
|
34
|
+
def type
|
35
|
+
@given_type
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -2,22 +2,26 @@ module Paramore
|
|
2
2
|
module PermittedParameterArgument
|
3
3
|
module_function
|
4
4
|
|
5
|
-
def parse(
|
5
|
+
def parse(field)
|
6
|
+
parse_type(field.type)
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse_type(type)
|
6
10
|
merge_hashes(
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
case
|
13
|
-
when Array
|
14
|
-
{
|
15
|
-
when Hash
|
16
|
-
{ key => merge_hashes(parse(definition.type)) }
|
11
|
+
case type
|
12
|
+
when Array
|
13
|
+
parse_type(type.first)
|
14
|
+
when Hash
|
15
|
+
type.map do |name, field|
|
16
|
+
case field.type
|
17
|
+
when Array, Hash
|
18
|
+
{ name => parse_type(field.type) }
|
17
19
|
else
|
18
|
-
|
20
|
+
name
|
19
21
|
end
|
20
22
|
end
|
23
|
+
else
|
24
|
+
[]
|
21
25
|
end
|
22
26
|
)
|
23
27
|
end
|
data/lib/paramore/validate.rb
CHANGED
@@ -2,23 +2,34 @@ module Paramore
|
|
2
2
|
module Validate
|
3
3
|
module_function
|
4
4
|
|
5
|
-
def run(
|
6
|
-
types(
|
5
|
+
def run(root_field)
|
6
|
+
types(root_field.type).uniq.each do |type|
|
7
7
|
unless type.respond_to?(Paramore.configuration.type_method_name)
|
8
8
|
raise NoMethodError,
|
9
9
|
"Paramore: type `#{type}` does not respond to " +
|
10
10
|
"`#{Paramore.configuration.type_method_name}`!"
|
11
11
|
end
|
12
12
|
end
|
13
|
+
|
14
|
+
root_field
|
13
15
|
end
|
14
16
|
|
15
|
-
def types(
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
def types(type)
|
18
|
+
case type
|
19
|
+
when Hash
|
20
|
+
hash_types(type)
|
21
|
+
when Array
|
22
|
+
type.flat_map { |subtype| types(subtype) }
|
23
|
+
else
|
24
|
+
[type]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def hash_types(hash)
|
29
|
+
hash.flat_map do |param_name, field|
|
30
|
+
raise Paramore::NonField.new(param_name, field) unless field.is_a?(Paramore::Field)
|
20
31
|
|
21
|
-
|
32
|
+
field.type.is_a?(Hash) ? types(field.type) : field.type
|
22
33
|
end.uniq
|
23
34
|
end
|
24
35
|
end
|
data/lib/paramore.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require_relative 'paramore/configuration'
|
2
2
|
require_relative 'paramore/railtie'
|
3
3
|
require_relative 'paramore/types'
|
4
|
-
require_relative 'paramore/
|
4
|
+
require_relative 'paramore/field'
|
5
5
|
|
6
6
|
module Paramore
|
7
7
|
class << self
|
@@ -17,9 +17,9 @@ module Paramore
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.field(given_type, options = {})
|
20
|
-
Paramore::
|
20
|
+
Paramore::Field.new(
|
21
21
|
given_type,
|
22
|
-
**Paramore::
|
22
|
+
**Paramore::Field::DEFAULT_OPTIONS.merge(options)
|
23
23
|
)
|
24
24
|
end
|
25
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paramore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 3.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lukas Kairevičius
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|
@@ -91,7 +91,7 @@ files:
|
|
91
91
|
- lib/paramore/configuration.rb
|
92
92
|
- lib/paramore/errors.rb
|
93
93
|
- lib/paramore/extension.rb
|
94
|
-
- lib/paramore/
|
94
|
+
- lib/paramore/field.rb
|
95
95
|
- lib/paramore/permitted_parameter_argument.rb
|
96
96
|
- lib/paramore/railtie.rb
|
97
97
|
- lib/paramore/types.rb
|
@@ -101,7 +101,7 @@ licenses:
|
|
101
101
|
- MIT
|
102
102
|
metadata: {}
|
103
103
|
post_install_message: |
|
104
|
-
Thank you for installing Paramore 2.0
|
104
|
+
Thank you for installing Paramore 3.2.0 !
|
105
105
|
From the command line you can run `paramore` to generate a configuration file
|
106
106
|
|
107
107
|
More details here : https://github.com/lumzdas/paramore/blob/master/README.md
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module Paramore
|
2
|
-
class FieldSchema
|
3
|
-
DEFAULT_OPTIONS = {
|
4
|
-
null: false,
|
5
|
-
empty: false,
|
6
|
-
compact: false,
|
7
|
-
}.freeze
|
8
|
-
|
9
|
-
def initialize(given_type, null:, empty:, compact:)
|
10
|
-
@given_type = given_type
|
11
|
-
@nullable = null
|
12
|
-
@empty = empty
|
13
|
-
@compact = compact
|
14
|
-
end
|
15
|
-
|
16
|
-
def compact?
|
17
|
-
compact
|
18
|
-
end
|
19
|
-
|
20
|
-
def nullable?
|
21
|
-
nullable
|
22
|
-
end
|
23
|
-
|
24
|
-
def use_empty_strings?
|
25
|
-
empty
|
26
|
-
end
|
27
|
-
|
28
|
-
def type
|
29
|
-
given_type
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
attr_reader :given_type, :nullable, :empty, :compact
|
35
|
-
end
|
36
|
-
end
|