Class: Doo::Base
- Inherits:
-
Object
- Object
- Doo::Base
- Defined in:
- lib/doo/base.rb
Instance Method Summary collapse
-
#initialize(variables = {}) ⇒ Base
constructor
A new instance of Base.
- #load(filename) ⇒ Object
- #set(k, v = nil, &block) ⇒ Object
- #using(variables = {}) ⇒ Object
- #with_clone(variables = {}, &block) ⇒ Object
Constructor Details
#initialize(variables = {}) ⇒ Base
Returns a new instance of Base.
7 8 9 |
# File 'lib/doo/base.rb', line 7 def initialize(variables = {}) using variables end |
Instance Method Details
#load(filename) ⇒ Object
3 4 5 |
# File 'lib/doo/base.rb', line 3 def load(filename) instance_eval(File.read(filename), filename) end |
#set(k, v = nil, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/doo/base.rb', line 15 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
11 12 13 |
# File 'lib/doo/base.rb', line 11 def using(variables = {}) variables.each { |k,v| set k, v } end |
#with_clone(variables = {}, &block) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/doo/base.rb', line 27 def with_clone(variables = {}, &block) obj = clone obj.using variables obj.instance_eval &block if block_given? obj end |