granite-form 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +1 -1
  3. data/.github/workflows/ruby.yml +3 -5
  4. data/.rubocop.yml +26 -48
  5. data/.rubocop_todo.yml +304 -27
  6. data/Appraisals +1 -1
  7. data/CHANGELOG.md +15 -2
  8. data/gemfiles/{rails.5.0.gemfile → rails.7.1.gemfile} +3 -3
  9. data/granite-form.gemspec +6 -5
  10. data/lib/granite/form/active_record/associations.rb +4 -3
  11. data/lib/granite/form/config.rb +1 -1
  12. data/lib/granite/form/errors.rb +34 -32
  13. data/lib/granite/form/extensions.rb +2 -1
  14. data/lib/granite/form/model/associations/base.rb +6 -2
  15. data/lib/granite/form/model/associations/collection/embedded.rb +1 -1
  16. data/lib/granite/form/model/associations/collection/proxy.rb +3 -3
  17. data/lib/granite/form/model/associations/embeds_any.rb +1 -1
  18. data/lib/granite/form/model/associations/embeds_many.rb +15 -11
  19. data/lib/granite/form/model/associations/embeds_one.rb +9 -8
  20. data/lib/granite/form/model/associations/nested_attributes.rb +60 -32
  21. data/lib/granite/form/model/associations/persistence_adapters/active_record/referenced_proxy.rb +2 -1
  22. data/lib/granite/form/model/associations/persistence_adapters/active_record.rb +7 -6
  23. data/lib/granite/form/model/associations/persistence_adapters/base.rb +8 -4
  24. data/lib/granite/form/model/associations/references_any.rb +1 -1
  25. data/lib/granite/form/model/associations/references_many.rb +3 -2
  26. data/lib/granite/form/model/associations/references_one.rb +1 -1
  27. data/lib/granite/form/model/associations/reflections/base.rb +3 -2
  28. data/lib/granite/form/model/associations/reflections/embeds_any.rb +4 -4
  29. data/lib/granite/form/model/associations/reflections/embeds_many.rb +4 -1
  30. data/lib/granite/form/model/associations/reflections/embeds_one.rb +4 -1
  31. data/lib/granite/form/model/associations/reflections/references_any.rb +6 -6
  32. data/lib/granite/form/model/associations/reflections/references_many.rb +1 -1
  33. data/lib/granite/form/model/associations/reflections/references_one.rb +1 -1
  34. data/lib/granite/form/model/associations/validations.rb +6 -6
  35. data/lib/granite/form/model/associations.rb +6 -4
  36. data/lib/granite/form/model/attributes/attribute.rb +1 -0
  37. data/lib/granite/form/model/attributes/base.rb +9 -7
  38. data/lib/granite/form/model/attributes/reference_one.rb +1 -1
  39. data/lib/granite/form/model/attributes/reflections/base/build_type_definition.rb +2 -1
  40. data/lib/granite/form/model/attributes/reflections/represents/build_type_definition.rb +2 -2
  41. data/lib/granite/form/model/attributes/represents.rb +1 -1
  42. data/lib/granite/form/model/attributes.rb +21 -13
  43. data/lib/granite/form/model/conventions.rb +1 -1
  44. data/lib/granite/form/model/persistence.rb +1 -1
  45. data/lib/granite/form/model/primary.rb +1 -1
  46. data/lib/granite/form/model/representation.rb +4 -4
  47. data/lib/granite/form/model/scopes.rb +5 -5
  48. data/lib/granite/form/model/validations.rb +4 -3
  49. data/lib/granite/form/types/active_support/time_zone.rb +1 -1
  50. data/lib/granite/form/types/array.rb +1 -1
  51. data/lib/granite/form/types/big_decimal.rb +1 -1
  52. data/lib/granite/form/types/boolean.rb +1 -1
  53. data/lib/granite/form/types/date.rb +1 -1
  54. data/lib/granite/form/types/date_time.rb +1 -1
  55. data/lib/granite/form/types/dictionary.rb +1 -1
  56. data/lib/granite/form/types/float.rb +1 -1
  57. data/lib/granite/form/types/has_subtype.rb +1 -0
  58. data/lib/granite/form/types/hash_with_action_controller_parameters.rb +2 -2
  59. data/lib/granite/form/types/integer.rb +1 -1
  60. data/lib/granite/form/types/object.rb +2 -1
  61. data/lib/granite/form/types/string.rb +1 -1
  62. data/lib/granite/form/types/time.rb +1 -1
  63. data/lib/granite/form/types/uuid.rb +1 -1
  64. data/lib/granite/form/util.rb +1 -1
  65. data/lib/granite/form/version.rb +1 -1
  66. data/spec/granite/form/active_record/associations_spec.rb +35 -13
  67. data/spec/granite/form/config_spec.rb +8 -4
  68. data/spec/granite/form/model/associations/embeds_many_spec.rb +99 -51
  69. data/spec/granite/form/model/associations/embeds_one_spec.rb +48 -25
  70. data/spec/granite/form/model/associations/persistence_adapters/active_record_spec.rb +12 -7
  71. data/spec/granite/form/model/associations/references_many_spec.rb +51 -10
  72. data/spec/granite/form/model/associations/references_one_spec.rb +17 -6
  73. data/spec/granite/form/model/associations/reflections/embeds_many_spec.rb +51 -16
  74. data/spec/granite/form/model/associations/reflections/embeds_one_spec.rb +19 -9
  75. data/spec/granite/form/model/associations/reflections/references_many_spec.rb +67 -15
  76. data/spec/granite/form/model/associations/reflections/references_one_spec.rb +34 -11
  77. data/spec/granite/form/model/associations/validations_spec.rb +16 -5
  78. data/spec/granite/form/model/associations_spec.rb +28 -9
  79. data/spec/granite/form/model/attributes/attribute_spec.rb +33 -11
  80. data/spec/granite/form/model/attributes/base_spec.rb +26 -2
  81. data/spec/granite/form/model/attributes/reflections/attribute_spec.rb +1 -0
  82. data/spec/granite/form/model/attributes/reflections/base_spec.rb +1 -0
  83. data/spec/granite/form/model/attributes/reflections/represents/build_type_definition_spec.rb +3 -1
  84. data/spec/granite/form/model/attributes/reflections/represents_spec.rb +2 -2
  85. data/spec/granite/form/model/attributes/represents_spec.rb +2 -2
  86. data/spec/granite/form/model/attributes_spec.rb +76 -36
  87. data/spec/granite/form/model/dirty_spec.rb +3 -0
  88. data/spec/granite/form/model/persistence_spec.rb +15 -5
  89. data/spec/granite/form/model/primary_spec.rb +17 -2
  90. data/spec/granite/form/model/representation_spec.rb +13 -3
  91. data/spec/granite/form/model/scopes_spec.rb +8 -3
  92. data/spec/granite/form/model/validations/associated_spec.rb +20 -6
  93. data/spec/granite/form/model/validations/nested_spec.rb +30 -14
  94. data/spec/granite/form/model/validations_spec.rb +1 -1
  95. data/spec/granite/form/model_spec.rb +1 -0
  96. data/spec/granite/form/types/collection_spec.rb +2 -1
  97. data/spec/granite/form/types/date_spec.rb +1 -1
  98. data/spec/granite/form/types/date_time_spec.rb +0 -2
  99. data/spec/granite/form/types/dictionary_spec.rb +1 -0
  100. data/spec/granite/form/types/has_subtype_spec.rb +6 -1
  101. data/spec/granite/form/types/hash_with_action_controller_parameters_spec.rb +1 -1
  102. data/spec/granite/form/types/object_spec.rb +2 -0
  103. data/spec/granite/form/types/time_spec.rb +0 -2
  104. data/spec/granite/form/util_spec.rb +6 -3
  105. data/spec/support/active_record.rb +13 -0
  106. data/spec/support/shared/nested_attribute_examples.rb +110 -54
  107. metadata +28 -16
  108. data/gemfiles/rails.5.1.gemfile +0 -14
  109. data/gemfiles/rails.5.2.gemfile +0 -14
