Module: PuppetForge::LazyRelations

Included in:
V3::Base
Defined in:
lib/puppet_forge/lazy_relations.rb

Overview

This module provides convenience accessors for related resources. Related classes will include LazyAccessors, allowing them to transparently fetch fetch more complete representations from the API.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Mask mistaken ‘include` calls by transparently extending this class.



12
13
14
# File 'lib/puppet_forge/lazy_relations.rb', line 12

def self.included(base)
  base.extend(self)
end

Instance Method Details

#lazy(name, class_name = name) ⇒ Object

Declares a new lazily loaded property.

This is particularly useful since our data hashes tend to contain at least a partial representation of the related object. This mechanism provides a proxy object which will avoid making HTTP requests until it is asked for a property it does not contain, at which point it will fetch the related object from the API and look up the property from there.

Parameters:

  • name (Symbol)

    the name of the lazy attribute

  • class_name (#to_s) (defaults to: name)

    the lazy relation’s class name



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet_forge/lazy_relations.rb', line 49

def lazy(name, class_name = name)
  klass = (class_name.is_a?(Class) ? class_name : nil)
  class_name = "#{class_name}"

  define_method(name) do
    @_lazy ||= {}

    @_lazy[name] ||= begin

      klass ||= parent.const_get(class_name)

      klass.send(:include, PuppetForge::LazyAccessors)
      fetch unless has_attribute?(name)
      value = attributes[name]
      klass.new(value) if value
    end
  end
end

#lazy_collection(name, class_name = name) ⇒ Object

Declares a new lazily loaded collection.

This behaves like #lazy, with the exception that the underlying attribute is an array of property hashes, representing several distinct models. In this case, we return an array of proxy objects, one for each property hash.

It’s also worth pointing out that this is not a paginated collection. Since the array of property hashes we’re wrapping is itself unpaginated and complete (if shallow), wrapping it in a paginated collection doesn’t provide any semantic value.

Parameters:

  • name (Symbol)

    the name of the lazy collection attribute

  • class_name (#to_s) (defaults to: name)

    the lazy relation’s class name

See Also:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/puppet_forge/lazy_relations.rb', line 89

def lazy_collection(name, class_name = name)
  klass = (class_name.is_a?(Class) ? class_name : nil)
  class_name = "#{class_name}"

  define_method(name) do
    @_lazy ||= {}

    @_lazy[name] ||= begin
      klass ||= parent.const_get(class_name)
      klass.send(:include, PuppetForge::LazyAccessors)
      fetch unless has_attribute?(name)
      (attribute(name) || []).map { |x| klass.new(x) }
    end
  end
end

#nameArray<class_name>

Returns a lazily-loaded proxy for a collection of name. To eagerly load any one of these name, call #fetch.

Returns:

  • (Array<class_name>)

    a proxy for the related collection of name



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet_forge/lazy_relations.rb', line 49

def lazy(name, class_name = name)
  klass = (class_name.is_a?(Class) ? class_name : nil)
  class_name = "#{class_name}"

  define_method(name) do
    @_lazy ||= {}

    @_lazy[name] ||= begin

      klass ||= parent.const_get(class_name)

      klass.send(:include, PuppetForge::LazyAccessors)
      fetch unless has_attribute?(name)
      value = attributes[name]
      klass.new(value) if value
    end
  end
end

#parentObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/puppet_forge/lazy_relations.rb', line 16

def parent
  if self.is_a? Class
    class_name = self.name
  else
    class_name = self.class.name
  end

  # Get the name of the version module
  version = class_name.split("::")[-2]

  if version.nil?
    raise RuntimeError, "Unable to determine the parent PuppetForge version module"
  end

  PuppetForge.const_get(version)
end