Class: Chawk::Models::Node

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

Overview

The Node, where most Chawk:Node information is persisted..

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#accessObject

Returns the value of attribute access.



37
38
39
# File 'lib/node.rb', line 37

def access
  @access
end

#agentObject

Returns the value of attribute agent.



28
29
30
# File 'lib/node.rb', line 28

def agent
  @agent
end

Instance Method Details

#_add(args, type, options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/node.rb', line 89

def _add(args, type, options={})
  check_write_access
  ni = NodeInvalidator.new(self)
  options[:observed_at] ? dt = options[:observed_at] : dt = Time.now
  _unravel(args) do |arg|
    case type
    when :point
      ni << point_recognizer(arg, dt, options)
    when :value
      ni << value_recognizer(arg, dt, options)
    end
  end
  ni.invalidate!
end

#_insert_point(val, ts, options = {}) ⇒ Object



118
119
120
121
# File 'lib/node.rb', line 118

def _insert_point(val,ts,options={})        
  self.points.create(_prepare_insert(val, ts, options))
  ts
end

#_insert_point_array(item, options) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/node.rb', line 135

def _insert_point_array(item,options)
  if item.length == 2 && item[0].is_a?(Integer)
    _insert_point item[0],item[1], options
  else
    raise ArgumentError, "Array Items must be in [value,timestamp] format. #{item.inspect}"
  end
end

#_insert_point_hash(item, ts, options) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/node.rb', line 123

def _insert_point_hash(item,ts,options)
  if item['v'] && item['v'].is_a?(Integer)
    if item['t']
      _insert_point item['v'],item['t'], options
    else
      _insert_point item['v'],ts, options
    end
  else
    raise ArgumentError, "Hash must have 'v' key set to proper type.. #{item.inspect}"
  end
end

#_insert_point_string(item, ts, options) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/node.rb', line 143

def _insert_point_string(item,ts,options)
  if item.length > 0 && item =~ /\A[-+]?[0-9]+/
    _insert_point item.to_i,ts, options
  else
    raise ArgumentError, "String Items must represent Integer. #{item.inspect}"
  end
end

#_insert_value(val, ts, options = {}) ⇒ Object



65
66
67
68
# File 'lib/node.rb', line 65

def _insert_value(val,ts,options={})
  self.values.create(_prepare_insert(val, ts, options))
  ts
end

#_prepare_insert(val, ts, options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/node.rb', line 53

def _prepare_insert(val, ts, options)
  values = {value:val,observed_at:ts.to_f}
  if options[:meta]
    if options[:meta].is_a?(Hash)
      values[:meta] = options[:meta].to_json
    else
      raise ArgumentError, "Meta must be a JSON-representable Hash. #{options[:meta].inspect}"
    end
  end
  values
end

#_range(dt_from, dt_to, coll, options = {}) ⇒ Object



203
204
205
206
# File 'lib/node.rb', line 203

def _range(dt_from, dt_to, coll, options={})
  check_read_access
  ret = coll.where("observed_at >= :dt_from AND  observed_at <= :dt_to",{dt_from:dt_from.to_f,dt_to:dt_to.to_f}, limit:1000,order:"observed_at asc, id asc")
end

#_unravel(items) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/node.rb', line 79

def _unravel(items)
  if items.is_a?(Array)
    items.each do |item|
      yield item
    end
  else
    yield items
  end
end

#add_points(args, options = {}) ⇒ Object

Add an item or an array of items (one at a time) to the datastore.

Parameters:

  • args (Object, Array of Objects)
  • options (Hash) (defaults to: {})

    You can also pass in :meta and :timestamp



114
115
116
# File 'lib/node.rb', line 114

def add_points(args,options={})
  _add(args,:point,options)
end

#add_values(args, options = {}) ⇒ Object

Add an item or an array of items (one at a time) to the datastore.

Parameters:

  • args (Object, Array of Objects)
  • options (Hash) (defaults to: {})

    You can also pass in :meta and :timestamp



107
108
109
# File 'lib/node.rb', line 107

def add_values(args,options={})
  _add(args,:value, options)
end

#check_admin_accessObject



178
179
180
181
182
# File 'lib/node.rb', line 178

def check_admin_access
  unless [:full,:admin,:read].include? @access
    raise SecurityError,"You do not have admin access to this node."
  end
end

#check_read_accessObject



172
173
174
175
176
# File 'lib/node.rb', line 172

def check_read_access
  unless [:full,:admin,:read].include? @access
    raise SecurityError,"You do not have read access to this node."
  end
end

#check_write_accessObject



166
167
168
169
170
# File 'lib/node.rb', line 166

def check_write_access
  unless [:full,:admin,:write].include? @access
    raise SecurityError,"You do not have write access to this node."
  end
end

#clear_points!Object



48
49
50
51
# File 'lib/node.rb', line 48