@@ -25,17 +25,17 @@ module Granite
25
25
 
26
26
  def klass
27
27
  @klass ||= if options[:data_source].present?
28
- options[:data_source].to_s.constantize
29
- else
30
- super
31
- end
28
+ options[:data_source].to_s.constantize
29
+ else
30
+ super
31
+ end
32
32
  end
33
33
 
34
- alias_method :data_source, :klass
34
+ alias data_source klass
35
35
 
36
36
  def persistence_adapter
37
37
  @persistence_adapter ||= self.class.persistence_adapter(klass)
38
- .new(data_source, options[:primary_key], @scope_proc)
38
+ .new(data_source, options[:primary_key], @scope_proc)
39
39
  end
40
40
 
41
41
  def read_source(object)
@@ -22,7 +22,7 @@ module Granite
22
22
 
23
23
  def reference_key
24
24
  @reference_key ||= options[:reference_key].presence.try(:to_sym) ||
25
- :"#{name.to_s.singularize}_#{primary_key.to_s.pluralize}"
25
+ :"#{name.to_s.singularize}_#{primary_key.to_s.pluralize}"
26
26
  end
27
27
  end
28
28
  end
@@ -24,7 +24,7 @@ module Granite
24
24
 
25
25
  def reference_key
26
26
  @reference_key ||= options[:reference_key].presence.try(:to_sym) ||
