Class: Lumberjack

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

Constant Summary collapse

@@methods_to_keep =
/^__/, /class/, /instance_eval/, /method_missing/,
/instance_variable_(g|s)et/, /instance_variables/, /inspect/, /send/,
/^object_id/, /^respond_to/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_scope) ⇒ Lumberjack

Returns a new instance of Lumberjack.



16
17
18
19
# File 'lib/lumberjack.rb', line 16

def initialize(initial_scope)
  @initial_scope = initial_scope
  @scope_stack ||= []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/lumberjack.rb', line 56

def method_missing(*args, &block)
  if still_modifying_scope?(args, block)
    @scope_stack << args.first
  else
    assign_to_current_scope(args, block)
  end
  self
end

Class Method Details

.construct(initial_scope = [], &block) ⇒ Object



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

def self.construct(initial_scope = [], &block)
  builder = new(initial_scope)
  builder.__process(block)
end

Instance Method Details

#/(ignore_me) ⇒ Object

syntatic sugar for scope resolution



30
31
32
# File 'lib/lumberjack.rb', line 30

def /(ignore_me)
  self
end

#__process(block) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/lumberjack.rb', line 21

def __process(block)
  @scope = [@initial_scope]
  instance_eval(&block) if block
  tree
rescue
  raise $!
end

#graft_branch(branch_name) ⇒ Object



44
45
46
47
48
# File 'lib/lumberjack.rb', line 44

def graft_branch(branch_name)
  branch = instance_variable_get("@#{branch_name}")
  raise "Attemption to graft branch #{branch_name} which is undefined" unless branch
  instance_eval(&branch)
end

#load_tree_file(filename) ⇒ Object



34
35
36
37
38
# File 'lib/lumberjack.rb', line 34

def load_tree_file(filename)
  File.open filename, 'r' do |f|
    eval f.read, binding, filename
  end
end

#prune(method, value) ⇒ Object



50
51
52
53
54
# File 'lib/lumberjack.rb', line 50

def prune(method, value)
  current_scope.delete_if do |twig|
    twig.respond_to?(method) && twig.send(method) == value
  end
end

#shared_branch(branch_name, &block) ⇒ Object



40
41
42
# File 'lib/lumberjack.rb', line 40

def shared_branch(branch_name, &block)
  instance_variable_set "@#{branch_name}", lambda(&block)
end