Module: Gearbox::AdHocProperties

Defined in:
lib/gearbox/mixins/ad_hoc_properties.rb

Overview

Allows a model instance to add a new property at runtime.

This is a (small) tip of the hat towards the flexibility offered by using a graph instead of a schema to govern the data store.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Getter and setter for an id property. Will be adjusted when I decide whether to mixin ActiveModel



12
13
14
# File 'lib/gearbox/mixins/ad_hoc_properties.rb', line 12

def id
  @id
end

Instance Method Details

#add_property(accessor, predicate, object) ⇒ Object

Add a property without defining it on the class. This will stay, will use the subject, and the regular infrastructure.

Parameters:

  • accessor, (Symbol)

    the new field being created.

  • predicate, (RDF::Statement)

    the predicate for the new field.

  • The (Any)

    value to store



35
36
37
38
# File 'lib/gearbox/mixins/ad_hoc_properties.rb', line 35

def add_property(accessor, predicate, object)
  new_property = RDF::Statement.new(bnode, predicate, object)
  attributes_list[accessor] = new_property
end

#attributes_listRDFCollection

Stored attributes

Returns:



16
17
18
# File 'lib/gearbox/mixins/ad_hoc_properties.rb', line 16

def attributes_list
  @attributes_list ||= RDFCollection.new
end

#bnodeRDF::Node

Generates or gets a blank node, based on the id. Will be replaced by subject.

Returns:

  • (RDF::Node)


23
24
25
26
27
28
# File 'lib/gearbox/mixins/ad_hoc_properties.rb', line 23

def bnode
  return @bnode if @bnode
  self.id ||= UUID.generate
  safe_id = "#{self.class.name}_#{id}".gsub(/[^A-Za-z0-9\-_]/, '_')
  @bnode = RDF::Node(safe_id)
end