27
- :"#{name}_#{primary_key}"
27
+ :"#{name}_#{primary_key}"
28
28
  end
29
29
  end
30
30
  end
@@ -9,7 +9,7 @@ module Granite
9
9
  run_validations!
10
10
  end
11
11
 
12
- alias_method :validate_ancestry, :valid_ancestry?
12
+ alias validate_ancestry valid_ancestry?
13
13
 
14
14
  def invalid_ancestry?
15
15
  !valid_ancestry?
@@ -19,16 +19,16 @@ module Granite
19
19
  valid_ancestry? || raise_validation_error
20
20
  end
21
21
 
22
- private
22
+ private
23
23
 
24
24
  def validate_nested!
25
25
  association_names.each do |name|
26
26
  association = association(name)
27
27
  invalid_block = if association.reflection.klass.method_defined?(:invalid_ansestry?)
28
- ->(object) { object.invalid_ansestry? }
29
- else
30
- ->(object) { object.invalid? }
31
- end
28
+ ->(object) { object.invalid_ansestry? }
29
+ else
30
+ ->(object) { object.invalid? }
31
+ end
32
32
 
33
33
  Granite::Form::Model::Validations::NestedValidator
34
34
  .validate_nested(self, name, association.target, &invalid_block)
@@ -58,6 +58,7 @@ module Granite
58
58
  def alias_association(alias_name, association_name)
59
59
  reflection = reflect_on_association(association_name)
60
60
  raise ArgumentError, "Can't alias undefined association `#{attribute_name}` on #{self}" unless reflection
61
+
61
62
  reflection.class.generate_methods alias_name, generated_associations_methods
62
63
  self._association_aliases = _association_aliases.merge(alias_name.to_sym => reflection.name)
63
64
  reflection
@@ -72,7 +73,7 @@ module Granite
72
73
  _associations.keys
73
74
  end
74
75
 
75
- private
76
+ private
76
77
 
77
78
  def attributes_for_inspect
