Module: RDF::AssociatedProperty
- Includes:
- Enumerable
- Defined in:
- lib/active_rdf/objectmanager/property.rb
Overview
Provides methods for accessing property values when @subject is set
Instance Method Summary collapse
-
#+(obj) ⇒ Object
Returns a new array with the object appended, or the objects values if obj.respond_to? :to_ary.
-
#-(obj) ⇒ Object
Returns a new array with the object removed.
- #==(other) ⇒ Object (also: #eql?)
-
#[](md5_or_value) ⇒ Object
(also: #at)
Value reference.
-
#[]=(md5_or_value, new_value) ⇒ Object
Selective value replacement.
-
#add(*args) ⇒ Object
Append.
-
#clear ⇒ Object
Removes all values.
-
#collect!(&block) ⇒ Object
(also: #map!)
Invokes the block once for each value, replacing the value with the value returned by block.
-
#context(context = nil) ⇒ Object
Returns the context for the property if context is nil.
-
#context=(context) ⇒ Object
Sets context for this property.
-
#datatype(type = nil) ⇒ Object
Returns the datatype if type is nil.
-
#datatype=(type) ⇒ Object
Sets datatype for this property.
-
#delete(md5_or_value) ⇒ Object
Deletes value given by key or value.
-
#delete_if(&block) ⇒ Object
Deletes every value for which block evaluates to true.
-
#each(&block) ⇒ Object
(also: #each_value)
Calls block once for each value, passing a copy of the value as a parameter.
-
#each_key(&block) ⇒ Object
Calls block once for each value, passing the key as a parameter.
-
#each_pair(&block) ⇒ Object
Calls block once for each value, passing the key and value as a parameters See also #to_h.
-
#empty? ⇒ Boolean
(also: #blank?)
Returns true if the property contains no values.
-
#fetch(md5, default = nil, &block) ⇒ Object
Returns a value from the property for the given key.
-
#include?(obj) ⇒ Boolean
(also: #value?, #has_value?)
Returns true if the given key or value is present.
-
#index(obj) ⇒ Object
Returns the key for a given value.
-
#inspect ⇒ Object
Return the value(s) of this property as a string.
-
#keys ⇒ Object
Returns a new array populated with the keys to the values.
-
#lang(tag = nil, exact = true) ⇒ Object
Returns the language tag and the match settings for the property if tag is nil.
-
#lang=(*args) ⇒ Object
Sets lang and match settings.
-
#length ⇒ Object
(also: #size)
Returns the number of values assigned to this property for this @subject.
-
#only ⇒ Object
Ensure the return of only one value assigned to this property for this @subject.
-
#reject!(&block) ⇒ Object
Equivalent to Property#delete_if, but returns nil if no changes were made.
-
#replace(new) ⇒ Object
Value replacement.
-
#to_h ⇒ Object
Returns a hash of copies of all values with indexes.
-
#values_at(*args) ⇒ Object
Return an array containing the values for the given keys.
Instance Method Details
#+(obj) ⇒ Object
Returns a new array with the object appended, or the objects values if obj.respond_to? :to_ary
85 86 87 |
# File 'lib/active_rdf/objectmanager/property.rb', line 85 def +(obj) to_a + [*obj] end |
#-(obj) ⇒ Object
Returns a new array with the object removed
90 91 92 |
# File 'lib/active_rdf/objectmanager/property.rb', line 90 def -(obj) to_a - [*obj] end |
#==(other) ⇒ Object Also known as: eql?
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/active_rdf/objectmanager/property.rb', line 94 def ==(other) if other.respond_to?(:to_ary) Set.new(other) == Set.new(to_a) else arr = to_a if arr.size == 1 other == arr[0] || super else super end end end |
#[](md5_or_value) ⇒ Object Also known as: at
Value reference. Retrieves a copy of the value by the key or value. Returns nil if not found.
68 69 70 71 72 73 |
# File 'lib/active_rdf/objectmanager/property.rb', line 68 def [](md5_or_value) unless md5_or_value.nil? arr = to_a arr.find{|value| value == md5_or_value} || arr.find{|value| get_key(value) == md5_or_value} end end |
#[]=(md5_or_value, new_value) ⇒ Object
Selective value replacement. Replaces the value given by key or value. Raises IndexError if value not found.
77 78 79 80 81 82 |
# File 'lib/active_rdf/objectmanager/property.rb', line 77 def []=(md5_or_value,new_value) value = self[md5_or_value] raise IndexError, "Couldn't find existing value to replace: #{md5_or_value}" unless value ActiveRDF::FederationManager.delete(@subject, self.property, value) add(new_value) end |
#add(*args) ⇒ Object
Append. Adds the given object(s) to the values for this property belonging to @subject This expression returns the property itself, so several appends may be chained together.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/active_rdf/objectmanager/property.rb', line 110 def add(*args) args.each do |arg| if arg.respond_to?(:to_ary) arg.to_ary.each {|item| ActiveRDF::FederationManager.add(@subject, self.property, item)} else ActiveRDF::FederationManager.add(@subject, self.property, arg) end end self end |
#clear ⇒ Object
Removes all values
122 123 124 125 |
# File 'lib/active_rdf/objectmanager/property.rb', line 122 def clear ActiveRDF::FederationManager.delete(@subject, self.property) self end |
#collect!(&block) ⇒ Object Also known as: map!
Invokes the block once for each value, replacing the value with the value returned by block
128 129 130 131 132 133 134 135 |
# File 'lib/active_rdf/objectmanager/property.rb', line 128 def collect!(&block) to_a.each do |item| new_item = yield(item) delete(item) add(new_item) end self end |
#context(context = nil) ⇒ Object
Returns the context for the property if context is nil. Returns a new RDF::Property object with the @context value set if context is provided see also #lang, #datatype
141 142 143 144 145 146 147 148 149 |
# File 'lib/active_rdf/objectmanager/property.rb', line 141 def context(context = nil) if context.nil? @context else property_with_context = self.dup property_with_context.context = context property_with_context end end |
#context=(context) ⇒ Object
Sets context for this property
152 153 154 |
# File 'lib/active_rdf/objectmanager/property.rb', line 152 def context=(context) @context = context end |
#datatype(type = nil) ⇒ Object
Returns the datatype if type is nil. Returns a new RDF::Property object with the @datatype set if type is provided see also #context, #lang
159 160 161 162 163 164 165 166 167 |
# File 'lib/active_rdf/objectmanager/property.rb', line 159 def datatype(type = nil) if type.nil? @datatype else property_with_datatype = dup property_with_datatype.datatype = type property_with_datatype end end |
#datatype=(type) ⇒ Object
Sets datatype for this property
170 171 172 |
# File 'lib/active_rdf/objectmanager/property.rb', line 170 def datatype=(type) @datatype = type end |
#delete(md5_or_value) ⇒ Object
Deletes value given by key or value. If the item is not found, returns nil. If the optional code block is given, returns the result of block if the item is not found
176 177 178 179 180 181 182 183 184 |
# File 'lib/active_rdf/objectmanager/property.rb', line 176 def delete(md5_or_value) value = self[md5_or_value] if value ActiveRDF::FederationManager.delete(@subject, self.property, value) value elsif block_given? yield end end |
#delete_if(&block) ⇒ Object
Deletes every value for which block evaluates to true
187 188 189 190 |
# File 'lib/active_rdf/objectmanager/property.rb', line 187 def delete_if(&block) # :yields: key, value reject!(&block) self end |
#each(&block) ⇒ Object Also known as: each_value
Calls block once for each value, passing a copy of the value as a parameter
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/active_rdf/objectmanager/property.rb', line 193 def each(&block) # :yields: value q = ActiveRDF::Query.new.distinct(:o).where(@subject,self,:o,@context) if @lang and !@datatype q.lang(:o,@lang,@exact_lang) elsif @datatype and !@lang q.datatype(:o, @datatype) elsif @lang and @datatype raise ActiveRdfError, "@datatype and @lang may not both be set" end q.execute(&block) self end |
#each_key(&block) ⇒ Object
Calls block once for each value, passing the key as a parameter
208 209 210 211 |
# File 'lib/active_rdf/objectmanager/property.rb', line 208 def each_key(&block) # :yields: key keys.each(&block) self end |
#each_pair(&block) ⇒ Object
Calls block once for each value, passing the key and value as a parameters See also #to_h
215 216 217 218 |
# File 'lib/active_rdf/objectmanager/property.rb', line 215 def each_pair(&block) # :yields: key, value each{|value| yield get_key(value), value} self end |
#empty? ⇒ Boolean Also known as: blank?
Returns true if the property contains no values
221 222 223 |
# File 'lib/active_rdf/objectmanager/property.rb', line 221 def empty? to_a.empty? end |
#fetch(md5, default = nil, &block) ⇒ Object
Returns a value from the property for the given key. If the key can’t be found, there are several options: With no other arguments, it will raise an IndexError exception; if default is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/active_rdf/objectmanager/property.rb', line 229 def fetch(md5, default = nil, &block) val = self[md5] if val val else if block_given? yield md5 elsif default default else raise IndexError, "could not find #{md5}" end end end |
#include?(obj) ⇒ Boolean Also known as: value?, has_value?
Returns true if the given key or value is present
245 246 247 |
# File 'lib/active_rdf/objectmanager/property.rb', line 245 def include?(obj) !!self[obj] end |
#index(obj) ⇒ Object
Returns the key for a given value. If not found, returns nil.
252 253 254 255 |
# File 'lib/active_rdf/objectmanager/property.rb', line 252 def index(obj) value = to_a.find{|val| obj == val} get_key(value) if value end |
#inspect ⇒ Object
Return the value(s) of this property as a string.
258 259 260 |
# File 'lib/active_rdf/objectmanager/property.rb', line 258 def inspect "#<RDF::Property #{abbr} [#{to_a.collect{|obj| obj.inspect}.join(", ")}]>" end |
#keys ⇒ Object
Returns a new array populated with the keys to the values
263 264 265 |
# File 'lib/active_rdf/objectmanager/property.rb', line 263 def keys collect{|value| get_key(value)} end |
#lang(tag = nil, exact = true) ⇒ Object
Returns the language tag and the match settings for the property if tag is nil. Returns a new RDF::Property object with the @lang value set if tag is provided see also #context, #datatype
270 271 272 273 274 275 276 277 278 |
# File 'lib/active_rdf/objectmanager/property.rb', line 270 def lang(tag = nil, exact = true) if tag.nil? [@lang,@exact_lang] else property_with_lang = RDF::Property.new(self, @subject) property_with_lang.lang = tag, exact property_with_lang end end |
#lang=(*args) ⇒ Object
Sets lang and match settings
281 282 283 284 285 |
# File 'lib/active_rdf/objectmanager/property.rb', line 281 def lang=(*args) args.flatten! @lang = args[0].sub(/^@/,'') @exact_lang = truefalse(args[1],true) end |
#length ⇒ Object Also known as: size
Returns the number of values assigned to this property for this @subject
288 289 290 |
# File 'lib/active_rdf/objectmanager/property.rb', line 288 def length to_a.length end |
#only ⇒ Object
Ensure the return of only one value assigned to this property for this @subject. If more than 1 value is found, ActiveRdfError is thrown.
295 296 297 298 299 |
# File 'lib/active_rdf/objectmanager/property.rb', line 295 def only entries = self.entries raise ActiveRDF::ActiveRdfError if entries.size > 1 entries[0] end |
#reject!(&block) ⇒ Object
Equivalent to Property#delete_if, but returns nil if no changes were made
302 303 304 305 306 307 308 309 310 311 |
# File 'lib/active_rdf/objectmanager/property.rb', line 302 def reject!(&block) # :yields: key, value change = false each_pair do |key, value| if yield(key, value) delete(value) change = true end end self if change end |
#replace(new) ⇒ Object
Value replacement. Replaces all current value(s) with the new value
314 315 316 317 318 |
# File 'lib/active_rdf/objectmanager/property.rb', line 314 def replace(new) clear add(new) self end |
#to_h ⇒ Object
Returns a hash of copies of all values with indexes. Changes to this hash will not effect the underlying values. Use #add or #replace to persist changes. See also #each_pair
327 328 329 330 331 332 333 |
# File 'lib/active_rdf/objectmanager/property.rb', line 327 def to_h hash = {} to_a.each do |value| hash[get_key(value)] = value end hash end |
#values_at(*args) ⇒ Object
Return an array containing the values for the given keys.
336 337 338 |
# File 'lib/active_rdf/objectmanager/property.rb', line 336 def values_at(*args) args.collect{|md5| self[md5]} end |