Class: Fixably::ResourceLazyLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/fixably/resource_lazy_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:) ⇒ ResourceLazyLoader

Returns a new instance of ResourceLazyLoader.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fixably/resource_lazy_loader.rb', line 8

def initialize(model:)
  if !model.respond_to?(:ancestors) ||
      !model.ancestors.include?(ApplicationResource)
    raise(
      ArgumentError,
      "The model is expected to be a class that extend ApplicationResource"
    )
  end

  @model = model
  @associations_to_expand = Set.new
end

Instance Attribute Details

#associations_to_expandObject (readonly)

Returns the value of attribute associations_to_expand.



6
7
8
# File 'lib/fixably/resource_lazy_loader.rb', line 6

def associations_to_expand
  @associations_to_expand
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/fixably/resource_lazy_loader.rb', line 5

def model
  @model
end

Instance Method Details

#all(*args) ⇒ Object



56
57
58
59
60
61
# File 'lib/fixably/resource_lazy_loader.rb', line 56

def all(*args)
  options = expand_associations(args.slice(0) || {})
  arguments = [options].concat(args[1..] || [])

  model.all(*arguments)
end

#find(*args) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/fixably/resource_lazy_loader.rb', line 34

def find(*args)
  id = args.slice(0)
  options = expand_associations(args.slice(1) || {})
  arguments = [options].concat(args[2..] || [])

  model.find(id, *arguments)
end

#first(*args) ⇒ Object



42
43
44
45
46
47
# File 'lib/fixably/resource_lazy_loader.rb', line 42

def first(*args)
  options = expand_associations(args.slice(0) || {})
  arguments = [options].concat(args[1..] || [])

  model.first(*arguments)
end

#includes(association) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fixably/resource_lazy_loader.rb', line 21

def includes(association)
  unless model.reflections.key?(association)
    raise(
      ArgumentError,
      "#{association} is not a known association of #{model}"
    )
  end

  associations_to_expand << association

  self
end

#last(*args) ⇒ Object



49
50
51
52
53
54
# File 'lib/fixably/resource_lazy_loader.rb', line 49

def last(*args)
  options = expand_associations(args.slice(0) || {})
  arguments = [options].concat(args[1..] || [])

  model.last(*arguments)
end

#where(clauses = {}) ⇒ Object



63
64
65
66
# File 'lib/fixably/resource_lazy_loader.rb', line 63

def where(clauses = {})
  arguments = expand_associations(clauses)
  model.where(arguments)
end