formulario 0.1.6 → 0.1.12

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: 273d0e050429479bb6a1a170ea7e0e86adf0fb445be85cbab978c4d0d6c7fc6f
4
- data.tar.gz: 80dbac663baaf27f6f0f7611b32b7c2b2aa70cb80f8eca8d79bb07f61c2cff9e
3
+ metadata.gz: 63bcd983db7081db6f273df3552b2a3c303a3ad523645ce708845261a9083eb2
4
+ data.tar.gz: c61c2edec3d80f0a63b379fff002a0466ff1fe1905c283375970da3c6b0fe338
5
5
  SHA512:
6
- metadata.gz: 41e50dd9124f6f809d6a2e1d6b513bf4b65795292e75a61ab7c3357d4da099e8a0b152359b967df803f7b9791b4a2bb71799fa713dd568d6c47c842415ad602e
7
- data.tar.gz: afaded423accd97236478ac2d490330c3d9dbb4178b010049aa6f9b99babd0f244ff98c8c5f19dd889a4ce2f4b3f6b4463dc9097196236f771c6d700ec33ee4c
6
+ metadata.gz: '09e7e4a46761f274b6f7666367e727a32af5dabe85c4d4ab7da4449b33c5bdfe226cb09aaa71c0750a30c227d3dec0afc00fce99be4270b7751df58ea0da4a0f'
7
+ data.tar.gz: f7ba1d511605f3fd4bb3cc993148729dab7d9d5dba54de53a853a016fe40b2ef2527c576ccf38fd28f3f624ce0f89c2340a22b037d2f104260fb1fa7a5f7e6b8
data/Gemfile.lock CHANGED
@@ -1,38 +1,38 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- formulario (0.1.6)
4
+ formulario (0.1.12)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  awesome_print (1.8.0)
10
- byebug (11.0.1)
11
- coderay (1.1.2)
12
- diff-lcs (1.3)
13
- docile (1.3.2)
14
- json (2.2.0)
15
- method_source (0.9.2)
16
- pry (0.12.2)
17
- coderay (~> 1.1.0)
18
- method_source (~> 0.9.0)
10
+ byebug (11.1.3)
11
+ coderay (1.1.3)
12
+ diff-lcs (1.4.4)
13
+ docile (1.3.5)
14
+ json (2.5.1)
15
+ method_source (1.0.0)
16
+ pry (0.14.0)
17
+ coderay (~> 1.1)
18
+ method_source (~> 1.0)
19
19
  pry-byebug (3.7.0)
20
20
  byebug (~> 11.0)
21
21
  pry (~> 0.10)
22
- rake (13.0.1)
22
+ rake (13.0.3)
23
23
  rspec (3.9.0)
24
24
  rspec-core (~> 3.9.0)
25
25
  rspec-expectations (~> 3.9.0)
26
26
  rspec-mocks (~> 3.9.0)
27
- rspec-core (3.9.0)
28
- rspec-support (~> 3.9.0)
29
- rspec-expectations (3.9.0)
27
+ rspec-core (3.9.3)
28
+ rspec-support (~> 3.9.3)
29
+ rspec-expectations (3.9.4)
30
30
  diff-lcs (>= 1.2.0, < 2.0)
31
31
  rspec-support (~> 3.9.0)
32
- rspec-mocks (3.9.0)
32
+ rspec-mocks (3.9.1)
33
33
  diff-lcs (>= 1.2.0, < 2.0)
34
34
  rspec-support (~> 3.9.0)
35
- rspec-support (3.9.0)
35
+ rspec-support (3.9.4)
36
36
  simplecov (0.17.1)
37
37
  docile (~> 1.1)
38
38
  json (>= 1.8, < 3)
@@ -52,4 +52,4 @@ DEPENDENCIES
52
52
  simplecov (~> 0.17.1)
53
53
 
54
54
  BUNDLED WITH
55
- 2.0.2
55
+ 2.2.3
@@ -7,6 +7,7 @@ require "formulario/fields/blank"
7
7
  require "formulario/fields/exceptional_value"
8
8
  require "formulario/fields/string"
