Class: FieldType

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
FieldTypePlanner
Defined in:
app/models/field_type.rb

Overview

Meta type of FieldValue. An Item type, or parameter type of the inputs/outputs of an OperationType or of the properties of a SampleType. FieldType holds a list of allowable values or objects for defining Operations or Samples that would satisfy the specifications of their respective abstract OperationType or SampleType.

Instance Method Summary collapse

Instance Method Details

#allowable_object_typesObject



77
78
79
80
81
82
83
# File 'app/models/field_type.rb', line 77

def allowable_object_types
  if item?
    allowable_field_types.collect(&:object_type)
  else
    []
  end
end

#allowable_sample_typesObject



64
65
66
67
68
69
70
# File 'app/models/field_type.rb', line 64

def allowable_sample_types
  if sample?
    allowable_field_types.collect(&:sample_type)
  else
    []
  end
end

#allowed?(val) ⇒ Boolean

Check whether the given value is allowed by this field type.

Parameters:

  • val (Object)

    the potential value to check

Returns:

  • (Boolean)

    whether or not the value would be allowed



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/field_type.rb', line 51

def allowed?(val)
  case ftype
  when 'string', 'url'
    val.is_a?(String)
  when 'number'
    val.is_a?(Integer) || val.is_a?(Float)
  when 'sample'
    allowable_field_types.collect { |aft| aft.sample_type.id }.member? val.sample_type_id
  when 'item'
    allowable_field_types.collect { |aft| aft.object_type.id }.member? val.object_type_id
  end
end

#as_json(options = {}) ⇒ Object



117
118
119
120
121
122
123
# File 'app/models/field_type.rb', line 117

def as_json(options = {})
  if options[:plain]
    super
  else
    super include: [allowable_field_types: { include: %i[sample_type object_type] }]
  end
end

#destroyObject



42
43
44
45
# File 'app/models/field_type.rb', line 42

def destroy
  allowable_field_types.each(&:destroy)
  super
end

#empty?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/field_type.rb', line 113

def empty?
  allowable_sample_types.select { |ast| ast }.empty?
end

#exportObject

Creates a hash for this field_type.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/models/field_type.rb', line 163

def export
  {
    ftype: type,
    role: role,
    name: name,
    sample_types: allowable_field_types.collect { |aft| aft.sample_type ? aft.sample_type.name : nil },
    object_types: allowable_field_types.collect { |aft| aft.object_type ? aft.object_type.name : nil },
    part: part ? true : false,
    array: array ? true : false,
    routing: routing,
    preferred_operation_type_id: preferred_operation_type_id,
    preferred_field_type_id: preferred_field_type_id,
    choices: choices
  }
end

#has_sample_typeObject



72
73
74
75
# File 'app/models/field_type.rb', line 72

def has_sample_type
  sample_types = allowable_sample_types.select { |st| st }
  !sample_types.empty?
end

#has_type(typename) ⇒ Object



89
90
91
# File 'app/models/field_type.rb', line 89

def has_type(typename)
  ftype == typename
end

#inconsistencies(raw_field_type, parent_name) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/field_type.rb', line 125

def inconsistencies(raw_field_type, parent_name)

  results = []

  results << "#{parent_name} field '#{name}' and imported field #{raw_field_type[:name]} should match." unless raw_field_type[:name] == name
  results << "#{parent_name} field '#{name}' has different choices than important field with same name" unless raw_field_type[:choices] == choices
  results << "#{parent_name} field '#{name}' has array = #{!!array} but imported field of the same name has array = #{!!raw_field_type[:array]}." unless !!raw_field_type[:array] == !!array
  unless !!raw_field_type[:part] == !!part
    # TODO: make sure the following line is correct.  previously had a && at the end
    results << "#{parent_name} field '#{name}' has required = #{!!required} but imported field of the same name has required = #{!!raw_field_type[:required]}." unless !!raw_field_type[:required] == !!required
    results << "#{parent_name} field '#{name}' has part = #{!!part} but imported field of the same name has part = #{!!raw_field_type[:part]}."
  end
  results << "#{parent_name} field '#{name}' has type is #{!!ftype} but imported field of the same name has type = #{!!raw_field_type[:ftype]}." unless raw_field_type[:ftype] == ftype
  results << "#{parent_name} field '#{name}' has role is #{!!role} but imported field of the same name has role = #{!!raw_field_type[:role]}" unless raw_field_type[:role] == role
  results << "#{parent_name} field '#{name}' has routing symbol is #{!!routing} but imported field of the same name has routing symbol = #{!!raw_field_type[:routing]}." unless raw_field_type[:role] == role

  if sample?

    l1 = allowable_field_types.collect do |aft|
      [
        aft.sample_type ? aft.sample_type.name : nil,
        aft.object_type ? aft.object_type.name : nil
      ]
    end

    a = raw_field_type[:sample_types] || []
    b = raw_field_type[:object_types] || []
    l2 = a.zip b
    msg = "#{parent_name}: Field '#{name}'' has different associated sample and object types than does the imported field by the same name."
    results << msg unless (l1.difference(l2) + l2.difference(l1)).empty?

  end

  results

end

#item?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'app/models/field_type.rb', line 109

def item?
  has_type('item')
end

#json?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'app/models/field_type.rb', line 105

def json?
  has_type('json')
end

#nameString

Gets name of FieldType.

Returns:

  • (String)

    the name of the FieldValue, as in "Forward Primer"



22
# File 'app/models/field_type.rb', line 22

attr_accessible :name

#number?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/models/field_type.rb', line 101

def number?
  has_type('number')
end

#parent_classString

Gets the name of the class of the object to which this FieldType belongs.

Returns:

  • (String)

    class of parent object, as in "SampleType"



32
# File 'app/models/field_type.rb', line 32

attr_accessible :parent_class

#parent_idFixnum

Gets the id of the object to which this FieldType belongs.

Returns:



27
# File 'app/models/field_type.rb', line 27

attr_accessible :parent_id

#sample?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/models/field_type.rb', line 97

def sample?
  has_type('sample')
end

#string?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/field_type.rb', line 93

def string?
  has_type('string')
end

#typeObject



85
86
87
# File 'app/models/field_type.rb', line 85

def type
  ftype
end