Class: Flexirest::LazyAssociationLoader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/flexirest/lazy_association_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, value, request, options = {}) ⇒ LazyAssociationLoader

Returns a new instance of LazyAssociationLoader.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/flexirest/lazy_association_loader.rb', line 7

def initialize(name, value, request, options = {})
  @name = name
  class_to_map = request.method[:options][:lazy][name] rescue nil
  @request = class_to_map.nil? ? request : Flexirest::Request.new(class_to_map._mapped_method(:find), class_to_map.new, options)
  @object = nil
  @options = options
  if value.is_a? Array
    @subloaders = value.map {|url| LazyAssociationLoader.new(name, url, request, options)}
  elsif value.is_a?(Hash) && (value.has_key?("url") || value.has_key?(:url))
    @url = (value["url"] || value[:url])
  elsif value.is_a?(Hash) && (value.has_key?("href") || value.has_key?(:href)) # HAL
    @url = (value["href"] || value[:href])
    @_hal_attributes = HashWithIndifferentAccess.new(value)
  elsif value.is_a?(Hash)
    mapped = {}
    value.each do |k,v|
      mapped[k.to_sym] = LazyAssociationLoader.new(name, v, request, options)
    end
    @subloaders = mapped
    # Need to also ensure that the hash/wrapped object is returned when the property is accessed
  elsif value.is_a? String
    @url = value
  else
    raise InvalidLazyAssociationContentException.new("Invalid content for #{@name}, expected Array, String or Hash containing 'url' key")
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/flexirest/lazy_association_loader.rb', line 76

def method_missing(name, *args)
  if @subloaders.is_a? Hash
    return @subloaders[name.to_sym]
  end
  ensure_lazy_loaded
  if @object
    @object.send(name, *args)
  end
end

Instance Method Details

#_hal_attributes(key) ⇒ Object



34
35
36
# File 'lib/flexirest/lazy_association_loader.rb', line 34

def _hal_attributes(key)
  @_hal_attributes[key]
end

#eachObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/flexirest/lazy_association_loader.rb', line 47

def each
  if @subloaders
    if @subloaders.is_a? Array
      @subloaders.each do |loader|
        yield loader
      end
    elsif @subloaders.is_a? Hash
      @subloaders.each do |key, value|
        yield key, value
      end
    end
  else
    ensure_lazy_loaded
    if @object.is_a? Flexirest::Base
      @object.each do |key, value|
        yield key, value
      end
    else
      @object.each do |obj|
        yield obj
      end
    end
  end
end

#keysObject



72
73
74
# File 'lib/flexirest/lazy_association_loader.rb', line 72

def keys
  @subloaders.keys
end

#sizeObject



38
39
40
41
42
43
44
45
# File 'lib/flexirest/lazy_association_loader.rb', line 38

def size
  if @subloaders
    @subloaders.size
  else
    ensure_lazy_loaded
    @object.size
  end
end