Class: Analysis

Inherits:
Ekylibre::Record::Base show all
Includes:
Attachable, Customizable
Defined in:
app/models/analysis.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2016 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: analyses

analysed_at            :datetime
analyser_id            :integer
created_at             :datetime         not null
creator_id             :integer
custom_fields          :jsonb
description            :text
geolocation            :geometry({:srid=>4326, :type=>"point"})
host_id                :integer
id                     :integer          not null, primary key
lock_version           :integer          default(0), not null
nature                 :string           not null
number                 :string           not null
product_id             :integer
reference_number       :string
retrieval_message      :string
retrieval_status       :string           default("ok"), not null
sampled_at             :datetime         not null
sampler_id             :integer
sampling_temporal_mode :string           default("instant"), not null
sensor_id              :integer
stopped_at             :datetime
updated_at             :datetime         not null
updater_id             :integer

Instance Method Summary collapse

Methods included from Customizable

#custom_value, #set_custom_value, #validate_custom_fields

Methods inherited from Ekylibre::Record::Base

#already_updated?, attr_readonly_with_conditions, #check_if_destroyable?, #check_if_updateable?, columns_definition, complex_scopes, customizable?, #customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, human_attribute_name_with_id, nomenclature_reflections, #old_record, #others, refers_to, scope_with_registration, simple_scopes, #updateable?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Returns value of an indicator if its name correspond to


122
123
124
125
126
127
# File 'app/models/analysis.rb', line 122

def method_missing(method_name, *args)
  if Nomen::Indicator.all.include?(method_name.to_s)
    return get(method_name, *args)
  end
  super
end

Instance Method Details

#get(indicator, *args) ⇒ Object


100
101
102
103
104
105
106
107
108
# File 'app/models/analysis.rb', line 100

def get(indicator, *args)
  unless indicator.is_a?(Nomen::Item) || indicator = Nomen::Indicator[indicator]
    raise ArgumentError, "Unknown indicator #{indicator.inspect}. Expecting one of them: #{Nomen::Indicator.all.sort.to_sentence}."
  end
  options = args.extract_options!
  items = self.items.where(indicator_name: indicator.name.to_s)
  return items.first.value if items.any?
  nil
end

#previousObject

Returns previous analysis. Works with sensors only.


117
118
119
# File 'app/models/analysis.rb', line 117

def previous
  sensor.analyses.where('sampled_at < ?', self.sampled_at).order(sampled_at: :desc).first
end

#read!(indicator, value, options = {}) ⇒ Object

Measure a product for a given indicator

Raises:

  • (ArgumentError)

86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/analysis.rb', line 86

def read!(indicator, value, options = {})
  unless indicator.is_a?(Nomen::Item) || indicator = Nomen::Indicator[indicator]
    raise ArgumentError, "Unknown indicator #{indicator.inspect}. Expecting one of them: #{Nomen::Indicator.all.sort.to_sentence}."
  end
  raise ArgumentError, 'Value must be given' if value.nil?
  options[:indicator_name] = indicator.name
  unless item = items.find_by(indicator_name: indicator.name)
    item = items.build(indicator_name: indicator.name)
  end
  item.value = value
  item.save!
  item
end

#status_changed?Boolean

Returns if status changed since previous call

Returns:

  • (Boolean)

111
112
113
114
# File 'app/models/analysis.rb', line 111

def status_changed?
  return true unless previous
  previous.retrieval_status != retrieval_status
end