Sha256: 27b5e09f3d2212438a7ced19b59008b7be29fbf6ead80fdba7996999a1b4cc3c

Contents?: true

Size: 1.2 KB

Versions: 11

Compression:

Stored size: 1.2 KB

Contents

begin
  require 'parslet'
rescue LoadError
  warn "Attributor::FieldSelector requires the 'parslet' gem, which can not be found. " \
       "Please make sure it's in your Gemfile or installed in your system."
end

module Attributor
  class FieldSelector
    require 'attributor/extras/field_selector/parser'
    require 'attributor/extras/field_selector/transformer'

    include Attributor::Type

    def self.json_schema_type
      :string
    end

    def self.native_type
      ::Hash
    end

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

    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

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

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
attributor-7.1 lib/attributor/extras/field_selector.rb
attributor-7.0 lib/attributor/extras/field_selector.rb
attributor-6.5 lib/attributor/extras/field_selector.rb
attributor-6.4 lib/attributor/extras/field_selector.rb
attributor-6.3 lib/attributor/extras/field_selector.rb
attributor-6.2 lib/attributor/extras/field_selector.rb
attributor-6.1 lib/attributor/extras/field_selector.rb
attributor-6.0 lib/attributor/extras/field_selector.rb
attributor-5.7 lib/attributor/extras/field_selector.rb
attributor-5.6 lib/attributor/extras/field_selector.rb
attributor-5.5 lib/attributor/extras/field_selector.rb