Class: PuppetForge::LazyAccessors::AccessorContainer

Inherits:
Module
  • Object
show all
Defined in:
lib/puppet_forge/lazy_accessors.rb

Overview

A Module subclass for attribute accessors.

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ AccessorContainer

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



100
101
102
# File 'lib/puppet_forge/lazy_accessors.rb', line 100

def initialize(base)
  base.send(:include, self)
end

Instance Method Details

#add_attributes(keys) ⇒ void

This method returns an undefined value.

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 PuppetForge::LazyAccessors#fetch any missing data.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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