Class: Kalimba::Resource
- Inherits:
-
Object
- Object
- Kalimba::Resource
- Extended by:
- Reflection
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Dirty, AttributeAssignment, Callbacks, LocalizedAttributes, Validations
- Defined in:
- lib/kalimba/resource.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Hash=> any with the resource attributes.
-
#subject ⇒ Object
readonly
Subject URI if the resource.
Class Method Summary collapse
-
.base_uri(uri = nil) ⇒ URI
Base URI for the resource.
-
.for(rid, params = {}) ⇒ Object
Create a new record with the given subject URI.
-
.from_datatype(datatype) ⇒ Kalimba::Resource
Return Kalimba resource class associated with the given datatype.
-
.has_many(name, params = {}) ⇒ Object
Collection definition.
-
.property(name, params = {}) ⇒ void
Property declaration.
-
.type(uri = nil) ⇒ URI
Type URI of RDFS class.
Instance Method Summary collapse
-
#eql?(other) ⇒ Boolean
(also: #==)
Resource equality.
-
#freeze ⇒ self
Freeze the attributes hash such that associations are still accessible, even on destroyed records.
-
#frozen? ⇒ Boolean
Checks whether the attributes hash has been frozen.
-
#initialize(params = {}, options = {}) {|_self| ... } ⇒ Resource
constructor
Create a new record.
-
#properties ⇒ Hash{String => Hash}
Properties with their options.
-
#to_rdf ⇒ URI?
RDF representation of the model.
Methods included from Reflection
create_reflection, reflect_on_association, reflections
Methods included from Validations
Methods included from Callbacks
Methods included from LocalizedAttributes
#retrieve_localizable_property, #store_localizable_property
Methods included from AttributeAssignment
Constructor Details
#initialize(params = {}, options = {}) {|_self| ... } ⇒ Resource
Create a new record
If given a block, yields the created object into it.
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 = {}, = {}) 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, )| value = [:collection] ? [] : nil attrs.merge(name => value) end assign_attributes(params, ) @destroyed = false yield self if block_given? end |
Instance Attribute Details
#attributes ⇒ Object
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 |
#subject ⇒ Object (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
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
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.
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
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
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.
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.
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
Can be set only once
Type URI of RDFS class
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.
281 282 283 |
# File 'lib/kalimba/resource.rb', line 281 def eql?(other) other.is_a?(Kalimba::Resource) && !subject.nil? && other.subject == subject end |
#freeze ⇒ self
Freeze the attributes hash such that associations are still accessible, even on destroyed records
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
262 263 264 |
# File 'lib/kalimba/resource.rb', line 262 def frozen? @attributes.frozen? end |
#properties ⇒ Hash{String => Hash}
Properties with their options
34 |
# File 'lib/kalimba/resource.rb', line 34 class_attribute :properties, instance_writer: false, instance_reader: false |
#to_rdf ⇒ URI?
RDF representation of the model
269 270 271 |
# File 'lib/kalimba/resource.rb', line 269 def to_rdf subject end |