class PuppetForge::LazyAccessors::AccessorContainer

A Module subclass for attribute accessors.

Public Class Methods

new(base) click to toggle source

Creating a new instance of this class will automatically include itself into the provided class.

@param base [Class] the class this container belongs to

# File lib/puppet_forge/lazy_accessors.rb, line 100
def initialize(base)
  base.send(:include, self)
end

Public Instance Methods

add_attributes(keys) click to toggle source

Adds attribute accessors, predicates, and mutators for the named keys. Since these methods will also be instantly available on all instances of the parent class, each of these methods will also conservatively {LazyAccessors#fetch} any missing data.

@param keys [Array<#to_s>] the list of attributes to create @return [void]

# File lib/puppet_forge/lazy_accessors.rb, line 111
def add_attributes(keys)
  keys.each do |key|
    next if methods.include?(name = key)

    define_method("#{name}") do
      fetch unless has_attribute?(name)
      attribute(name)
    end

    define_method("#{name}?") do
      fetch unless has_attribute?(name)
      has_attribute?(name)
    end

    define_method("#{name}=") do |value|
      fetch unless has_attribute?(name)
      attributes[name] = value
    end
  end
end