Module: Krakow::Utils::Lazy::ClassMethods

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

Overview

Class methods for laziness

Instance Method Summary collapse

Instance Method Details

#attribute(name, type, options = {}) ⇒ nil

Add new attributes to class

Parameters:

  • name (String)
  • type (Class, Array<Class>)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :required (true, false)

    must be provided on initialization

  • :default (Object, Proc)

    default value

Returns:

  • (nil)


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/krakow/utils/lazy.rb', line 62

def attribute(name, type, options={})
  name = name.to_sym
  attributes[name] = {:type => type}.merge(options)
  define_method(name) do
    arguments[name.to_sym]
  end
  define_method("#{name}?") do
    !!arguments[name.to_sym]
  end
  nil
end

#attributes(*args) ⇒ Array<Hash>

Return attributes

Parameters:

  • args (Symbol)

    :required or :optional

Returns:

  • (Array<Hash>)


78
79
80
81
82
83
84
85
86
87
# File 'lib/krakow/utils/lazy.rb', line 78

def attributes(*args)
  @attributes ||= {}
  if(args.include?(:required))
    Hash[@attributes.find_all{|k,v| v[:required]}]
  elsif(args.include?(:optional))
    Hash[@attributes.find_all{|k,v| !v[:required]}]
  else
    @attributes
  end
end

#set_attributes(attrs) ⇒ TrueClass

TODO:

need deep dup here

Directly set attribute hash

Parameters:

  • attrs (Hash)

Returns:

  • (TrueClass)


94
95
96
97
# File 'lib/krakow/utils/lazy.rb', line 94

def set_attributes(attrs)
  @attributes = attrs.dup
  true
end