78
79
  (_associations.map do |name, reflection|
@@ -82,7 +83,7 @@ module Granite
82
83
 
83
84
  def generated_associations_methods
84
85
  @generated_associations_methods ||= const_set(:GeneratedAssociationsMethods, Module.new)
85
- .tap { |proxy| include proxy }
86
+ .tap { |proxy| include proxy }
86
87
  end
87
88
  end
88
89
 
@@ -92,15 +93,16 @@ module Granite
92
93
  end
93
94
  end
94
95
 
95
- alias_method :eql?, :==
96
+ alias eql? ==
96
97
 
97
98
  def association(name)
98
99
  reflection = self.class.reflect_on_association(name)
99
100
  return unless reflection
101
+
100
102
  (@_associations ||= {})[reflection.name] ||= reflection.build_association(self)
101
103
  end
102
104
 
103
- private
105
+ private
104
106
 
105
107
  def attributes_for_inspect
106
108
  (association_names.map do |name|
@@ -7,6 +7,7 @@ module Granite
7
7
 
8
8
  def write(value)
9
9
  return if readonly?
10
+
10
11
  pollute do
11
12
  write_value value
12
13
  end
@@ -4,6 +4,7 @@ module Granite
4
4
  module Attributes
5
5
  class Base
6
6
  attr_reader :type_definition
7
+
7
8
  delegate :type, :reflection, :owner, :enum, to: :type_definition
8
9
  delegate :name, :readonly, to: :reflection
9
10
 
@@ -20,6 +21,7 @@ module Granite
20
21
 
21
22
  def write(value)
22
23
  return if readonly?
24
+
23
25
  write_value value
24
26
  end
25
27
 
@@ -57,12 +59,12 @@ module Granite
57
59
 
58
60
  def inspect_attribute
59
61
  value = case read
60
- when Date, Time, DateTime
61
- %("#{read.to_s(:db)}")
62
- else
63
- inspection = read.inspect
64
- inspection.size > 100 ? inspection.truncate(50) : inspection
65
- end
62
+ when Date, Time, DateTime
63
+ %("#{read.to_formatted_s(:db)}")
64
+ else
65
+ inspection = read.inspect
66
+ inspection.size > 100 ? inspection.truncate(50) : inspection
67
+ end
66
68
  "#{name}: #{value}"
67
69
  end
68
70
 
@@ -89,7 +91,7 @@ module Granite
89
91
  end
90
92
  end
91
93
 
92
- private
94
+ private
93
95
 
94
96
  def remove_variable(*names)
95
97
  names.flatten.each do |name|
@@ -32,7 +32,7 @@ module Granite
32
32
  @value_cache
33
33
  end
34
34
 
35
- private
35
+ private
36
36
 
37
37
  def association
38
38
  @association ||= owner.association(reflection.association)
@@ -6,6 +6,7 @@ module Granite
6
6
  class Base
7
7
  class BuildTypeDefinition
8
8
  attr_reader :owner, :reflection
9
+
9
10
  delegate :name, to: :reflection
10
11
 
11
12
  def initialize(owner, reflection)
@@ -19,7 +20,7 @@ module Granite
19
20
  type_definition_for(type)
20
21
  end
21
22
 
22
- private
23
+ private
23
24
 
24
25
  def type
25
26
  reflection.options[:type]
@@ -19,7 +19,7 @@ module Granite
19
19
  end
20
20
  end
21
21
 
22
- private
22
+ private
23
23
 
24
24
  def reference
25
25
  owner.__send__(reflection.reference)
@@ -61,7 +61,7 @@ module Granite
61
61
 
62
62
  def convert_active_model_type_to_definition(attribute_type)
63
63
  type = attribute_type.try(:value_class) ||
64
- Associations::PersistenceAdapters::ActiveRecord::TYPES[attribute_type.type&.to_sym]
64
+ Associations::PersistenceAdapters::ActiveRecord::TYPES[attribute_type.type&.to_sym]
65
65
  type_definition_for(type) if type
66
66
  end
67
67
  end
@@ -24,7 +24,7 @@ module Granite
24
24
  end
25
25
  end
26
26
 
27
- private
27
+ private
28
28
 
29
29
  def reference
30
30
  owner.__send__(reflection.reference)
@@ -25,7 +25,8 @@ module Granite
25
25
 
26
26
  %w[attribute collection dictionary].each do |kind|
27
27
  define_singleton_method kind do |*args, &block|
28
- add_attribute("Granite::Form::Model::Attributes::Reflections::#{kind.camelize}".constantize, *args, &block)
28
+ add_attribute("Granite::Form::Model::Attributes::Reflections::#{kind.camelize}".constantize, *args,
29
+ &block)
29
30
  end
30
31
  end
31
32
  end
@@ -34,7 +35,7 @@ module Granite
34
35
  def add_attribute(reflection_class, *args, &block)
35
36
  reflection = reflection_class.build(self, generated_attributes_methods, *args, &block)
36
37
  self._attributes = _attributes.merge(reflection.name => reflection)
37
- should_define_dirty = (dirty? && reflection_class != Granite::Form::Model::Attributes::Reflections::Base)
38
+ should_define_dirty = dirty? && reflection_class != Granite::Form::Model::Attributes::Reflections::Base
38
39
  define_dirty(reflection.name, generated_attributes_methods) if should_define_dirty
39
40
  reflection
40
41
  end
@@ -42,7 +43,12 @@ module Granite
42
43
  def alias_attribute(alias_name, attribute_name)
43
44
  reflection = reflect_on_attribute(attribute_name)
44
45
  raise ArgumentError, "Unable to alias undefined attribute `#{attribute_name}` on #{self}" unless reflection
45
- raise ArgumentError, "Unable to alias base attribute `#{attribute_name}`" if reflection.class == Granite::Form::Model::Attributes::Reflections::Base
46
+
47
+ if reflection.class == Granite::Form::Model::Attributes::Reflections::Base
48
+ raise ArgumentError,
49
+ "Unable to alias base attribute `#{attribute_name}`"
50
+ end
51
+
46
52
  reflection.class.generate_methods alias_name, generated_attributes_methods
47
53
  self._attribute_aliases = _attribute_aliases.merge(alias_name.to_s => reflection.name)
48
54
  define_dirty alias_name, generated_attributes_methods if dirty?
@@ -85,7 +91,7 @@ module Granite
85
91
  self._sanitize = previous_sanitize
86
92
  end
87
93
 
88
- private
94
+ private
89
95
 
90
96
  def original_inspect
91
97
  Object.method(:inspect).unbind.bind(self).call
@@ -101,7 +107,7 @@ module Granite
101
107
  def generated_attributes_methods
102
108
  @generated_attributes_methods ||=
103
109
  const_set(:GeneratedAttributesMethods, Module.new)
104
- .tap { |proxy| include proxy }
110
+ .tap { |proxy| include proxy }
105
111
  end
106
112
 
107
113
  def inverted_attribute_aliases
@@ -117,14 +123,15 @@ module Granite
117
123
  end
118
124
 
119
125
  def ==(other)
120
- super || other.instance_of?(self.class) && other.attributes(false) == attributes(false)
126
+ super || (other.instance_of?(self.class) && other.attributes(false) == attributes(false))
121
127
  end
122
128
 
123
- alias_method :eql?, :==
129
+ alias eql? ==
124
130
 
125
131
  def attribute(name)
126
132
  reflection = self.class.reflect_on_attribute(name)
127
133
  return unless reflection
134
+
128
135
  initial_value = @initial_attributes.to_h.fetch(reflection.name, Granite::Form::UNDEFINED)
129
136
  @_attributes ||= {}
130
137
  @_attributes[reflection.name] ||= reflection.build_attribute(self, initial_value)
@@ -134,13 +141,13 @@ module Granite
134
141
  attribute(name).write(value)
135
142
  end
136
143
 
137
- alias_method :[]=, :write_attribute
144
+ alias []= write_attribute
138
145
 
139
146
  def read_attribute(name)
140
147
  attribute(name).read
141
148
  end
142
149
 
143
- alias_method :[], :read_attribute
150
+ alias [] read_attribute
144
151
 
145
152
  def read_attribute_before_type_cast(name)
146
153
  attribute(name).read_before_type_cast
@@ -162,7 +169,7 @@ module Granite
162
169
  assign_attributes(attrs)
163
170
  end
164
171
 
165
- alias_method :update_attributes, :update
172
+ alias update_attributes update
166
173
 
167
174
  def assign_attributes(attrs)
168
175
  attrs.each do |name, value|
@@ -172,12 +179,13 @@ module Granite
172
179
  if respond_to?("#{name}=") && !sanitize_value
173
180
  public_send("#{name}=", value)
174
181
  else
175
- logger.info("Ignoring #{sanitize_value ? 'primary' : 'undefined'} `#{name}` attribute value for #{self} during mass-assignment")
182
+ attribute_type = sanitize_value ? 'primary' : 'undefined'
183
+ logger.debug("Ignoring #{attribute_type} `#{name}` attribute value for #{self} during mass-assignment")
176
184
  end
177
185
  end
178
186
  end
179
187
 
180
- alias_method :attributes=, :assign_attributes
188
+ alias attributes= assign_attributes
181
189
 
182
190
  def sync_attributes
183
191
  attribute_names.each do |name|
@@ -198,7 +206,7 @@ module Granite
198
206
  super
199
207
  end
200
208
 
201
- private
209
+ private
202
210
 
203
211
  def attributes_for_inspect
204
212
  attribute_names(false).map do |name|
@@ -19,7 +19,7 @@ module Granite
19
19
  !persisted?
20
20
  end
21
21
 
22
- alias_method :new_object?, :new_record?
22
+ alias new_object? new_record?
23
23
 
24
24
  module ClassMethods
25
25
  def i18n_scope
@@ -30,7 +30,7 @@ module Granite
30
30
  false
31
31
  end
32
32
 
33
- private
33
+ private
34
34
 
35
35
  def mark_persisted!
36
36
  @persisted = true
@@ -29,7 +29,7 @@ module Granite
29
29
  alias_attribute :primary_attribute, _primary_name
30
30
  end
31
31
 
32
- alias_method :primary_attribute, :primary
32
+ alias primary_attribute primary
33
33
 
34
34
  def has_primary_attribute? # rubocop:disable Naming/PredicateName
35
35
  has_attribute? _primary_name
@@ -19,7 +19,7 @@ module Granite
19
19
  attrs = attrs.to_unsafe_hash if attrs.respond_to?(:to_unsafe_hash)
20
20
  attrs = attrs.stringify_keys
21
21
  represented_attrs = self.class.represented_names_and_aliases
22
- .each_with_object({}) do |name, result|
22
+ .each_with_object({}) do |name, result|
23
23
  result[name] = attrs.delete(name) if attrs.key?(name)
24
24
  end
25
25
 
@@ -29,7 +29,7 @@ module Granite
29
29
  end
30
30
  end
31
31
 
32
- alias_method :attributes=, :assign_attributes
32
+ alias attributes= assign_attributes
33
33
  end
34
34
 
35
35
  module ClassMethods
@@ -53,9 +53,9 @@ module Granite
53
53
  end
54
54
  end
55
55
 
56
- private
56
+ private
57
57
 
58
- def run_validations! #:nodoc:
58
+ def run_validations! # :nodoc:
59
59
  super
60
60
  emerge_represented_attributes_errors!
61
61
  errors.empty?
@@ -26,11 +26,14 @@ module Granite
26
26
 
27
27
  unless trust && source.is_a?(self.class)
28
28
  source.each do |entity|
29
- raise AssociationTypeMismatch.new(self.class._scope_model, entity.class) unless entity.is_a?(self.class._scope_model)
29
+ unless entity.is_a?(self.class._scope_model)
30
+ raise AssociationTypeMismatch.new(self.class._scope_model,
31
+ entity.class)
32
+ end
30
33
  end
31
34
  end
32
35
 
33
- super source
36
+ super(source)
34
37
  end
35
38
  end
36
39
 
@@ -38,7 +41,6 @@ module Granite
38
41
  super || self.class._scope_model.respond_to?(method)
39
42
  end
40
43
 
41
- # rubocop:disable Style/MethodMissing
42
44
  # rubocop-0.52.1 doesn't understand that `#respond_to_missing?` is defined above
43
45
  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
44
46
  def method_missing(method, *args, **kwargs, &block)
@@ -76,8 +78,6 @@ module Granite
76
78
  end
77
79
  end
78
80
  end
79
- # rubocop:enable Style/MethodMissing
80
-
81
81
  def with_scope
82
82
  previous_scope = self.class._scope_model.current_scope
83
83
  self.class._scope_model.current_scope = self
@@ -1,3 +1,6 @@
1
+ require_relative 'validations/nested'
2
+ require_relative 'validations/associated'
3
+
1
4
  module Granite
2
5
  module Form
3
6
  module Model
@@ -22,7 +25,7 @@ module Granite
22
25
  valid?(context) || raise_validation_error
23
26
  end
24
27
 
25
- protected
28
+ protected
26
29
 
27
30
  def raise_validation_error
28
31
  raise Granite::Form::ValidationError, self
@@ -31,5 +34,3 @@ module Granite
31
34
  end
32
35
  end
33
36
  end
34
-
35
- Dir[File.dirname(__FILE__) + '/validations/*.rb'].each { |file| require file }
@@ -5,7 +5,7 @@ module Granite
5
5
  module Types
6
6
  module ActiveSupport
7
7
  class TimeZone < Float
8
- private
8
+ private
9
9
 
10
10
  def typecast(value)
11
11
  case value
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class Array < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  if value.is_a?(::String)
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class BigDecimal < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  BigDecimal(Float(value).to_s) if value
@@ -29,7 +29,7 @@ module Granite
29
29
  MAPPING[value]
30
30
  end
31
31
 
32
- private
32
+ private
33
33
 
34
34
  def typecast(value)
35
35
  self.class.typecast(value)
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class Date < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  value.try(:to_date)
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class DateTime < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  value.try(:to_datetime)
@@ -10,7 +10,7 @@ module Granite
10
10
  value.transform_values { |v| subtype_definition.prepare(v) }.with_indifferent_access
11
11
  end
12
12
 
13
- private
13
+ private
14
14
 
15
15
  def to_hash(value)
16
16
  Hash[value]
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class Float < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  Float(value)
@@ -3,6 +3,7 @@ module Granite
3
3
  module Types
4
4
  class HasSubtype
5
5
  attr_reader :subtype_definition
6
+
6
7
  delegate :reflection, :owner, :type, :name, :enum, to: :subtype_definition
7
8
 
8
9
  def initialize(subtype_definition)
@@ -4,13 +4,13 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class HashWithActionControllerParameters < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  case value
11
11
  when ActionController::Parameters
12
12
  value.to_h if value.permitted?
13
- when ::Hash then
13
+ when ::Hash
14
14
  value
15
15
  end
16
16
  end
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class Integer < Float
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  super.try(:to_i)
@@ -5,6 +5,7 @@ module Granite
5
5
  module Types
6
6
  class Object
7
7
  attr_reader :reflection, :owner, :type
8
+
8
9
  delegate :name, to: :reflection
9
10
 
10
11
  def initialize(type, reflection, owner)
@@ -34,7 +35,7 @@ module Granite
34
35
  end.to_set
35
36
  end
36
37
 
37
- private
38
+ private
38
39
 
39
40
  def ensure_type(value)
40
41
  if value.instance_of?(type)
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class String < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  value.to_s
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class Time < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  value.is_a?(::String) && ::Time.zone ? ::Time.zone.parse(value) : value.try(:to_time)
@@ -4,7 +4,7 @@ module Granite
4
4
  module Form
5
5
  module Types
6
6
  class UUID < Object
7
- private
7
+ private
8
8
 
9
9
  def typecast(value)
10
10
  case value
@@ -41,7 +41,7 @@ module Granite
41
41
  end
42
42
  end
43
43
 
44
- private
44
+ private
45
45
 
46
46
  def evaluate_proc(value, *args)
47
47
  instance_exec(*args, &value)
@@ -1,5 +1,5 @@
1
1
  module Granite
2
2
  module Form
3
- VERSION = '0.4.0'.freeze
3
+ VERSION = '0.6.0'.freeze
4
4
  end
5
5
  end