def clear_points!
  check_admin_access
  points.destroy_all
end

#clear_values!Object



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

def clear_values!
  check_admin_access
  values.destroy_all
end

#decrement(value = 1, options = {}) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/node.rb', line 194

def decrement(value=1, options={})
  check_write_access
  if value.is_a?(Integer)
    increment (-1) * value, options
  else 
    raise ArgumentError, "Value must be an Integer"
  end
end

#increment(value = 1, options = {}) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/node.rb', line 184

def increment(value=1, options={})
  check_write_access
  if value.is_a?(Integer)
    last = self.points.last
    add_points last.value + value,options 
  else 
    raise ArgumentError, "Value must be an Integer"
  end
end

#initObject



39
40
41
# File 'lib/node.rb', line 39

def init
  @agent = nil
end

#point_recognizer(item, dt, options = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/node.rb', line 151

def point_recognizer(item, dt, options={})
  case 
  when item.is_a?(Integer)
    _insert_point item,dt, options
  when item.is_a?(Array)
    _insert_point_array(item, options)
  when item.is_a?(Hash)
    _insert_point_hash(item,dt,options)
  when item.is_a?(String)
    _insert_point_string(item,dt,options)
  else
    raise ArgumentError, "Can't recognize format of data item. #{item.inspect}"
  end
end

#points_range(dt_from, dt_to, options = {}) ⇒ Array of Objects

Returns items whose observed_at times fit within from a range.

Parameters:

  • dt_from (Time::Time)

    The start time.

  • dt_to (Time::Time)

    The end time.

Returns:

  • (Array of Objects)


227
228
229
# File 'lib/node.rb', line 227

def points_range(dt_from, dt_to,options={})
  _range(dt_from, dt_to, points, options)
end

#points_since(dt_from) ⇒ Array of Objects

Returns items whose observed_at times fit within from a range ending now.

Parameters:

  • dt_from (Time::Time)

    The start time.

Returns:

  • (Array of Objects)


234
235
236
# File 'lib/node.rb', line 234

def points_since(dt_from)
  self.points_range(dt_from,Time.now)
end

#set_permissions(agent, read = false, write = false, admin = false) ⇒ Object

Sets permissions flag for this address, for a specific agent. The existing Chawk::Relationship will be destroyed and a new one created as specified. Write access is not yet checked.

Parameters:

  • agent (Chawk::Agent)

    the agent to give permission.

  • read (Boolean) (defaults to: false)

    true/false can the agent read this address.

  • write (Boolean) (defaults to: false)

    true/false can the agent write this address. (Read acces is required to write.)

  • admin (Boolean) (defaults to: false)

    does the agent have ownership/adnim rights for this address. (Read and write are granted if admin is as well.)



260
261
262
263
264
265
266
267
268
269
# File 'lib/node.rb', line 260

def set_permissions(agent,read=false,write=false,admin=false)
  rels = relations.where(:agent_id => agent.id)
  rels.delete_all()
  rels = relations.where(:agent_id => agent.id)
  if read || write || admin
    vals = {agent:agent,read:(read ? true : false),write:(write ? true : false),admin:(admin ? true : false)}
    relations.create(vals)
  end
  nil
end

#set_public_read(value) ⇒ Object

Sets public read flag for this address

Parameters:

  • value (Boolean)

    true if public reading is allowed, false if it is not.



240
241
242
243
244
# File 'lib/node.rb', line 240

def set_public_read(value)
  value = value ? true : false
  self.update_attributes :public_read => value
  #save
end

#set_public_write(value) ⇒ Object

Sets public write flag for this address

Parameters:

  • value (Boolean)

    true if public writing is allowed, false if it is not.



248
249
250
251
252
# File 'lib/node.rb', line 248

def set_public_write(value)
  value = value ? true : false
  self.update_attributes :public_write => value
  #save
end

#value_recognizer(item, dt, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/node.rb', line 70

def value_recognizer(item, dt, options={})
  case 
  when item.is_a?(String)
    _insert_value item,dt, options
  else
    raise ArgumentError, "Can't recognize format of data item. #{item.inspect}"
  end
end

#values_range(dt_from, dt_to, options = {}) ⇒ Array of Objects

Returns items whose observed_at times fit within from a range.

Parameters:

  • dt_from (Time::Time)

    The start time.

  • dt_to (Time::Time)

    The end time.

Returns:

  • (Array of Objects)


212
213
214
# File 'lib/node.rb', line 212

def values_range(dt_from, dt_to,options={})
  _range(dt_from, dt_to, values, options)
end

#values_since(dt_from) ⇒ Array of Objects

Returns items whose observed_at times fit within from a range ending now.

Parameters:

  • dt_from (Time::Time)

    The start time.

Returns:

  • (Array of Objects)


219
220
221
# File 'lib/node.rb', line 219

def values_since(dt_from)
  self.values_range(dt_from,Time.now)
end