Class: Praxis::Extensions::FieldSelection::FieldSelector
- Inherits:
-
Object
- Object
- Praxis::Extensions::FieldSelection::FieldSelector
- Includes:
- Attributor::Dumpable, Attributor::Type
- Defined in:
- lib/praxis/extensions/field_selection/field_selector.rb
Class Attribute Summary collapse
-
.media_type ⇒ Object
readonly
Returns the value of attribute media_type.
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Class Method Summary collapse
- .display_name ⇒ Object
- .dump(value, **_opts) ⇒ Object
- .example(context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object
- .family ⇒ Object
- .for(media_type) ⇒ Object
- .json_schema_type ⇒ Object
- .load(value, _context = Attributor::DEFAULT_ROOT_CONTEXT, **_options) ⇒ Object
- .native_type ⇒ Object
- .validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, _attribute = nil) ⇒ Object
Instance Method Summary collapse
- #_dump(fields) ⇒ Object
- #_validate(type, fields, context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object
- #dump(*_args) ⇒ Object
-
#initialize(fields) ⇒ FieldSelector
constructor
A new instance of FieldSelector.
- #validate(_context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object
Constructor Details
#initialize(fields) ⇒ FieldSelector
Returns a new instance of FieldSelector.
74 75 76 |
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 74 def initialize(fields) @fields = fields end |
Class Attribute Details
.media_type ⇒ Object (readonly)
Returns the value of attribute media_type.
69 70 71 |
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 69 def media_type @media_type end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
72 73 74 |
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 72 def fields @fields end |
Class Method Details
.display_name ⇒ Object
18 19 20 |
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 18 def self.display_name 'FieldSelector' end |
.dump(value, **_opts) ⇒ Object
64 65 66 |
# 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) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 48 def self.example(context = Attributor::DEFAULT_ROOT_CONTEXT, **) fields = if media_type media_type.attributes.keys.sample(3).join(',') else Attributor::FieldSelector.example(context, **) end self.load(fields) end |
.family ⇒ Object
22 23 24 |
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 22 def self.family 'string' end |
.for(media_type) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# 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 ⇒ Object
10 11 12 |
# 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) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 37 def self.load(value, _context = Attributor::DEFAULT_ROOT_CONTEXT, **) 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 ⇒ Object
14 15 16 |
# File 'lib/praxis/extensions/field_selection/field_selector.rb', line 14 def self.native_type self end |
.validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, _attribute = nil) ⇒ Object
57 58 59 60 61 62 |
# 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 |
Instance Method Details
#_dump(fields) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# 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) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# 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) ⇒ Object
78 79 80 81 82 |
# 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) ⇒ Object
94 95 96 97 98 99 |
# 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 |