Module: Katalyst::Tables::Collection::Type::Helpers::Multiple
- Defined in:
- lib/katalyst/tables/collection/type/helpers/multiple.rb
Overview
Adds support for multiple: true
Instance Method Summary collapse
- #cast(value) ⇒ Object
- #default_value ⇒ Object
- #deserialize(value) ⇒ Object
- #initialize(multiple: false) ⇒ Object
- #multiple? ⇒ Boolean
- #serialize(value) ⇒ Object
- #to_param(value) ⇒ Object
- #value_for_multiple(value) ⇒ Object
Instance Method Details
#cast(value) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/katalyst/tables/collection/type/helpers/multiple.rb', line 20 def cast(value) return (multiple? ? [] : nil) if value.nil? if multiple? && value.is_a?(::Array) value_for_multiple(value.flat_map { |v| cast(v) }) elsif multiple? value_for_multiple(super) else super end end |
#default_value ⇒ Object
60 61 62 |
# File 'lib/katalyst/tables/collection/type/helpers/multiple.rb', line 60 def default_value multiple? ? [] : super end |
#deserialize(value) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/katalyst/tables/collection/type/helpers/multiple.rb', line 32 def deserialize(value) if multiple? && value.is_a?(::Array) value.map { |v| deserialize(v) }.flatten elsif multiple? [super].flatten.compact else super end end |
#initialize(multiple: false) ⇒ Object
10 11 12 13 14 |
# File 'lib/katalyst/tables/collection/type/helpers/multiple.rb', line 10 def initialize(multiple: false, **) super(**) @multiple = multiple end |
#multiple? ⇒ Boolean
16 17 18 |
# File 'lib/katalyst/tables/collection/type/helpers/multiple.rb', line 16 def multiple? @multiple end |
#serialize(value) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/katalyst/tables/collection/type/helpers/multiple.rb', line 42 def serialize(value) if multiple? && value.is_a?(::Array) value.map { |v| serialize(v) }.flatten else super end end |
#to_param(value) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/katalyst/tables/collection/type/helpers/multiple.rb', line 50 def to_param(value) if multiple? && value.is_a?(::Array) "[#{value.map { |v| to_param(v) }.flatten.join(', ')}]" else super end end |
#value_for_multiple(value) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/katalyst/tables/collection/type/helpers/multiple.rb', line 64 def value_for_multiple(value) case value when ::Array value.reject { |v| v.is_a?(::Range) } when ::Range value else [value] end end |