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]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_valueObject

Returns the value of attribute default_value


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

def default_value
  @default_value
end

#display_nameObject

Returns the value of attribute display_name


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

def display_name
  @display_name
end

#keyObject

Returns the value of attribute key


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

def key
  @key
end

#kindObject

Returns the value of attribute kind


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

def kind
  @kind
end

#maxObject

Returns the value of attribute max


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

def max
  @max
end

#minObject

Returns the value of attribute min


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

def min
  @min
end

#requiredObject

Returns the value of attribute required


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

def required
  @required
end

#selectionObject

Returns the value of attribute selection


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

def selection
  @selection
end

#stepObject

Returns the value of attribute step


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

def step
  @step
end

Class Method Details

.for_request_types(request_types) ⇒ Object


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

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

  request_types.each do |request_type|
    request_type.request_attributes.each do |att|
      attributes[att.name] &= att.to_field_info(request_type)
    end
  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)

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

def &(other)
  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
75
76
77
# 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


79
80
81
# File 'app/models/field_info.rb', line 79

def kind_priority
  KIND.index(kind)
end

#parametersObject


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

def parameters
  { min: min, max: max, step: 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


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

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

#valueObject


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

def value
  default_value || ''
end