Module: Ladder::Resource::Dynamic::InstanceMethods

Defined in:
lib/ladder/resource/dynamic.rb

Instance Method Summary collapse

Instance Method Details

#<<(statement) ⇒ Object?

Push an RDF::Statement into the object

Parameters:

  • statement (RDF::Statement, Hash, Array)

    @see RDF::Statement#from

Returns:

  • (Object, nil)

    the value inserted into the object

See Also:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ladder/resource/dynamic.rb', line 116

def <<(statement)
  # ActiveTriples::Resource expects: RDF::Statement, Hash, or Array
  statement = RDF::Statement.from(statement) unless statement.is_a? RDF::Statement

  case statement.object
  when resource_class.type then return # Don't store statically-defined types
  when RDF::Node then return super # Delegate nodes (relations) to parent
  end

  if RDF.type == statement.predicate
    # Store type information
    self._types ||= []
    self._types << statement.object.to_s

    apply_types
    return
  end

  # If we have an undefined predicate, then dynamically define it
  property statement.predicate.qname.last, predicate: statement.predicate unless field_from_predicate statement.predicate

  if self._context && self._context.values.include?(statement.predicate.to_s)
    send("#{self._context.key(statement.predicate.to_s)}=", statement.object.to_s)
  end

  super
end

#update_resource(opts = {}) ⇒ ActiveTriples::Resource

Update the delegated ActiveTriples::Resource from ActiveModel properties & relations

Parameters:

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

    options to pass to Mongoid / ActiveTriples

Returns:

  • (ActiveTriples::Resource)

    resource for the object

See Also:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ladder/resource/dynamic.rb', line 94

def update_resource(opts = {})
  # NB: super has to go first or AT clobbers properties
  super(opts)

  if self._context
    self._context.each do |field_name, uri|
      value = send(field_name)
      cast_uri = RDF::URI.new(value)
      resource.set_value(RDF::Vocabulary.find_term(uri), cast_uri.valid? ? cast_uri : value)
    end
  end

  resource
end