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


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

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

#indicatorObject


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

def indicator
  Nomen::Indicator[indicator_name]
end

#indicator=(item) ⇒ Object


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

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

#set_datatypeObject


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

def set_datatype
  unless indicator
    raise "Unknown indicator name in #{self.class.name}##{id}: #{indicator_name.inspect}"
  end
  self.indicator_datatype = indicator.datatype
end

#validate_valueObject


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

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


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

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

#value=(object) ⇒ Object

Write value into good place


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

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
    object = Charta.new_geometry(object).transform(:WGS84).to_rgeo
  end
  send("#{datatype}_value=", object)
end