Class: Spider::Model::Request

Inherits:
ModelHash show all
Defined in:
lib/spiderfw/model/request.rb

Overview

The request object specifies which data is to be loaded for a model. It is similar in purpose to the SELECT … part of an SQL query.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ModelHash

#[], #[]=, #get_deep_obj, #modelhash_orig_set

Constructor Details

#initialize(val = nil, params = {}) ⇒ Request

Returns a new instance of Request.

Parameters:

  • value (Array|Hash)

    Value to initialize the Request with. May be a Hash, or an Array of elements.

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

    Params may have:

    • :total_rows Request the total rows corresponding to the Query from the storage



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spiderfw/model/request.rb', line 19

def initialize(val=nil, params={})
    if (val.is_a?(Array))
        super()
        val.each{ |v| request(v) }
    else
        super(val)
    end
    @total_rows = params[:total_rows]
    @polymorphs = {}
    @expandable = true
end

Instance Attribute Details

#expandablebool

Returns if true, the request will be expanded with lazy groups on load.

Returns:

  • (bool)

    if true, the request will be expanded with lazy groups on load



14
15
16
# File 'lib/spiderfw/model/request.rb', line 14

def expandable
  @expandable
end

#polymorphsbool (readonly)

Returns find also the given subclasses of the queried model.

Returns:

  • (bool)

    find also the given subclasses of the queried model.



12
13
14
# File 'lib/spiderfw/model/request.rb', line 12

def polymorphs
  @polymorphs
end

#total_rowsbool

Returns if true, the total number of rows returned by the query is requested.

Returns:

  • (bool)

    if true, the total number of rows returned by the query is requested.



10
11
12
# File 'lib/spiderfw/model/request.rb', line 10

def total_rows
  @total_rows
end

Class Method Details

.strict(val = nil, params = {}) ⇒ Request

Initializes a Request that should not be expanded by the Mapper

Parameters:

  • val (Array|Hash) (defaults to: nil)
  • params (Hash) (defaults to: {})

Returns:



35
36
37
38
39
# File 'lib/spiderfw/model/request.rb', line 35

def self.strict(val=nil, params={})
    r = self.new(val, params)
    r.expandable = false
    r
end

Instance Method Details

#expandable?bool

Returns true if the Request can be expanded by the mapper (using lazy groups).

Returns:

  • (bool)

    true if the Request can be expanded by the mapper (using lazy groups)



95
96
97
# File 'lib/spiderfw/model/request.rb', line 95

def expandable?
    @expandable
end

#only_polymorphsself

Requests that only the subclasses requested with #with_polymorphs are returned, not the mapper’s base class

Returns:

  • (self)


71
72
73
74
# File 'lib/spiderfw/model/request.rb', line 71

def only_polymorphs
    @only_polymorphs = true
    return self
end

#only_polymorphs?bool

Returns True if only polymorphs should be returned.

Returns:

  • (bool)

    True if only polymorphs should be returned



77
78
79
# File 'lib/spiderfw/model/request.rb', line 77

def only_polymorphs?
    @only_polymorphs
end

#polymorphs?bool

Returns True if there are requested polymorphs.

Returns:

  • (bool)

    True if there are requested polymorphs



64
65
66
# File 'lib/spiderfw/model/request.rb', line 64

def polymorphs?
    @polymorphs.empty? ? false : true
end

#request(element) ⇒ void

This method returns an undefined value.

Parameters:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spiderfw/model/request.rb', line 43

def request(element) # :nodoc:
    if element.is_a?(Element)
        self[element.name.to_s] = true
    elsif element.is_a?(String)
        element.split(',').each do |el|
            self[el.strip] = true
        end
    else
        self[element] = true
    end
end

#with_polymorphs(type, request) ⇒ Object

Requests that the mapper looks for subclasses of the given type, loading additional subclass specific elements specified in the request

Parameters:

  • Class<BaseModel] (Class<BaseModel] type The subclass)

    type The subclass

  • request (Request)

    Request for subclass specific elements



59
60
61
# File 'lib/spiderfw/model/request.rb', line 59

def with_polymorphs(type, request)
    @polymorphs[type] = request
end

#with_superclassself

Requests that the mapper retrieves also objects belonging to the model’s superclass

Returns:

  • (self)


83
84
85
86
# File 'lib/spiderfw/model/request.rb', line 83

def with_superclass
    @with_superclass = true
    return self
end

#with_superclass?bool

Returns true if the superclass was requested with #with_superclass.

Returns:



89
90
91
# File 'lib/spiderfw/model/request.rb', line 89

def with_superclass?
    @with_superclass
end