paramore 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d42894f0d3cefbd8979dc3048515ad8285a7f322f1ec1907b19d9941d6f95620
4
- data.tar.gz: '0283777104760cb5d2d8774d95a61aa5841adaa38f35a488218cf89865a2571b'
3
+ metadata.gz: 6bc18680611ecf9983143593443a9d04e3a7b57ac77111f183960287600a9592
4
+ data.tar.gz: 613b869bddc6de6a585e8de9f0448373a40da62d4daecf736e19e0955fa477ed
5
5
  SHA512:
6
- metadata.gz: 19def8fd4558111735cb93a28157e3fd7a7905be0b11b4d4515c85ccee3f26f64d6ab1c1d6c464d96230030960a89bac4ff31c96e391603a8786a08c98bdb8a1
7
- data.tar.gz: da286395c83d3e13fc5ec1a0344a0232800fbf841bbc8bec14e32072059c32f678caef0d609cb17a6a4780e8ef3b4cd699fd52e2432ecca3dede925156ff0162
6
+ metadata.gz: efb59cc8e0ee2d9bb324f8d543ff7e5e22a652d4bc23b5ea067b5fdadb3d47cd85ea228d5d855e47a43980f2723b8dc7983d11f592c44404607c00967b522788
7
+ data.tar.gz: aba0613b0838fd1b1d523bfe62913e33f1b4be678891c0794db7af2aa3138cd9755ff3c8c9b501e13203ccb49c94cd6d04e303d43bb7df4951279cdc22ff7c44
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Paramore
2
2
 
3
- Paramore is a small gem intended to make strong parameter definitions declarative
4
- and provide a unified way to typecast and sanitize their values outside of controllers.
3
+ Paramore allows you to declare a typed schema for your params,
4
+ so that any downstream code can work with the data it expects.
5
5
 
6
6
  # Installation
7
7
 
@@ -20,7 +20,7 @@ $ bundle
20
20
  <h3>Without typing</h3>
21
21
 
22
22
  ```ruby
23
- paramorize :item_params,
23
+ param_schema :item_params,
24
24
  item: [:name, :description, :for_sale, :price, metadata: [tags: []]]
25
25
  ```
26
26
 
@@ -74,15 +74,15 @@ class ItemsController < ApplicationController
74
74
  Item.create(item_params)
75
75
  end
76
76
 
77
- paramorize :item_params,
77
+ param_schema :item_params,
78
78
  item: {
79
- name: Paratype[Paramore::SanitizedString],
80
- description: Paratype[Paramore::StrippedString, null: true],
81
- for_sale: Paratype[Paramore::Boolean],
82
- price: Paratype[Paramore::Decimal],
83
- metadata: Paratype[{
84
- tags: Paratype[[Types::ItemTag], compact: true]
85
- }]
79
+ name: Paramore.field(Paramore::SanitizedString),
80
+ description: Paramore.field(Paramore::StrippedString, null: true),
81
+ for_sale: Paramore.field(Paramore::Boolean),
82
+ price: Paramore.field(Paramore::Decimal),
83
+ metadata: Paramore.field({
84
+ tags: Paramore.field([Types::ItemTag], compact: true)
85
+ })
86
86
  }
87
87
  end
88
88
  ```
@@ -129,13 +129,13 @@ Notice that the `Paramore::StrippedString` does not perform `.squeeze(' ')`, onl
129
129
  <h3>nil</h3>
130
130
 
131
131
  Types are non-nullable by default and raise exceptions if the param hash misses any.
132
- This can be disabled for any type by declaring `Paratype[Paramore::Int, null: true]`.
132
+ This can be disabled for any type by declaring `Paramore.field(Paramore::Int, null: true)`.
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
136
  If a, say, `item_ids` array is non-nullable, but the received parameter is `['1', '', '3']`, only the `'1'` and `'2'` will get passed to type classes, and the resulting array will contain a nil, eg.: `['1', nil, '3']`.
137
- nils inside arrays can still be passed to type classes by declaring `Paratype[[Paramore::Int], empty: true]`.
138
- If you wish to get rid of empty array elements, declare `Paratype[Paramore::Int, compact: true]`.
137
+ nils inside arrays can still be passed to type classes by declaring `Paramore.field([Paramore::Int], empty: true)`.
138
+ If you wish to get rid of empty array elements, declare `Paramore.field(Paramore::Int, compact: true)`.
139
139
 
140
140
  <h3>Configuration</h3>
141
141
 
@@ -4,8 +4,8 @@ class Paramore::NilParameter < StandardError
4
4
  end
5
5
  end
6
6
 
7
- class Paramore::NonParatype < StandardError
7
+ class Paramore::NonFieldSchema < StandardError
8
8
  def initialize(param_name, type)
9
- super("`#{param_name}` defined as a `#{type.class}`, expected `Paratype`! Perhaps you declared a plain hash instead of Paratype[{}]?")
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
11
11
  end
@@ -4,10 +4,16 @@ require_relative 'permitted_parameter_argument'
4
4
 
5
5
  module Paramore
6
6
  module Extension
7
- def paramorize(accessor_name, parameter_configuration)
7
+ OPTIONS = %i[
8
+ default
9
+ ].freeze
10
+
11
+ def param_schema(accessor_name, configuration)
12
+ parameter_configuration = configuration.except(*OPTIONS)
13
+
8
14
  unless parameter_configuration.keys.size == 1
9
15
  raise ArgumentError,
