Sha256: b46b4e9f48c95bc7f787d583c0b772d640db6fa05b9a02049c991688adabab0c

Contents?: true

Size: 919 Bytes

Versions: 6

Compression:

Stored size: 919 Bytes

Contents

require 'active_support/core_ext/string'

class Kookaburra
  # @private
  module DependencyAccessor
    # Creates a private attr_reader on the class that will raise an exception if
    # the attribute has a nil value when it is called. Useful for attributes
    # that can optionally be set in an object's constructor but it is not an
    # error for them to be missing unless something actually wants to use them.
    def dependency_accessor(*names)
      names.each { |name| define_dependency_accessor(name) }
    end

    private

    def define_dependency_accessor(name)
      define_method(name) do
        instance_variable_get("@#{name}") or raise "No %s object was set on %s initialization." \
          % [name, [self.class.name, 'an Anonymous Class!!!'].reject(&:blank?).first]
      end

      define_method("#{name}=") do |value|
        instance_variable_set("@#{name}", value)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kookaburra-0.23.1 lib/kookaburra/dependency_accessor.rb
kookaburra-0.23.0 lib/kookaburra/dependency_accessor.rb
kookaburra-0.22.3 lib/kookaburra/dependency_accessor.rb
kookaburra-0.22.2 lib/kookaburra/dependency_accessor.rb
kookaburra-0.22.1 lib/kookaburra/dependency_accessor.rb
kookaburra-0.22.0 lib/kookaburra/dependency_accessor.rb