Metadata

class
Superclass
Sequel::Model( :thingfish__metadata )
Included Modules
Thingfish::Normalization

A row of metadata describing an asset in a Thingfish store.

Public Class Methods

anchor
from_hash( hash )

Return a new Metadata object from the given oid and one-dimensional hash used by Thingfish.

# File lib/thingfish/metastore/pg/metadata.rb, line 89
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
anchor
metadata_columns()

Return the columns of the table that are used for resource metadata.

# File lib/thingfish/metastore/pg/metadata.rb, line 106
def self::metadata_columns
        return self.columns - [self.primary_key, :user_metadata]
end
anchor
new( * )

Do some initial attribute setup for new objects.

# File lib/thingfish/metastore/pg/metadata.rb, line 112
def initialize( * )
        super
        self[ :user_metadata ] ||= Sequel.pg_jsonb({})
end

Public Instance Methods

anchor
merge!( values )

Merge new metadata values into the metadata for the resource

# File lib/thingfish/metastore/pg/metadata.rb, line 131
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
anchor
order_metadata( *columns )

Dataset method: Order results by the specified columns.

# File lib/thingfish/metastore/pg/metadata.rb, line 59
def order_metadata( *columns )
        columns.flatten!
        ds = self
        columns.each do |column|
                if Thingfish::Metastore::PG::Metadata.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
anchor anchor anchor
to_hash()

Return the metadata as a Hash; overridden from Sequel::Model to merge the user and system pairs together.

# File lib/thingfish/metastore/pg/metadata.rb, line 120
def to_hash
        hash = self.values

        hash.delete( :oid )
        hash.merge!( hash.delete(:user_metadata) )

        return normalize_keys( hash )
end
anchor
unrelated()

Dataset method: Limit results to metadata which is not for a related resource.

# File lib/thingfish/metastore/pg/metadata.rb, line 27
def unrelated
        return self.where_metadata( relation: nil )
end
anchor
user_metadata_expr( field )

Returns a Sequel expression suitable for use as the key of a query against the specified user metadata field.

# File lib/thingfish/metastore/pg/metadata.rb, line 80
def user_metadata_expr( field )
        return Sequel.pg_jsonb( :user_metadata ).get_text( field.to_s )
end
anchor
where_metadata( hash )

Dataset method: Limit results to records whose operational or user metadata matches the values from the specified hash.

# File lib/thingfish/metastore/pg/metadata.rb, line 44
def where_metadata( hash )
        ds = self
        hash.each do |field, value|
                if Thingfish::Metastore::PG::Metadata.metadata_columns.include?( field.to_sym )
                        ds = ds.where( field.to_sym => value )
                else
                        ds = ds.where( self.user_metadata_expr(field) => value )
                end
        end

        return ds
end

Protected Instance Methods

anchor
method_missing( sym, *args, &block )

Proxy method – fetch a value from the metadata hash if it exists.

# File lib/thingfish/metastore/pg/metadata.rb, line 148
def method_missing( sym, *args, &block )
        return self.user_metadata[ sym.to_s ] || super
end