Class: Attributor::FieldSelector

Inherits:
Object
  • Object
show all
Includes:
Type
Defined in:
lib/attributor/extras/field_selector.rb,
lib/attributor/extras/field_selector/parser.rb,
lib/attributor/extras/field_selector/transformer.rb

Defined Under Namespace

Classes: Parser, Transformer

Class Method Summary collapse

Methods included from Type

included

Class Method Details

.example(_context = nil, options: {}) ⇒ Object



19
20
21
22
23
# File 'lib/attributor/extras/field_selector.rb', line 19

def self.example(_context = nil, options: {})
  3.times.each_with_object([]) do |_i, array|
    array << /\w{5,8}/.gen
  end.join(',')
end

.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/attributor/extras/field_selector.rb', line 25

def self.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
  return nil if value.nil?
  return value if valid_type? value
  return {} if value.empty?

  parsed = Parser.new.parse(value)
  Transformer.new.apply(parsed)
rescue
  raise CoercionError, context: context, from: value.class, to: self, value: value
end

.native_typeObject



15
16
17
# File 'lib/attributor/extras/field_selector.rb', line 15

def self.native_type
  ::Hash
end

.valid_type?(value) ⇒ Boolean

Returns:



40
41
42
# File 'lib/attributor/extras/field_selector.rb', line 40

def self.valid_type?(value)
  return true if value.is_a?(native_type) || value.is_a?(self.class)
end

.validate(_value, _context = nil, _attribute) ⇒ Object



36
37
38
# File 'lib/attributor/extras/field_selector.rb', line 36

def self.validate(_value, _context = nil, _attribute)
  [].freeze
end