Sha256: 78c10725017c1da9f59a24d488530daa2a63c395e7861cd4a7d6b6026a446bb1

Contents?: true

Size: 914 Bytes

Versions: 15

Compression:

Stored size: 914 Bytes

Contents

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
        class_name = self.class.name
        class_name.sub!(/^$/, 'an Anonymous Class!!!')
        instance_variable_get("@#{name}") or raise "No %s object was set on %s initialization." \
          % [name, class_name]
      end

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
kookaburra-2.0.0 lib/kookaburra/dependency_accessor.rb
kookaburra-1.3.1 lib/kookaburra/dependency_accessor.rb
kookaburra-1.3.0 lib/kookaburra/dependency_accessor.rb
kookaburra-1.2.3 lib/kookaburra/dependency_accessor.rb
kookaburra-1.2.2 lib/kookaburra/dependency_accessor.rb
kookaburra-1.2.1 lib/kookaburra/dependency_accessor.rb
kookaburra-1.2.0 lib/kookaburra/dependency_accessor.rb
kookaburra-1.1.0 lib/kookaburra/dependency_accessor.rb
kookaburra-1.0.0 lib/kookaburra/dependency_accessor.rb
kookaburra-0.27.0 lib/kookaburra/dependency_accessor.rb
kookaburra-0.26.1 lib/kookaburra/dependency_accessor.rb
kookaburra-0.26.0 lib/kookaburra/dependency_accessor.rb
kookaburra-0.25.0 lib/kookaburra/dependency_accessor.rb
kookaburra-0.24.1 lib/kookaburra/dependency_accessor.rb
kookaburra-0.24.0 lib/kookaburra/dependency_accessor.rb