class Apia::FieldSet
Public Instance Methods
add(field)
click to toggle source
Add a new field to the fieldset
@param field [Apia::Field] @return [Apia::Field]
# File lib/apia/field_set.rb, line 16 def add(field) self[field.name] = field end
generate_hash(source, request: nil, path: [])
click to toggle source
Generate a hash for the fields that are defined on this object. It should receive the source object as well as a request
@param source [Object, Hash] @param request [Apia::Request] @param only [Array] @return [Hash]
# File lib/apia/field_set.rb, line 40 def generate_hash(source, request: nil, path: []) each_with_object({}) do |(_, field), hash| next unless field.include?(source, request) field_path = path + [field] next if request&.endpoint && !request&.endpoint&.include_field?(field_path) value = field.value(source, request: request, path: field_path) next if value == :skip hash[field.name.to_sym] = value end end
spec()
click to toggle source
Generate a default field spec for this field set based on the values provided for the include option.
@return [FieldSpec]
# File lib/apia/field_set.rb, line 58 def spec @spec ||= begin spec = each_with_object([]) do |(key, field), array| next if field.include == false if field.include.is_a?(::String) array << "#{key}[#{field.include}]" elsif field.type.object? || field.type.polymorph? array << "#{key}[*]" else array << key end end.join(',') FieldSpec.parse(spec) end end
validate(errors, object)
click to toggle source
Validate this field set and add errors as appropriate
@param errors [Apia::ManifestErrors] @param object [Object] @return [void]
# File lib/apia/field_set.rb, line 25 def validate(errors, object) each_value do |field| unless field.type.usable_for_field? errors.add object, 'InvalidFieldType', "Type for field #{field.name} must be a scalar, enum or object" end end end