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
-
#id ⇒ Object
Getter and setter for an id property.
Instance Method Summary collapse
-
#add_property(accessor, predicate, object) ⇒ Object
Add a property without defining it on the class.
-
#attributes_list ⇒ RDFCollection
Stored attributes.
-
#bnode ⇒ RDF::Node
Generates or gets a blank node, based on the id.
Instance Attribute Details
#id ⇒ Object
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.
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_list ⇒ RDFCollection
Stored attributes
16 17 18 |
# File 'lib/gearbox/mixins/ad_hoc_properties.rb', line 16 def attributes_list @attributes_list ||= RDFCollection.new end |
#bnode ⇒ RDF::Node
Generates or gets a blank node, based on the id. Will be replaced by subject.
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 |