Module: Krakow::Utils::Lazy::InstanceMethods

Included in:
Distribution, FrameType, Producer::Http
Defined in:
lib/krakow/utils/lazy.rb

Overview

Instance methods for laziness

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsHash (readonly)

Returns argument hash.

Returns:

  • (Hash)

    argument hash



14
15
16
# File 'lib/krakow/utils/lazy.rb', line 14

def arguments
  @arguments
end

Instance Method Details

#initialize(args = {}) ⇒ Object Also known as: super_init

Create new instance

Parameters:

  • args (Hash) (defaults to: {})

Returns:

  • (Object)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/krakow/utils/lazy.rb', line 20

def initialize(args={})
  @arguments = {}.tap do |hash|
    self.class.attributes.each do |name, options|
      val = args[name]
      if(options[:required] && !args.has_key?(name))
        raise ArgumentError.new("Missing required option: `#{name}`")
      end
      if(val && options[:type] && !(valid = [options[:type]].flatten.compact).detect{|k| val.is_a?(k)})
        raise TypeError.new("Invalid type for option `#{name}` (#{val} <#{val.class}>). Valid - #{valid.map(&:to_s).join(',')}")
      end
      if(val.nil? && options[:default] && !args.has_key?(name))
        val = options[:default].respond_to?(:call) ? options[:default].call : options[:default]
      end
      hash[name] = val
    end
  end
end

#inspectString

Returns:

  • (String)


45
46
47
# File 'lib/krakow/utils/lazy.rb', line 45

def inspect
  "<#{self.class.name}:#{object_id} [#{arguments.inspect}]>"
end

#to_sString

Returns:

  • (String)


40
41
42
# File 'lib/krakow/utils/lazy.rb', line 40

def to_s
  "<#{self.class.name}:#{object_id}>"
end