lib/thingfish/metastore/pggraph/node.rb
author Mahlon E. Smith <mahlon@martini.nu>
Wed, 09 Nov 2016 22:38:18 -0800
changeset 8 1ad0d5bc5083
parent 7 f0ef12e25a2f
child 20 e2e96d97b77c
permissions -rw-r--r--
Fix criteria searches that include both 'relationship' and 'relation' fields.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
# -*- ruby -*-
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
#encoding: utf-8
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
require 'sequel/model'
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
require 'thingfish/mixins'
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
require 'thingfish/metastore/pggraph' unless defined?( Thingfish::Metastore::PgGraph )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
# A row of metadata describing an asset in a Thingfish store.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
class Thingfish::Metastore::PgGraph::Node < Sequel::Model( :nodes )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
	include Thingfish::Normalization
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
	# Related resources for this node
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
	one_to_many :related_nodes, :key => :id_p, :class => 'Thingfish::Metastore::PgGraph::Edge'
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
	# Edge relation if this node is a related resource
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
	one_to_one :related_to, :key => :id_c, :class => 'Thingfish::Metastore::PgGraph::Edge'
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
	# Allow instances to be created with a primary key
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
	unrestrict_primary_key
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
	# Dataset methods
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
	dataset_module do
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
		### Dataset method: Limit results to metadata which is for a related resource.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
		###
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
		def related
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
			return self.join_edges( :rel ).exclude( :rel__id_c => nil )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
		### Dataset method: Limit results to metadata which is not for a related resource.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
		###
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
		def unrelated
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
			return self.join_edges( :notrel ).filter( :notrel__id_c => nil )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
		### Dataset method: Limit results to records whose operational or user
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
		### metadata matches the values from the specified +hash+.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
		###
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
		def where_metadata( hash )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
			ds = self
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
			hash.each do |field, value|
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
				# Direct DB column
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
				#
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
				if self.model.metadata_columns.include?( field.to_sym )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
					ds = ds.where( field.to_sym => value )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
				# User metadata or edge relationship
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
				#
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
				else
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
					if field.to_sym == :relationship
8
1ad0d5bc5083 Fix criteria searches that include both 'relationship' and 'relation' fields.
Mahlon E. Smith <mahlon@martini.nu>
parents: 7
diff changeset
    57
						ds = ds.join_edges unless ds.joined_dataset?
1ad0d5bc5083 Fix criteria searches that include both 'relationship' and 'relation' fields.
Mahlon E. Smith <mahlon@martini.nu>
parents: 7
diff changeset
    58
						ds = ds.filter( Sequel.pg_jsonb( :edges__prop ).get_text( field.to_s ) => value )
0
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
					elsif field.to_sym == :relation
8
1ad0d5bc5083 Fix criteria searches that include both 'relationship' and 'relation' fields.
Mahlon E. Smith <mahlon@martini.nu>
parents: 7
diff changeset
    61
						ds = ds.join_edges unless ds.joined_dataset?
0
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
						ds = self.join_edges.filter( :edges__id_p => value )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
					else
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
						ds = ds.where( self.user_metadata_expr(field) => value )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
					end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
				end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
			end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
			return ds
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
3
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    74
		### Dataset method: Order results by the specified +columns+.
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    75
		###
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    76
		def order_metadata( *columns )
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    77
			columns.flatten!
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    78
			ds = self
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    79
			columns.each do |column|
7
f0ef12e25a2f Fix copy/pasta.
Mahlon E. Smith <mahlon@martini.nu>
parents: 3
diff changeset
    80
				if Thingfish::Metastore::PgGraph::Node.metadata_columns.include?( column.to_sym )
3
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    81
					ds = ds.order_append( column.to_sym )
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    82
				else
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    83
					ds = ds.order_append( self.user_metadata_expr(column) )
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    84
				end
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    85
			end
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    86
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    87
			return ds
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    88
		end
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    89
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    90
5b4ee98d698c Pull recent Metastore::PG fixes into Metastore::PgGraph.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    91
0
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
		#########
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
		protected
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
		#########
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
		### Return a dataset linking related nodes to edges.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
		###
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    98
		def join_edges( aka=nil )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
			return self.join_table( :left, :edges, { :id_c => :nodes__id }, { :table_alias => aka } )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   102
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   103
		### Returns a Sequel expression suitable for use as the key of a query against
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
		### the specified user metadata field.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
		def user_metadata_expr( field )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   106
			return Sequel.pg_jsonb( :user_metadata ).get_text( field.to_s )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   107
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   108
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   109
	end # dataset_module
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   111
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   112
	### Return a new Metadata object from the given +oid+ and one-dimensional +hash+
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   113
	### used by Thingfish.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   114
	def self::from_hash( hash )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   115
		metadata = Thingfish::Normalization.normalize_keys( hash )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   116
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   117
		md = new
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   118
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
		md.format        = metadata.delete( 'format' )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   120
		md.extent        = metadata.delete( 'extent' )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   121
		md.created       = metadata.delete( 'created' )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   122
		md.uploadaddress = metadata.delete( 'uploadaddress' ).to_s
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   123
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   124
		md.user_metadata = Sequel.pg_jsonb( metadata )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   125
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   126
		return md
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   127
	end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   128
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   129
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   130
	### Return the columns of the table that are used for resource metadata.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   131
	def self::metadata_columns
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   132
		return self.columns - [self.primary_key, :user_metadata]
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   133
	end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   134
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   135
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   136
	### Do some initial attribute setup for new objects.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   137
	def initialize( * )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   138
		super
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   139
		self[ :user_metadata ] ||= Sequel.pg_jsonb({})
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   140
	end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   141
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   142
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   143
	### Return the metadata as a Hash; overridden from Sequel::Model to
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   144
	### merge the user and system pairs together.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   145
	def to_hash
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   146
		hash = self.values.dup
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   147
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   148
		hash.delete( :id )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   149
		hash.merge!( hash.delete(:user_metadata) )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   150
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   151
		if related_to = self.related_to
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   152
			hash.merge!( related_to.prop )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   153
			hash[ :relation ] = related_to.id_p
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   154
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   155
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   156
		return normalize_keys( hash )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   157
	end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   158
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   159
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   160
	### Merge new metadata +values+ into the metadata for the resource
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   161
	def merge!( values )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   162
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   163
		# Extract and set the column-metadata values first
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   164
		self.class.metadata_columns.each do |col|
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   165
			next unless values.key?( col.to_s )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   166
			self[ col ] = values.delete( col.to_s )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   167
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   168
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   169
		self.user_metadata.merge!( values )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   170
	end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   171
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   172
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   173
	### Hook creation for new related resources, divert relation data to
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   174
	### a new edge row.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   175
	###
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   176
	def around_save
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   177
		relationship = self.user_metadata.delete( 'relationship' )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   178
		relation     = self.user_metadata.delete( 'relation' )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   179
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   180
		super
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   181
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   182
		if relation
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   183
			edge = Thingfish::Metastore::PgGraph::Edge.new
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   184
			edge.prop[ 'relationship' ] = relationship
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   185
			edge.id_p = relation
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   186
			edge.id_c = self.id
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   187
			edge.save
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   188
		end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   189
	end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   190
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   191
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   192
	#########
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   193
	protected
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   194
	#########
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   195
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   196
	### Proxy method -- fetch a value from the metadata hash if it exists.
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   197
	def method_missing( sym, *args, &block )
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   198
		return self.user_metadata[ sym.to_s ] || super
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   199
	end
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   200
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   201
end # Thingfish::Metastore::PgGraph::Node
3cc90e88c6ab Initial commit.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   202