Class: RSchema::Coercers::FixedHash::DefaultArraysToEmpty

Inherits:
Object
  • Object
show all
Defined in:
lib/rschema/coercers/fixed_hash/default_arrays_to_empty.rb

Overview

The HTTP standard says that when a form is submitted, all unchecked check boxes will not be sent to the server. That is, they will not be present at all in the params hash.

This class coerces these missing values into an empty array, where an array is expected.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fixed_hash_schema) ⇒ DefaultArraysToEmpty

Returns a new instance of DefaultArraysToEmpty.



20
21
22
23
# File 'lib/rschema/coercers/fixed_hash/default_arrays_to_empty.rb', line 20

def initialize(fixed_hash_schema)
  #TODO: make fixed hash attributes frozen, and eliminate dup
  @hash_attributes = fixed_hash_schema.attributes.map(&:dup)
end

Instance Attribute Details

#hash_attributesObject (readonly)

Returns the value of attribute hash_attributes.



14
15
16
# File 'lib/rschema/coercers/fixed_hash/default_arrays_to_empty.rb', line 14

def hash_attributes
  @hash_attributes
end

Class Method Details

.build(schema) ⇒ Object



16
17
18
# File 'lib/rschema/coercers/fixed_hash/default_arrays_to_empty.rb', line 16

def self.build(schema)
  new(schema)
end

Instance Method Details

#call(value) ⇒ Object



25
26
27
# File 'lib/rschema/coercers/fixed_hash/default_arrays_to_empty.rb', line 25

def call(value)
  Result.success(default_arrays_to_empty(value))
end

#will_affect?(value) ⇒ Boolean

Returns:



29
30
31
# File 'lib/rschema/coercers/fixed_hash/default_arrays_to_empty.rb', line 29

def will_affect?(value)
  keys_to_default(value).any?
end