10
- "Paramore: exactly one required attribute allowed! Given: #{param_definition.keys}"
16
+ "Paramore: exactly one required attribute allowed! Given: #{parameter_configuration.keys}"
11
17
  end
12
18
 
13
19
  required_parameter_name = parameter_configuration.keys.first
@@ -15,15 +21,20 @@ module Paramore
15
21
 
16
22
  Paramore::Validate.run(types_definition) if types_definition.is_a?(Hash)
17
23
 
24
+ permitted_parameter_argument =
25
+ if types_definition.is_a?(Hash)
26
+ Paramore::PermittedParameterArgument.parse(types_definition)
27
+ else
28
+ types_definition
29
+ end
30
+
18
31
  define_method(accessor_name) do |rails_parameters = params|
19
32
  return instance_variable_get("@#{accessor_name}") if instance_variable_defined?("@#{accessor_name}")
20
33
 
21
- permitted_parameter_argument =
22
- if types_definition.is_a?(Hash)
23
- Paramore::PermittedParameterArgument.parse(types_definition)
24
- else
25
- types_definition
26
- end
34
+ if rails_parameters[required_parameter_name].nil? && configuration[:default]
35
+ instance_variable_set("@#{accessor_name}", configuration[:default])
36
+ return instance_variable_get("@#{accessor_name}")
37
+ end
27
38
 
28
39
  permitted_params = rails_parameters
29
40
  .require(required_parameter_name)
@@ -0,0 +1,36 @@
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
@@ -8,7 +8,7 @@ module Paramore
8
8
  case definition
9
9
  when Hash
10
10
  { key => merge_hashes(parse(definition)) }
11
- when Paratype
11
+ when Paramore::FieldSchema
12
12
  case definition.type
13
13
  when Array
14
14
  { key => [] }
@@ -13,12 +13,12 @@ module Paramore
13
13
  end
14
14
 
15
15
  def types(types_definition)
16
- types_definition.flat_map do |param_name, paratype|
17
- unless paratype.is_a?(Paratype)
18
- raise Paramore::NonParatype.new(param_name, paratype)
16
+ types_definition.flat_map do |param_name, field_schema|
17
+ unless field_schema.is_a?(Paramore::FieldSchema)
18
+ raise Paramore::NonFieldSchema.new(param_name, field_schema)
19
19
  end
20
20
 
21
- paratype.type.is_a?(Hash) ? types(paratype.type) : paratype.type
21
+ field_schema.type.is_a?(Hash) ? types(field_schema.type) : field_schema.type
22
22
  end.uniq
23
23
  end
24
24
  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 'paratype'
4
+ require_relative 'paramore/field_schema'
5
5
 
6
6
  module Paramore
7
7
  class << self
@@ -15,4 +15,11 @@ module Paramore
15
15
  def self.configure
16
16
  yield(configuration)
17
17
  end
18
+
19
+ def self.field(given_type, options = {})
20
+ Paramore::FieldSchema.new(
21
+ given_type,
22
+ **Paramore::FieldSchema::DEFAULT_OPTIONS.merge(options)
23
+ )
24
+ end
18
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: 1.0.1
4
+ version: 2.0.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-06-30 00:00:00.000000000 Z
11
+ date: 2021-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails
@@ -56,16 +56,22 @@ dependencies:
56
56
  name: rails
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '5.0'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '7'
62
65
  type: :runtime
63
66
  prerelease: false
64
67
  version_requirements: !ruby/object:Gem::Requirement
65
68
  requirements:
66
- - - "~>"
69
+ - - ">="
67
70
  - !ruby/object:Gem::Version
68
71
  version: '5.0'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '7'
69
75
  description: |
70
76
  Paramore lets you declare which parameters are permitted and what object is responsible
71
77
  for typing/sanitizing/type-casting them before they are passed along to your models/domain.
@@ -85,17 +91,17 @@ files:
85
91
  - lib/paramore/configuration.rb
86
92
  - lib/paramore/errors.rb
87
93
  - lib/paramore/extension.rb
94
+ - lib/paramore/field_schema.rb
88
95
  - lib/paramore/permitted_parameter_argument.rb
89
96
  - lib/paramore/railtie.rb
90
97
  - lib/paramore/types.rb
91
98
  - lib/paramore/validate.rb
92
- - lib/paratype.rb
93
99
  homepage: https://github.com/lumzdas/paramore
94
100
  licenses:
95
101
  - MIT
96
102
  metadata: {}
97
103
  post_install_message: |
98
- Thank you for installing Paramore 1.0.1 !
104
+ Thank you for installing Paramore 2.0.0 !
99
105
  From the command line you can run `paramore` to generate a configuration file
100
106
 
101
107
  More details here : https://github.com/lumzdas/paramore/blob/master/README.md
data/lib/paratype.rb DELETED
@@ -1,32 +0,0 @@
1
- class Paratype
2
- def self.[](given_type, null: false, empty: false, compact: false)
3
- self.new(given_type, null: null, empty: empty, compact: compact)
4
- end
5
-
6
- def initialize(given_type, null:, empty:, compact:)
7
- @given_type = given_type
8
- @nullable = null
9
- @empty = empty
10
- @compact = compact
11
- end
12
-
13
- def compact?
14
- compact
15
- end
16
-
17
- def nullable?
18
- nullable
19
- end
20
-
21
- def use_empty_strings?
22
- empty
23
- end
24
-
25
- def type
26
- given_type
27
- end
28
-
29
- private
30
-
31
- attr_reader :given_type, :nullable, :empty, :compact
32
- end