class NodeAttribute < ServerSide::Model schema :node_attributes do field :node_id, :integer field :kind, :integer field :value, :text index :node_id index [:node_id, :kind], :unique => true end def self.[](node_id, kind, create = false) att = find(:node_id => node_id, :kind => kind) if !att && create att = create(:node_id => node_id, :kind => kind) end att[:value] end def self.[]=(node_id, kind, value) att = find(:node_id => node_id, :kind => kind) if att att.set(:value => value) else att = create(:node_id => node_id, :kind => kind, :value => value) end att end end class Node < ServerSide::Model schema :nodes do field :path, :text, :unique => true, :null => false field :parent_id, :integer field :name, :text, :null => false field :kind, :smallint field :expression, :text field :producer_id, :integer field :last_quality, :smallint, :null => false, :default => 0 field :last_stamp, :double, :null => false field :last_value, :text, :default => nil field :last_datatype, :smallint, :default => nil index :parent_id index :producer_id end one_to_one :attributes, :class => NodeAttribute, :key => :node_id def self.create(parent, name, kind) transaction do node = super( :path => parent[:path]/name, :parent_id => parent[:id], :name => name, :kind => kind, :producer_id => parent[:producer_id], :last_stamp => Time.now ) node.copy_attributes(parent.attributes) node.update_producer end end def copy_attributes(source) source.each {|a| set_attribute(a[:kind], a[:value])} end end