Class: Doo::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/doo/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(variables = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
# File 'lib/doo/base.rb', line 8

def initialize(variables = {})
  using variables
end

Instance Method Details

#load(filename) ⇒ Object



3
4
5
6
# File 'lib/doo/base.rb', line 3

def load(filename)
  filename = find_file_in_load_path(filename)
  instance_eval(File.read(filename), filename)
end

#set(k, v = nil, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/doo/base.rb', line 16

def set(k, v=nil, &block)
  singleton_class.class_eval do
    define_method k, (block_given?)? Proc.new { |*args|
      if args.empty?
        instance_eval &block
      else
        with_clone(args[0], &block)
      end
    } : lambda {v}
  end
end

#using(variables = {}) ⇒ Object



12
13
14
# File 'lib/doo/base.rb', line 12

def using(variables = {})
  variables.each { |k,v| set k, v }
end

#with_clone(variables = {}, &block) ⇒ Object



28
29
30
31
32
33
# File 'lib/doo/base.rb', line 28

def with_clone(variables = {}, &block)
  obj = clone
  obj.using variables
  obj.instance_eval &block if block_given?
  obj
end