9
9
  require "formulario/fields/integer"
10
+ require "formulario/fields/float"
10
11
  require "formulario/fields/boolean"
11
12
 
12
13
  require "formulario/fields/default_time_field"
@@ -3,21 +3,16 @@ module Formulario
3
3
  class Collection < Field
4
4
  include Enumerable
5
5
 
6
- def fields
7
- raw_value
8
- end
9
-
10
- def values
11
- fields.map(&:value)
12
- end
13
- alias :value :values
14
-
15
6
  def self.[](type)
16
7
  Class.new(self).tap { |typed_class|
17
- typed_class.field_types = TYPES.fetch(type)
8
+ typed_class.fields_type = type
18
9
  }
19
10
  end
20
11
 
12
+ def fields
13
+ raw_value
14
+ end
15
+
21
16
  def exceptional?
22
17
  fields.any?(&:exceptional?)
23
18
  end
@@ -26,20 +21,25 @@ module Formulario
26
21
  fields.each(&block)
27
22
  end
28
23
 
24
+ def values
25
+ map(&:value)
26
+ end
27
+
29
28
  private
30
29
 
31
- def self.build(raw_value)
32
- new Array(raw_value).map { |value|
33
- field_types.for(value)
34
- }
30
+ def self.build(raw_values)
31
+ new( Array(raw_values).map { |raw_value|
32
+ fields_type.for(raw_value)
33
+ }
34
+ )
35
35
  end
36
36
 
37
- def self.field_types
38
- @field_types ||= Field
37
+ def self.fields_type
38
+ @fields_type ||= Field
39
39
  end
40
40
 
41
- def self.field_types=(type)
42
- @field_types = type
41
+ def self.fields_type=(type)
42
+ @fields_type = type_for(type)
43
43
  end
44
44
  end
45
45
  end
@@ -9,7 +9,6 @@ module Formulario
9
9
  new(raw_value, reasons: reasons)
10
10
  end
11
11
 
12
-
13
12
  def exceptional?
14
13
  true
15
14
  end
@@ -26,6 +25,10 @@ module Formulario
26
25
  reasons.each(&block)
27
26
  end
28
27
 
28
+ def exceptional_class
29
+ self.class
30
+ end
31
+
29
32
  private
30
33
 
31
34
  def initialize(raw_value, reasons: Undefined)
@@ -43,7 +46,6 @@ module Formulario
43
46
 
44
47
  super(value)
45
48
  end
46
-
47
49
  end
48
50
  end
49
51
  end
@@ -16,6 +16,10 @@ module Formulario
16
16
  :array => Formulario::Field::Collection,
17
17
  }
18
18
 
19
+ def self.type_for(type)
20
+ TYPES.fetch(type)
21
+ end
22
+
19
23
  attr_reader :raw_value
20
24
 
21
25
  def self.for(raw_value)
@@ -67,6 +71,10 @@ module Formulario
67
71
  value == new_other.value
68
72
  end
69
73
 
74
+ def exceptional_class
75
+ ::Formulario::Field::ExceptionalValue
76
+ end
77
+
70
78
  private
71
79
 
72
80
  def self.build(raw_value)
@@ -0,0 +1,14 @@
1
+ module Formulario
2
+ class Field
3
+ class Float < Field
4
+ private
5
+
6
+ def self.build(raw_value)
7
+ new Float(raw_value)
8
+ rescue ArgumentError, TypeError => e
9
+ ExceptionalValue.new(raw_value, reasons: ['needs to represent a float'])
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -66,7 +66,7 @@ module Formulario
66
66
  validator_hash[:validator].message
67
67
  end
68
68
 
69
- ::Formulario::Field::ExceptionalValue
69
+ value.exceptional_class
70
70
  .new(current_value,
71
71
  reasons: message || validator_hash[:message])
72
72
  end
@@ -3,23 +3,29 @@ module Formulario
3
3
  attr_accessor :model
4
4
 
5
5
  def initialize(skip_validations: false, **params)
6
- __fields.each do |field_name, **options|
6
+ self.class.__fields.each do |field_name, **options|
7
7
  send("#{field_name}=", params.delete(field_name))
