Class: Ryext::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ryext/query.rb

Constant Summary collapse

PARAM_KEYS =
[
  :start, :limit
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, params = {}) ⇒ Query

Returns a new instance of Query.



11
12
13
14
15
16
# File 'lib/ryext/query.rb', line 11

def initialize(klass, params = {})
  @parent_klass = klass
  @params = params

  extract_from_params
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



5
6
7
# File 'lib/ryext/query.rb', line 5

def 
  @account
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/ryext/query.rb', line 4

def params
  @params
end

#parent_klassObject (readonly)

Returns the value of attribute parent_klass.



4
5
6
# File 'lib/ryext/query.rb', line 4

def parent_klass
  @parent_klass
end

Instance Method Details

#base_urlObject



26
27
28
# File 'lib/ryext/query.rb', line 26

def base_url
  @parent_klass.base_url(@params)
end

#collectionObject



52
53
54
55
# File 'lib/ryext/query.rb', line 52

def collection
  load unless loaded?
  @collection
end

#each(&block) ⇒ Object



38
39
40
# File 'lib/ryext/query.rb', line 38

def each(&block)
  collection.each(&block)
end

#extract_from_paramsObject



18
19
20
# File 'lib/ryext/query.rb', line 18

def extract_from_params
  @account = @params.delete(:account)
end

#instance_klassObject



22
23
24
# File 'lib/ryext/query.rb', line 22

def instance_klass
  @parent_klass.instance_klass
end

#loadObject



46
47
48
49
50
# File 'lib/ryext/query.rb', line 46

def load
  response = request_for_query
  @collection = response.collection.map { |x| @parent_klass.new(x, @account) }
  @loaded = true
end

#loaded?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/ryext/query.rb', line 42

def loaded?
  @loaded
end

#request_for_queryObject



57
58
59
# File 'lib/ryext/query.rb', line 57

def request_for_query
   && .request(self)
end

#to_paramsObject



30
31
32
33
34
35
36
# File 'lib/ryext/query.rb', line 30

def to_params
  if @parent_klass.respond_to?(:clean_params)
    @parent_klass.clean_params(@params)
  else
    @params
  end
end