Class: PlantWatchdog::Model::Metadata

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/plantwatchdog/model.rb

Constant Summary collapse

@@description_to_dataclass =

has_one :plant

{}

Instance Method Summary collapse

Instance Method Details

#dataclassObject



362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/plantwatchdog/model.rb', line 362

def dataclass
  logger.debug "Accessing dataclass for metadata " + description.to_s
  if ! @@description_to_dataclass.has_key? description then
    logger.debug "Creating new dataclass for metadata " + description.to_s
    dataclass = Class.new(Model::Measurement)
    description.each { |args| dataclass.column *args }
    logger.debug dataclass.class
    dataclass. = self
    @@description_to_dataclass[description] = dataclass
  end
  @@description_to_dataclass[description]
end

#descriptionObject



375
376
377
378
# File 'lib/plantwatchdog/model.rb', line 375

def description
  serialized = read_attribute("description")
  ActiveSupport::JSON.decode(serialized) unless serialized.nil?
end

#description=(arg) ⇒ Object



380
381
382
383
384
385
386
387
# File 'lib/plantwatchdog/model.rb', line 380

def description=(arg)
  # the description is immutable
  # once an instance of metadata has been saved, no update is allowed
  raise "description is immutable" if read_attribute("description")
  serialized = arg
  serialized = ActiveSupport::JSON.encode(serialized) unless serialized.is_a? String
  write_attribute("description",serialized)
end