8
8
  end
9
9
 
10
10
  params.each do |k, v|
11
- send("#{k}=", v)
11
+ if self.respond_to?("#{k}=")
12
+ send("#{k}=", v)
13
+ end
12
14
  end
13
15
 
14
16
  __validate! unless skip_validations
15
17
  end
16
18
 
17
19
  def self.for(model, **rest)
18
- fields = __fields.keys.each_with_object({}) { |field_name, res|
19
- res[field_name] = model.send(field_name)
20
- }
21
-
22
- new(**fields.merge({model: model, **rest}))
20
+ if model
21
+ fields = __fields.keys.each_with_object({}) { |field_name, res|
22
+ res[field_name] = model.send(field_name)
23
+ }
24
+
25
+ new(**fields.merge({model: model, **rest}))
26
+ else
27
+ new(**rest)
28
+ end
23
29
  end
24
30
 
25
31
  def self.default
@@ -31,10 +37,6 @@ module Formulario
31
37
  new(skip_validations: true, **params)
32
38
  end
33
39
 
34
- def __fields
35
- @__fields ||= {}
36
- end
37
-
38
40
  def params
39
41
  __fields.each_with_object({}) { |field_info, res|
40
42
  field_name = field_info.first
@@ -74,12 +76,12 @@ module Formulario
74
76
 
75
77
  private
76
78
 
77
- def self.__fields
78
- @field ||= {}
79
+ def __fields
80
+ @__fields ||= {}
79
81
  end
80
82
 
81
- def __fields
82
- @__fields ||= self.class.__fields
83
+ def self.__fields
84
+ @field ||= {}
83
85
  end
84
86
 
85
87
  def self.field(field_name,
@@ -93,7 +95,12 @@ module Formulario
93
95
  validators: [],
94
96
  )
95
97
 
98
+ define_method("__set_field_#{field_name}") do
99
+ __fields[field_name.to_sym] ||= self.class.__fields[field_name.to_sym].dup
100
+ end
101
+
96
102
  define_method(field_name) do
103
+ send("__set_field_#{field_name}")
97
104
  __fields[field_name.to_sym].value
98
105
  end
99
106
 
@@ -104,6 +111,7 @@ module Formulario
104
111
  end
105
112
 
106
113
  define_method("#{field_name}=") do |raw_value|
114
+ send("__set_field_#{field_name}")
107
115
  __fields[field_name.to_sym].value = Field.type_for(field_type).for(
108
116
  send("set_#{field_name}", raw_value)
109
117
  )
@@ -1,3 +1,3 @@
1
1
  module Formulario
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.12"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formulario
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Federico Iachetti
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-26 00:00:00.000000000 Z
11
+ date: 2021-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -123,6 +123,7 @@ files:
123
123
  - lib/formulario/fields/default_time_field.rb
124
124
  - lib/formulario/fields/exceptional_value.rb
125
125
  - lib/formulario/fields/field.rb
126
+ - lib/formulario/fields/float.rb
126
127
  - lib/formulario/fields/form_field.rb
127
128
  - lib/formulario/fields/id.rb
128
129
  - lib/formulario/fields/integer.rb
@@ -140,7 +141,7 @@ metadata:
140
141
  homepage_uri: https://gitlab.com/fiachetti/formulario
141
142
  source_code_uri: https://gitlab.com/fiachetti/formulario
142
143
  changelog_uri: https://gitlab.com/fiachetti/formulario/blob/d46f675a18c4fbf1d16634a5da343a648af7cf0c/CHANGELOG.org
143
- post_install_message:
144
+ post_install_message:
144
145
  rdoc_options: []
145
146
  require_paths:
146
147
  - lib
@@ -155,8 +156,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
156
  - !ruby/object:Gem::Version
156
157
  version: '0'
157
158
  requirements: []
158
- rubygems_version: 3.0.6
159
- signing_key:
159
+ rubygems_version: 3.2.3
160
+ signing_key:
160
161
  specification_version: 4
161
162
  summary: Form objects for your project
162
163
  test_files: []