Class: Praxis::Extensions::FieldSelection::FieldSelector

Inherits:
Object
  • Object
show all
Includes:
Attributor::Dumpable, Attributor::Type
Defined in:
lib/praxis/extensions/field_selection/field_selector.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ FieldSelector

Returns a new instance of FieldSelector.



69
70
71
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 69

def initialize(fields)
  @fields = fields
end

Class Attribute Details

.media_typeObject (readonly)

Returns the value of attribute media_type.



64
65
66
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 64

def media_type
  @media_type
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



67
68
69
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 67

def fields
  @fields
end

Class Method Details

.display_nameObject



14
15
16
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 14

def self.display_name
  'FieldSelector'
end

.dump(value, **opts) ⇒ Object



59
60
61
# 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) ⇒ Object



44
45
46
47
48
49
50
51
# 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

.familyObject



18
19
20
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 18

def self.family
  'string'
end

.for(media_type) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# 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_typeObject



10
11
12
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 10

def self.native_type
  self
end

.validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, _attribute = nil) ⇒ Object



53
54
55
56
57
# 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

Instance Method Details

#_dump(fields) ⇒ Object



78
79
80
81
82
83
84
85
86
# 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) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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) ⇒ Object



73
74
75
76
# 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) ⇒ Object



88
89
90
91
92
# 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