Class: Chawk::Models::Range

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

Instance Method Summary collapse

Instance Method Details

#build_datasetObject



43
44
45
# File 'lib/range.rb', line 43

def build_dataset
  populate!
end

#build_subnodeObject



29
30
31
32
33
34
35
# File 'lib/range.rb', line 29

def build_subnode
  if subkey.to_s == ''
    self.subkey = parent_node.key + "/" + SecureRandom.hex.to_s
  end
  self.data_node = Chawk::Models::Node.create(key:subkey)
  grant_node_access
end

#grant_node_accessObject



23
24
25
26
27
# File 'lib/range.rb', line 23

def grant_node_access
  # TODO: vet this very carefully.
  # The only way to get here should be through an authorized source.
  self.data_node.access=:full
end

#order_is_correctObject



37
38
39
40
41
# File 'lib/range.rb', line 37

def order_is_correct
  if self.start_ts >= self.stop_ts
    errors.add(:stop_ts, "must be after start_ts.")
  end
end

#point_from_parent_point(now) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/range.rb', line 47

def point_from_parent_point(now)
  point = parent_node.points.where("observed_at <= :dt_to",{dt_to:now}).order(observed_at: :desc, id: :desc).first
  if point
    value = point.value
  else
    value = default || 0
  end
  data_node.points.create(observed_at:now, recorded_at:Time.now, value:value)
end

#populate!Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/range.rb', line 57

def populate!
  # TODO: Accounting hook
  # TODO: perform in callback (celluloid?)
  self.data_node.points.destroy_all
  step = 0.25 * self.beats
  now = (self.start_ts*4).round/4.to_f
  while now < self.stop_ts
    point = point_from_parent_point now
    now += step
  end
end

#reloadObject



18
19
20
21
# File 'lib/range.rb', line 18

def reload
  super
  grant_node_access
end