Class: Tumblr::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/tumblr/query.rb

Defined Under Namespace

Modules: DelegateMethods

Instance Method Summary collapse

Constructor Details

#initialize(*parameters, &block) ⇒ Query

Returns a new instance of Query.



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

def initialize(*parameters, &block)
  @params = {}
  @param_keys = parameters.to_a
  @fetch_block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tumblr/query.rb', line 35

def method_missing(method, *args, &block)
  if @param_keys.include?(method.to_sym)
    value = args.length == 1 ? args.first : args
    @array = nil if value != @params[method.to_sym]
    @params[method.to_sym] = value
    self
  elsif to_a.respond_to?(method)
    to_a.send(method, *args, &block)
  else
    super(method, *args, &block)
  end
end

Instance Method Details

#allObject



23
24
25
# File 'lib/tumblr/query.rb', line 23

def all
  to_a
end

#to_aObject



27
28
29
30
31
32
33
# File 'lib/tumblr/query.rb', line 27

def to_a
  if @array && @array.length > 0
    @array
  else
    @array = @fetch_block.call(@params)
  end
end