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

Class Method Details

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



23
24
25
26
27
# File 'lib/attributor/extras/field_selector.rb', line 23

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

.json_schema_typeObject



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

def self.json_schema_type
  :string
end

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



29
30
31
32
33
34
35
36
37
38
# File 'lib/attributor/extras/field_selector.rb', line 29

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



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

def self.native_type
  ::Hash
end

.valid_type?(value) ⇒ Boolean

Returns:



44
45
46
# File 'lib/attributor/extras/field_selector.rb', line 44

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



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

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