Module: Spira::Resource

Included in:
Base
Defined in:
lib/spira/resource.rb

Instance Method Summary collapse

Instance Method Details

#configure(options = {}) ⇒ Object

Configuration options for the Spira::Resource:

@params options :base_uri :: base URI to be used for the resource :default_vocabulary :: default vocabulary to use for the properties defined for this resource All these configuration options are readable via their respectively named Spira resource methods.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spira/resource.rb', line 16

def configure(options = {})
  singleton_class.class_eval do
    { base_uri: options[:base_uri],
      default_vocabulary: options[:default_vocabulary]
    }.each do |name, value|
      # redefine reader methods only when required,
      # otherwise, use the ancestor methods
      if value
        define_method name do
          value
        end
      end
    end
  end
end

#has_many(name, opts = {}) ⇒ Object

The plural form of property. Has_many has the same options as property, but instead of a single value, a Ruby Array of objects will be created instead.

has_many corresponds to an RDF subject with several triples of the same predicate. This corresponds to a Ruby Array, which will be returned when the property is accessed. Arrays will be accepted for new values, but ordering and duplicate values will be lost on save.

See Also:

  • Base::DSL#property


112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/spira/resource.rb', line 112

def has_many(name, opts = {})
  property(name, opts)

  reflections[name] = AssociationReflection.new(:has_many, name, opts)

  define_method "#{name.to_s.singularize}_ids" do
    records = send(name) || []
    records.map(&:id).compact
  end
  define_method "#{name.to_s.singularize}_ids=" do |ids|
    records = ids.map {|id| self.class.reflect_on_association(name).klass.unserialize(id) }.compact
    send "#{name}=", records
  end
end

#property(name, opts = {}) ⇒ Void

Add a property to this class. A property is an accessor field that represents an RDF predicate.

type for this property. If a Spira::Type is given, that class will be used to serialize and unserialize values. If a String is given, it should be the String form of a Spira::Base class name (Strings are used to prevent issues with load order).

Examples:

A simple string property

property :name, predicate: RDF::Vocab::FOAF.name, type: String

A property which defaults to Types::Any

property :name, predicate: RDF::Vocab::FOAF.name

An integer property

property :age,  predicate: RDF::Vocab::FOAF.age, type: Integer

Parameters:

  • name (Symbol)

    The name of this property

  • opts (Hash{Symbol => Any}) (defaults to: {})

    property options

Options Hash (opts):

  • :predicate (RDF::URI)

    The RDF predicate which will refer to this property

  • :type (Spira::Type, String) — default: Spira::Types::Any

    The

Returns:

  • (Void)

See Also:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/spira/resource.rb', line 80

def property(name, opts = {})
  if opts.delete(:localized)
    raise 'Only Spira::Types::Any properties can accept the :localized option' unless type_for(opts[:type]) == Spira::Types::Any
    define_localized_property_methods(name, opts)
    has_many "#{name}_native", opts.merge(type: Spira::Types::Native)
  else
    unset_has_many(name)
    predicate = predicate_for(opts[:predicate], name)
    type = type_for(opts[:type])
    properties[name] = HashWithIndifferentAccess.new(predicate: predicate, type: type)

    define_attribute_method name
    define_method "#{name}=" do |arg|
      write_attribute name, arg
    end
    define_method name do
      read_attribute name
    end
  end
end

#type(uri = nil) ⇒ Object

Declare a type for the Spira::Resource. You can declare multiple types for a resource with multiple “type” assignments. If no types are declared for a resource, they are inherited from the parent resource.

@params uri



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/spira/resource.rb', line 41

def type(uri = nil)
  if uri
    if uri.is_a?(RDF::URI)
      ts = @types ? types : Set.new
      singleton_class.class_eval do
        define_method :types do
          ts
        end
      end
      @types = ts << uri
    else
      raise TypeError, "Type must be a RDF::URI"
    end
  else
    types.first
  end
end