class Praxis::Extensions::FieldSelection::FieldSelector

Attributes

media_type[R]
fields[R]

Private Class Methods

display_name() click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 18
def self.display_name
  'FieldSelector'
end
dump(value, **_opts) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 64
def self.dump(value, **_opts)
  self.load(value).dump
end
example(context = Attributor::DEFAULT_ROOT_CONTEXT, **options) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 48
def self.example(context = Attributor::DEFAULT_ROOT_CONTEXT, **options)
  fields = if media_type
             media_type.attributes.keys.sample(3).join(',')
           else
             Attributor::FieldSelector.example(context, **options)
           end
  self.load(fields)
end
family() click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 22
def self.family
  'string'
end
for(media_type) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 26
def self.for(media_type)
  unless media_type < Praxis::MediaType
    raise ArgumentError, "Invalid type: #{media_type.name} for FieldSelector. " \
                         'Must be a subclass of MediaType'
  end

  ::Class.new(self) do
    @media_type = media_type
  end
end
json_schema_type() click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 10
def self.json_schema_type
  :string
end
load(value, _context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 37
def self.load(value, _context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
  return value if value.is_a?(native_type)

  if value.nil? || value.blank?
    new(true)
  else
    parsed = Attributor::FieldSelector.load(value)
    new(parsed)
  end
end
native_type() click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 14
def self.native_type
  self
end
new(fields) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 74
def initialize(fields)
  @fields = fields
end
validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, _attribute = nil) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 57
def self.validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, _attribute = nil)
  return [] unless media_type

  instance = self.load(value, context)
  instance.validate(context)
end

Private Instance Methods

_dump(fields) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 84
def _dump(fields)
  fields.each_with_object([]) do |(field, spec), array|
    array << if spec == true
               field
             else
               "#{field}{#{_dump(spec)}}"
             end
  end.join(',')
end
_validate(type, fields, context = Attributor::DEFAULT_ROOT_CONTEXT) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 101
def _validate(type, fields, context = Attributor::DEFAULT_ROOT_CONTEXT)
  errors = []
  fields.each do |name, field_spec|
    unless type.attributes.key?(name)
      errors << "Attribute with name #{name} not found in #{Attributor.type_name(type)}"
      next
    end

    next unless field_spec.is_a?(Hash)

    sub_context = context + [name]
    sub_attribute = type.attributes[name]
    sub_type = sub_attribute.type
    sub_type = sub_type.member_type if sub_attribute.type.respond_to?(:member_attribute)
    errors.push(*_validate(sub_type, field_spec, sub_context))
  end
  errors
end
dump(*_args) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 78
def dump(*_args)
  return '' if fields == true

  _dump(fields)
end
validate(_context = Attributor::DEFAULT_ROOT_CONTEXT) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 94
def validate(_context = Attributor::DEFAULT_ROOT_CONTEXT)
  errors = []
  return errors if fields == true

  _validate(self.class.media_type, fields)
end