module Krakow::Utils::Lazy::ClassMethods

Class methods for laziness

Public Instance Methods

attribute(name, type, options={}) click to toggle source

Add new attributes to class

@param name [String] @param type [Class, Array<Class>] @param options [Hash] @option options [true, false] :required must be provided on initialization @option options [Object, Proc] :default default value @return [nil]

# 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) click to toggle source

Return attributes

@param args [Symbol] :required or :optional @return [Array<Hash>]

# 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) click to toggle source

Directly set attribute hash

@param attrs [Hash] @return [TrueClass] @todo need deep dup here

# File lib/krakow/utils/lazy.rb, line 94
def set_attributes(attrs)
  @attributes = attrs.dup
  true
end