Module: Ladder::Resource::Dynamic

Extended by:
ActiveSupport::Concern
Includes:
Ladder::Resource
Defined in:
lib/ladder/resource/dynamic.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Methods included from Ladder::Resource

#<<, from_uri, #klass_from_predicate, #update_resource

Methods included from Serializable

#as_framed_jsonld, #as_jsonld, #as_qname

Instance Method Details

#property(field_name, opts = {}) ⇒ Hash

Dynamically define a field on the object instance; in addition to (or overloading) class-level properties

Parameters:

  • field_name (String)

    ActiveModel attribute name for the field

  • opts (Hash) (defaults to: {})

    options to pass to Mongoid / ActiveTriples

Options Hash (opts):

  • :predicate (RDF::Term)

    RDF predicate for this property

Returns:

  • (Hash)

    an updated context for the object

See Also:

  • Ladder::Resource#property


28
29
30
31
32
33
34
35
36
37
# File 'lib/ladder/resource/dynamic.rb', line 28

def property(field_name, opts = {})
  # Store context information
  self._context ||= Hash.new(nil)

  # Ensure new field name is unique
  field_name = opts[:predicate].qname.join('_').to_sym if resource_class.properties.symbolize_keys.keys.include? field_name

  self._context[field_name] = opts[:predicate].to_s
  apply_context
end