class Thingfish::Metastore::PgGraph::Node
A row of metadata describing an asset in a Thingfish
store.
Public Class Methods
Return a new Metadata object from the given oid
and one-dimensional hash
used by Thingfish
.
# File lib/thingfish/metastore/pggraph/node.rb, line 113 def self::from_hash( hash ) metadata = Thingfish::Normalization.normalize_keys( hash ) md = new md.format = metadata.delete( 'format' ) md.extent = metadata.delete( 'extent' ) md.created = metadata.delete( 'created' ) md.uploadaddress = metadata.delete( 'uploadaddress' ).to_s md.user_metadata = Sequel.pg_jsonb( metadata ) return md end
Return the columns of the table that are used for resource metadata.
# File lib/thingfish/metastore/pggraph/node.rb, line 130 def self::metadata_columns return self.columns - [self.primary_key, :user_metadata] end
Do some initial attribute setup for new objects.
# File lib/thingfish/metastore/pggraph/node.rb, line 136 def initialize( * ) super self[ :user_metadata ] ||= Sequel.pg_jsonb({}) end
Public Instance Methods
Hook creation for new related resources, divert relation data to a new edge row.
# File lib/thingfish/metastore/pggraph/node.rb, line 175 def around_save relationship = self.user_metadata.delete( 'relationship' ) relation = self.user_metadata.delete( 'relation' ) super if relation edge = Thingfish::Metastore::PgGraph::Edge.new edge.prop[ 'relationship' ] = relationship edge.id_p = relation edge.id_c = self.id edge.save end end
Return a dataset linking related nodes to edges.
# File lib/thingfish/metastore/pggraph/node.rb, line 97 def join_edges( aka=nil ) return self.join_table( :left, :edges, { :id_c => :nodes__id }, { :table_alias => aka } ) end
Merge new metadata values
into the metadata for the resource
# File lib/thingfish/metastore/pggraph/node.rb, line 160 def merge!( values ) # Extract and set the column-metadata values first self.class.metadata_columns.each do |col| next unless values.key?( col.to_s ) self[ col ] = values.delete( col.to_s ) end self.user_metadata.merge!( values ) end
Dataset method: Order results by the specified columns
.
# File lib/thingfish/metastore/pggraph/node.rb, line 75 def order_metadata( *columns ) columns.flatten! ds = self columns.each do |column| if Thingfish::Metastore::PgGraph::Node.metadata_columns.include?( column.to_sym ) ds = ds.order_append( column.to_sym ) else ds = ds.order_append( self.user_metadata_expr(column) ) end end return ds end
Return the metadata as a Hash; overridden from Sequel::Model to merge the user and system pairs together.
# File lib/thingfish/metastore/pggraph/node.rb, line 144 def to_hash hash = self.values.dup hash.delete( :id ) hash.merge!( hash.delete(:user_metadata) ) if related_to = self.related_to hash.merge!( related_to.prop ) hash[ :relation ] = related_to.id_p end return normalize_keys( hash ) end
Returns a Sequel expression suitable for use as the key of a query against the specified user metadata field.
# File lib/thingfish/metastore/pggraph/node.rb, line 104 def user_metadata_expr( field ) return Sequel.pg_jsonb( :user_metadata ).get_text( field.to_s ) end
Dataset method: Limit results to records whose operational or user metadata matches the values from the specified hash
.
# File lib/thingfish/metastore/pggraph/node.rb, line 43 def where_metadata( hash ) ds = self hash.each do |field, value| # Direct DB column # if self.model.metadata_columns.include?( field.to_sym ) ds = ds.where( field.to_sym => value ) # User metadata or edge relationship # else if field.to_sym == :relationship ds = ds.join_edges unless ds.joined_dataset? ds = ds.filter( Sequel.pg_jsonb( :edges__prop ).get_text( field.to_s ) => value ) elsif field.to_sym == :relation ds = ds.join_edges unless ds.joined_dataset? ds = self.join_edges.filter( :edges__id_p => value ) else ds = ds.where( self.user_metadata_expr(field) => value ) end end end return ds end
Protected Instance Methods
Proxy method – fetch a value from the metadata hash if it exists.
# File lib/thingfish/metastore/pggraph/node.rb, line 196 def method_missing( sym, *args, &block ) return self.user_metadata[ sym.to_s ] || super end