Class: RDFMapper::Scope::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/scope/loader.rb

Overview

Loader is responsible for loading and updating model attributes. An instance of Loader is assigned to each search query and association.

Instance Method Summary collapse

Constructor Details

#initialize(cls, options = {}) ⇒ Loader

Returns a new instance of Loader.



9
10
11
12
13
# File 'lib/lib/scope/loader.rb', line 9

def initialize(cls, options = {})
  @conditions = Query.new(cls, options)
  @objects = []
  @cls = cls
end

Instance Method Details

#type(adapter, options = {}) ⇒ Object #type(collection) ⇒ Object

Sets data adapter or collection.

Overloads:

  • #type(adapter, options = {}) ⇒ Object

    Sets data adapter, this will override default model adapter

    Parameters:

    • adapter (Symbol)

      (:rails, :sparql, :rest)

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

      options to pass on to the adapter constructor

    Returns:

    • (Object)

      adapter instance

  • #type(collection) ⇒ Object

    Sets collection of instances that should be queried.

    Parameters:

    • collection (Array)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lib/scope/loader.rb', line 39

def from(adapter_or_collection, options = {})
  if adapter_or_collection.kind_of? Array
    @loaded = true
    adapter_or_collection.select do |instance|
      @conditions.matches?(instance)
    end.each do |instance|
      @objects << instance.properties
    end
    return 
  end
  @adapter = RDFMapper::Adapters.register(adapter_or_collection, @cls, options)
end

#get(index) ⇒ Object

Creates a new ‘scoped’ instance of RDFMapper::Model.

Parameters:

  • index (Integer)

Returns:

  • (Object)


67
68
69
70
# File 'lib/lib/scope/loader.rb', line 67

def get(index)
  instance = @cls.new(@objects[index])
  RDFMapper::Scope.apply(instance, self, index)
end

#has_id?RDF::URI?

Checks if model ID is specified within conditions. Returns RDF::URI if found, nil otherwise.

Returns:

  • (RDF::URI)
  • (nil)


22
23
24
# File 'lib/lib/scope/loader.rb', line 22

def has_id?
  @conditions[:id].nil? ? nil : RDF::URI.new(@conditions[:id])
end

#inspectString

Developer-friendly representation of the instance

Returns:

  • (String)


95
96
97
# File 'lib/lib/scope/loader.rb', line 95

def inspect #nodoc
  "#<%sLoader:%s>" % [@cls, object_id]
end

#lengthInteger

Returns the number of loaded objects.

Returns:

  • (Integer)


57
58
59
# File 'lib/lib/scope/loader.rb', line 57

def length
  load.length
end

#update(index, instance = nil) ⇒ Object

Updates an existing ‘scoped’ instance of RDFMapper::Model (sets ID and attributes).

Parameters:

  • index (Integer)
  • instance (Object) (defaults to: nil)

Returns:

  • (Object)


80
81
82
83
84
85
86
87
88
# File 'lib/lib/scope/loader.rb', line 80

def update(index, instance = nil) #nodoc
  atts = load[index]
  if atts.nil?
    return instance.send(:nil!)
  end
  instance.send(:id=, atts[:id])
  instance.attributes = atts
  instance
end