Class: Kalimba::Resource

Inherits:
Object
  • Object
show all
Extended by:
Reflection
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Dirty, AttributeAssignment, Callbacks, LocalizedAttributes, Validations
Defined in:
lib/kalimba/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Reflection

create_reflection, reflect_on_association, reflections

Methods included from Validations

#save, #save!, #valid?

Methods included from Callbacks

#destroy, #save

Methods included from LocalizedAttributes

#retrieve_localizable_property, #store_localizable_property

Methods included from AttributeAssignment

#assign_attributes

Constructor Details

#initialize(params = {}, options = {}) {|_self| ... } ⇒ Resource

Create a new record

If given a block, yields the created object into it.

Parameters:

  • params (Hash<[Symbol, String] => Any>) (defaults to: {})

    properties to assign

Yields:

  • (_self)

Yield Parameters:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/kalimba/resource.rb', line 228

def initialize(params = {}, options = {})
  params = params.stringify_keys

  if params["_subject"]
    if self.class.base_uri
      @subject = self.class.base_uri.dup
      @subject.fragment = params.delete("_subject")
    else
      raise KalimbaError, "Cannot assign an ID to a resource without base_uri"
    end
  end

  @attributes = self.class.properties.inject({}) do |attrs, (name, options)|
    value = options[:collection] ? [] : nil
    attrs.merge(name => value)
  end
  assign_attributes(params, options)

  @destroyed = false

  yield self if block_given?
end

Instance Attribute Details

#attributesObject

Note:

Do not modify it directly, unless you know what you are doing!

Hash=> any with the resource attributes



29
30
31
# File 'lib/kalimba/resource.rb', line 29

def attributes
  @attributes
end

#subjectObject (readonly)

Subject URI if the resource



23
24
25
# File 'lib/kalimba/resource.rb', line 23

def subject
  @subject
end

Class Method Details

.base_uri(uri = nil) ⇒ URI

Base URI for the resource

Parameters:

  • uri (String, URI) (defaults to: nil)

Returns:

  • (URI)


78
79
80
# File 'lib/kalimba/resource.rb', line 78

def base_uri(uri = nil)
  @base_uri ||= uri && URI(uri.to_s.sub(/\/?$/, "/"))
end

.for(rid, params = {}) ⇒ Object

Note:

In the world of RDF a resource cannot be instantly defined as “new”, because any arbitrary subject that you specify might be already present in the storage. So you can use ID of an existing resource as well.

Don’t forget to #reload the resource, if you need its actual attributes (if any) pulled from the storage.

Note:

The resource ID that you supply will be added as an URI fragment to base_uri (or raise an error if base_uri is not defined).

Create a new record with the given subject URI

