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 14
def self.display_name
  'FieldSelector'
end
dump(value,**opts) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 59
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 44
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 18
def self.family
  'string'
end
for(media_type) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 22
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
load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **options) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 33
def self.load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **options)
  return value if value.kind_of?(self.native_type)

  if value.nil? || value.blank?
    self.new(true)
  else
    parsed = Attributor::FieldSelector.load(value)
    self.new(parsed)
  end
end
native_type() click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 10
def self.native_type
  self
end
new(fields) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 69
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 53
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 78
def _dump(fields)
  fields.each_with_object([]) do |(field, spec), array|
    if spec == true
      array << field
    else
      array << "#{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 94
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

    if field_spec.kind_of?(Hash)
      sub_context = context + [name]
      sub_attribute = type.attributes[name]
      sub_type = sub_attribute.type
      if sub_attribute.type.respond_to?(:member_attribute)
        sub_type = sub_type.member_type
      end
      errors.push(*_validate(sub_type,field_spec, sub_context))
    end
  end
  errors
end
dump(*args) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 73
def dump(*args)
  return '' if self.fields == true
  _dump(self.fields)
end
validate(context=Attributor::DEFAULT_ROOT_CONTEXT) click to toggle source
# File lib/praxis/extensions/field_selection/field_selector.rb, line 88
def validate(context=Attributor::DEFAULT_ROOT_CONTEXT)
  errors = []
  return errors if self.fields == true
  _validate(self.class.media_type, fields)
end