params_ready 0.0.1 → 0.0.2

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: 16b29937985fac59ceb959ed77ada492f9e943a81e926eb1fb0d5668f3868f97
4
- data.tar.gz: 4aab416caa7d1663265d09eacf8229a3e280db9a1d1e29c8b154d07b74543a50
3
+ metadata.gz: 6e0db6411b802c544350d14eb54c921ae019ac7b2365fc2639b4f2f1b77ee40a
4
+ data.tar.gz: 3811299181627e888b1ac97970a4264f850db87f0475be355a138a30aa825eb1
5
5
  SHA512:
6
- metadata.gz: 7e1ae323719b369cf6da284cd95e8596fe7e2071e0d4e7cb4e20253559a02b8ac06d34e1e79b02d7b16fd12ad0acfabd14ea57429895c0f55510e2fbf78a4790
7
- data.tar.gz: 9df1e4edf7cb7996a4f91e4036c2656abad0c980100c37166de5b014431f9093ff307a3ff374360a0eae20357b2f60ca28ac64ef33df105ec80f3b76229684a8
6
+ metadata.gz: f2f339b6dd2a649244d53c94168ad3e3b238a42268632175c08c3928e01861d75c5c19d7fc3c29b8e0482e8658ed6e5f3a17d9ccda34d0b63808b5e1538b4870
7
+ data.tar.gz: 355252607fd9c47d58acf4cf1176bc5d35677390bbfeecfe1e24d881f9303be23d20dbcc8611b9c4cff25ff277944e0a2ad9e85aef7ccf0e7174a0a02856f1c3
data/lib/params_ready.rb CHANGED
@@ -28,9 +28,9 @@ require_relative 'params_ready/query/variable_operator_predicate'
28
28
 
29
29
 
30
30
  module ParamsReady
31
- VERSION = '0.0.1'.freeze
31
+ VERSION = '0.0.2'.freeze
32
32
 
33
33
  def self.gem_version
34
34
  ::Gem::Version.new(VERSION)
35
35
  end
36
- end
36
+ end
@@ -27,8 +27,8 @@ module ParamsReady
27
27
  end
28
28
 
29
29
  def by_columns
30
- bare_value.each_with_object(Hash.new(:none)) do |tuple, hash|
31
- hash[tuple[0].unwrap] = tuple[1].unwrap
30
+ bare_value.each_with_index.each_with_object(Hash.new([:none, nil])) do |(tuple, index), hash|
31
+ hash[tuple[0].unwrap] = [tuple[1].unwrap, index]
32
32
  end
33
33
  end
34
34
 
@@ -10,8 +10,8 @@ module ParamsReady
10
10
  update_in(first_page_value, [])
11
11
  end
12
12
 
13
- def last_page(*args)
14
- update_in(last_page_value(*args), [])
13
+ def last_page(*args, **opts)
14
+ update_in(last_page_value(*args, **opts), [])
15
15
  end
16
16
  end
17
17
  end
@@ -113,7 +113,7 @@ module ParamsReady
113
113
  name_for_formatter :value
114
114
 
115
115
  def name_for_formatter
116
- coder_name = @coder.type_identifier
116
+ coder_name = @coder.type_identifier if @coder.respond_to? :type_identifier
117
117
  return coder_name unless coder_name.nil?
118
118
 
119
119
  super
@@ -6,26 +6,29 @@ require_relative '../extensions/class_reader_writer'
6
6
 
7
7
  module ParamsReady
8
8
  module Value
9
+ module Coercion
10
+ def try_coerce(input, context)
11
+ coerce input, context
12
+ rescue => _error
13
+ raise CoercionError.new(input, value_class_name)
14
+ end
15
+
16
+ def strict_default?
17
+ true
18
+ end
19
+ end
20
+
9
21
  class Coder
10
22
  extend Extensions::ClassReaderWriter
11
-
12
- class_reader_writer :type_identifier
13
- type_identifier :value
23
+ extend Coercion
14
24
 
15
25
  def self.value_class_name
16
26
  last = self.name.split("::").last
17
27
  last.remove('Coder')
18
28
  end
19
29
 
20
- def self.try_coerce(input, context)
21
- coerce input, context
22
- rescue => _error
23
- raise CoercionError.new(input, value_class_name)
24
- end
25
-
26
- def self.strict_default?
27
- true
28
- end
30
+ class_reader_writer :type_identifier
31
+ type_identifier :value
29
32
  end
30
33
 
31
34
  class GenericCoder
@@ -13,15 +13,26 @@ module ParamsReady
13
13
  type = Value::Constraint.constraint_type(name_or_constraint)
14
14
  type.build(*args, **opts, &block)
15
15
  else
16
- if name_or_constraint.respond_to?(:valid?) && name_or_constraint.respond_to?(:error_message)
17
- name_or_constraint
18
- else
19
- raise ParamsReadyError, "Not a constraint: #{name_or_constraint.class.name}"
20
- end
16
+ valid, missing_method = valid_constraint?(name_or_constraint, strategy)
17
+ on_constraint_invalid(missing_method) unless valid
18
+ name_or_constraint
21
19
  end
22
20
  new(constraint, strategy: strategy)
23
21
  end
24
22
 
23
+ def self.on_constraint_invalid(missing_method)
24
+ raise ParamsReadyError, "Not a valid constraint, '#{missing_method}' unimplemented"
25
+ end
26
+
27
+ def self.valid_constraint?(constraint, strategy)
28
+ return [false, 'valid?'] unless constraint.respond_to?(:valid?)
29
+ return [false, 'error_message'] unless constraint.respond_to?(:error_message)
30
+ return [true, nil] unless strategy == :clamp
31
+ return [false, 'clamp'] unless constraint.respond_to? :clamp
32
+
33
+ [true, nil]
34
+ end
35
+
25
36
  def initialize(constraint, strategy: :raise)
26
37
  @constraint = constraint
27
38
  @strategy = check_strategy(constraint, strategy)
@@ -33,7 +44,9 @@ module ParamsReady
33
44
  when :raise, :undefine
34
45
  strategy.to_sym
35
46
  when :clamp
36
- raise ParamsReadyError, 'Clamping not applicable' unless constraint.clamp?
47
+ if constraint.respond_to? :clamp?
48
+ raise ParamsReadyError, 'Clamping not applicable' unless constraint.clamp?
49
+ end
37
50
  strategy.to_sym
38
51
  else
39
52
  raise ParamsReadyError, "Unexpected constraint strategy #{strategy}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: params_ready
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Milsimer
@@ -174,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  - !ruby/object:Gem::Version
175
175
  version: '0'
176
176
  requirements: []
177
- rubygems_version: 3.1.4
177
+ rubygems_version: 3.1.2
178
178
  signing_key:
179
179
  specification_version: 4
180
180
  summary: Define controller interfaces in Rails