Class: FieldInfo

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/field_info.rb

Overview

There no subclasses at the moment, because we want to keep things simple

(especially no need to use a factory)

Defined Under Namespace

Modules: NullFieldInfo

Constant Summary collapse

SELECTION =
'Selection'
TEXT =
'Text'
BOOLEAN =
'Boolean'
NUMERIC =
'Numeric'
KIND =

Sorted in order of least restrictiveness

[TEXT, NUMERIC, SELECTION, BOOLEAN].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_valueObject

Returns the value of attribute default_value.



24
25
26
# File 'app/models/field_info.rb', line 24

def default_value
  @default_value
end

#display_nameObject

Returns the value of attribute display_name.



24
25
26
# File 'app/models/field_info.rb', line 24

def display_name
  @display_name
end

#keyObject

Returns the value of attribute key.



24
25
26
# File 'app/models/field_info.rb', line 24

def key
  @key
end

#kindObject

Returns the value of attribute kind.



24
25
26
# File 'app/models/field_info.rb', line 24

def kind
  @kind
end

#maxObject

Returns the value of attribute max.



24
25
26
# File 'app/models/field_info.rb', line 24

def max
  @max
end

#minObject

Returns the value of attribute min.



24
25
26
# File 'app/models/field_info.rb', line 24

def min
  @min
end

#requiredObject

Returns the value of attribute required.



24
25
26
# File 'app/models/field_info.rb', line 24

def required
  @required
end

#selectionObject

Returns the value of attribute selection.



24
25
26
# File 'app/models/field_info.rb', line 24

def selection
  @selection
end

#stepObject

Returns the value of attribute step.



24
25
26
# File 'app/models/field_info.rb', line 24

def step
  @step
end

Class Method Details

.for_request_types(request_types) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'app/models/field_info.rb', line 26

def self.for_request_types(request_types)
  attributes = Hash.new(NullFieldInfo)

  request_types.each do |request_type|
    request_type.request_attributes.each { |att| attributes[att.name] &= att.to_field_info(request_type) }
  end

  attributes.values
end

Instance Method Details

#&(other) ⇒ FieldInfo

Combine two field infos to one with the most limited options

Parameters:

  • other (FieldInfo)

    The FieldInfo to combine with

Returns:

  • (FieldInfo)

    The new, more restrictive field info

Raises:

  • (StandardError)


57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/field_info.rb', line 57

def &(other) # rubocop:todo Metrics/AbcSize
  raise StandardError, "Attempted to combine #{key} with #{other.key} FieldInfos" unless key == other.key

  dup.tap do |combined|
    # Use set selector to filter to those common to all attributes
    combined.selection = [combined.selection, other.selection].compact.reduce(&:&)
    combined.required ||= other.required

    # If kinds differ, we want to select the most restrictive (eg. selection over numeric)
    combined.kind = other.kind if combined.kind_priority < other.kind_priority
    combined.default_value ||= other.default_value
  end
end

#==(other) ⇒ Object



71
72
73
74
# File 'app/models/field_info.rb', line 71

def ==(other)
  display_name == other.display_name && key == other.key && kind == other.kind &&
    default_value == other.default_value && selection == other.selection
end

#kind_priorityObject



76
77
78
# File 'app/models/field_info.rb', line 76

def kind_priority
  KIND.index(kind)
end

#parametersObject



44
45
46
# File 'app/models/field_info.rb', line 44

def parameters
  { min:, max:, step: }
end

#parameters=(parameters) ⇒ Object

Parameters were only ever used to hold selection This provides legacy support for a handful of serialized field infos in the database



39
40
41
# File 'app/models/field_info.rb', line 39

def parameters=(parameters)
  self.selection = parameters&.fetch(:selection, [])
end

#valueObject



48
49
50
# File 'app/models/field_info.rb', line 48

def value
  default_value || ''
end