Module: Tripod::Fields::ClassMethods

Defined in:
lib/tripod/fields.rb

Instance Method Summary collapse

Instance Method Details

#field(name, predicate, options = {}) ⇒ Field

Defines all the fields that are accessible on the Resource For each field that is defined, a getter and setter will be added as an instance method to the Resource.

 @option options [ String, RDF::URI ] datatype The uri of the datatype for the field (will be used to create an RDF::Literal of the right type on the way in only).

Examples:

Define a field.

field :name, 'http://example.com/name'

Define a field of a specific RDF type

field :modified_at, 'http://example.com/modified_at', datatype: RDF::XSD.DateTime

Define a multi-valued field (can be combined with other options)

field :tags, 'http://example.com/tag', multivalued: true

Define a field containing a URI to another RDF resource

field :knows, 'http://example.com/knows', is_uri: true

Parameters:

  • name (Symbol)

    The name of the field.

  • predicate (String, RDF::URI)

    The predicate for the field.

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

    The options to pass to the field.

Options Hash (options):

  • multivalued (Boolean)

    Is this a multi-valued field? Default is false.

Returns:

  • (Field)

    The generated field



45
46
47
48
# File 'lib/tripod/fields.rb', line 45

def field(name, predicate, options = {})
  @fields ||= {}
  add_field(name, predicate, options)
end

#fieldsObject

Return all of the fields on a Resource in a manner that respects Ruby’s inheritance rules. i.e. subclass fields should override superclass fields with the same



66
67
68
69
70
# File 'lib/tripod/fields.rb', line 66

def fields
  tripod_superclasses.map { |c| c.instance_variable_get(:@fields) }.reduce do |acc,class_fields|
    class_fields.merge(acc)
  end
end

#get_field(name) ⇒ Object

Return the field object on a Resource associated with the given name.

Examples:

Get the field.

Person.get_field(:name)

Parameters:

  • name (Symbol)

    The name of the field.

Raises:



56
57
58
59
60
61
# File 'lib/tripod/fields.rb', line 56

def get_field(name)
  @fields ||= {}
  field = fields[name]
  raise Tripod::Errors::FieldNotPresent.new unless field
  field
end