Parameters:

  • rid (String)

    ID to use for the resource

  • params (Hash<[Symbol, String] => Any>) (defaults to: {})

    (see RDFSResource#initialize)

Returns:

  • (Object)

    instance of the model



56
57
58
# File 'lib/kalimba/resource.rb', line 56

def for(rid, params = {})
  new(params.merge(:_subject => rid))
end

.from_datatype(datatype) ⇒ Kalimba::Resource

Return Kalimba resource class associated with the given datatype

Parameters:

  • uri (String, URI, Symbol)

Returns:



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/kalimba/resource.rb', line 161

def from_datatype(datatype)
  return datatype if datatype.is_a?(Class) && datatype.ancestors.include?(Kalimba::Resource)

  datatype =
    case datatype
    when URI
      datatype
    when Symbol
      const_get(datatype).type
    when String
      if datatype =~ URI.regexp
        URI(datatype)
      else
        const_get(datatype).type
      end
    else
      if datatype.respond_to?(:uri)
        datatype.uri
      else
        raise KalimbaError, "invalid datatype identifier"
      end
    end
  Kalimba::Resource.descendants.detect {|a| a.type == datatype }
end

.has_many(name, params = {}) ⇒ Object

Collection definition

“Has-many” relations/collections are declared with help of ‘has_many` method. It accepts the same parameters as `property` (basically, it is an alias to `property name, …, collection: true`). Additionally, you can specify `:datatype` as a name of another model, as seen below. If you specify datatype as an URI, it will be automatically resolved to either a model (having the same `type`) or anonymous class.

You don’t have to treat ‘has_many` as an association with other models, however. It is acceptable to declare a collection of strings or any other resources using `has_many`:

Note however, that if given datatype does not refer to a Kalimba::Resource class, no reflections and ‘_ids` and `_ids=` accessors will be created in that case.

Examples:

has_many :friends, :predicate => "http://schema.org/Person", :datatype => :Person
has_many :duties, :predicate => "http://works.com#duty", :datatype => NS::XMLSchema["string"]


153
154
155
# File 'lib/kalimba/resource.rb', line 153

def has_many(name, params = {})
  property name, params.merge(:collection => true)
end

.property(name, params = {}) ⇒ void

This method returns an undefined value.

Property declaration

Model attributes should be declared using ‘property`. Two mandatory parameters are `:predicate` and `:datatype`, that can accept URIs as URI or String objects. You can also use “NS::” namespaces provided by `xml_schema` gem.

Parameters:

  • name (Symbol, String)
  • params (Hash) (defaults to: {})

Options Hash (params):

  • :predicate (String, URI)
  • :datatype (String, URI, Symbol)
  • :collection (Boolean)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/kalimba/resource.rb', line 95

def property(name, params = {})
  name = name.to_s

  params[:predicate] = URI(params[:predicate])
  association_class = Kalimba::Resource.from_datatype(params[:datatype])
  if association_class
    params[:datatype] = association_class
    if params[:collection]
      define_collection(name, association_class)
    else
      define_resource(name, association_class)
    end
  else
    params[:datatype] = URI(params[:datatype])
  end

  self.properties[name] = params

  define_attribute_method name if self.is_a?(Class)

  class_eval <<-HERE, __FILE__, __LINE__
    def #{name}=(value)
      write_attribute "#{name}", value
    end
  HERE

  if localizable_property?(name)
    class_eval <<-HERE, __FILE__, __LINE__
      def localized_#{name.pluralize}
        @localized_#{name.pluralize} ||= {}
      end
    HERE
  end
end

.type(uri = nil) ⇒ URI

Note:

Can be set only once

Type URI of RDFS class

Parameters:

  • uri (URI, String) (defaults to: nil)

Returns:

  • (URI)


66
67
68
69
70
71
72
# File 'lib/kalimba/resource.rb', line 66

def type(uri = nil)
  if uri
    @type ||= URI(uri)
  else
    @type
  end
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Resource equality

Two Kalimba resources are equal if their subjects are equal. We do not tell between persisted or not persisted RDF resources. However, “new” resources (those without a subject) are never equal.

Parameters:

Returns:

  • (Boolean)


281
282
283
# File 'lib/kalimba/resource.rb', line 281

def eql?(other)
  other.is_a?(Kalimba::Resource) && !subject.nil? && other.subject == subject
end

#freezeself

Freeze the attributes hash such that associations are still accessible, even on destroyed records

Returns:

  • (self)


255
256
257
# File 'lib/kalimba/resource.rb', line 255

def freeze
  @attributes.freeze; self
end

#frozen?Boolean

Checks whether the attributes hash has been frozen

Returns:

  • (Boolean)


262
263
264
# File 'lib/kalimba/resource.rb', line 262

def frozen?
  @attributes.frozen?
end

#propertiesHash{String => Hash}

Properties with their options

Returns:

  • (Hash{String => Hash})


34
# File 'lib/kalimba/resource.rb', line 34

class_attribute :properties, instance_writer: false, instance_reader: false

#to_rdfURI?

RDF representation of the model

Returns:

  • (URI, nil)

    subject URI



269
270
271
# File 'lib/kalimba/resource.rb', line 269

def to_rdf
  subject
end