Module: ReadingStorable

Extended by:
ActiveSupport::Concern
Included in:
AnalysisItem, InterventionParameterReading, ProductNatureVariantReading, ProductReading
Defined in:
app/models/concerns/reading_storable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#absolutize_measureObject


41
42
43
44
45
# File 'app/models/concerns/reading_storable.rb', line 41

def absolutize_measure
  if indicator_datatype_measure? && measure_value.is_a?(Measure)
    self.absolute_measure_value = measure_value.in(indicator.unit)
  end
end

#indicatorObject


80
81
82
# File 'app/models/concerns/reading_storable.rb', line 80

def indicator
  Nomen::Indicator[indicator_name]
end

#indicator=(item) ⇒ Object


84
85
86
# File 'app/models/concerns/reading_storable.rb', line 84

def indicator=(item)
  self.indicator_name = item.name
end

#set_datatypeObject


36
37
38
39
# File 'app/models/concerns/reading_storable.rb', line 36

def set_datatype
  return unless indicator
  self.indicator_datatype = indicator.datatype
end

#validate_valueObject


47
48
49
50
51
52
# File 'app/models/concerns/reading_storable.rb', line 47

def validate_value
  if indicator_datatype_measure?
    # TODO: Check unit
    # errors.add(:unit, :invalid) if unit.dimension != indicator.unit.dimension
  end
end

#valueObject

Read value from good place


55
56
57
58
# File 'app/models/concerns/reading_storable.rb', line 55

def value
  datatype = indicator_datatype || indicator.datatype
  send(datatype.to_s + '_value')
end

#value=(object) ⇒ Object

Write value into good place


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/concerns/reading_storable.rb', line 61

def value=(object)
  datatype = (indicator_datatype || indicator.datatype).to_sym
  if object.is_a?(String)
    if datatype == :measure
      object = Measure.new(object)
    elsif datatype == :boolean
      object = %w[1 t true y yes ok].include?(object.to_s.strip.downcase)
    elsif datatype == :decimal
      object = object.to_d
    elsif datatype == :integer
      object = object.to_i
    end
  end
  if datatype == :geometry || datatype == :multi_polygon || datatype == :point
    object = Charta.new_geometry(object)
  end
  send("#{datatype}_value=", object)
end