Module: Extree::Seed

Included in:
Root
Defined in:
lib/extree/seed.rb

Constant Summary collapse

BRANCH_METHOD_PREFIX =
"extree_branch_".freeze
@@call_stack =
[]
@@call_roots =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.close_scope(object) ⇒ Object



33
34
35
# File 'lib/extree/seed.rb', line 33

def close_scope object
  @@call_roots.delete_at @@call_roots.index(object)
end

.current(root) ⇒ Object



17
18
19
# File 'lib/extree/seed.rb', line 17

def current root
  @@call_roots.include?(root) ? @@call_stack.last : root
end

.open_scope(object) ⇒ Object



29
30
31
# File 'lib/extree/seed.rb', line 29

def open_scope object
  @@call_roots << object
end

.popObject



25
26
27
# File 'lib/extree/seed.rb', line 25

def pop
  @@call_stack.pop
end

.push(object) ⇒ Object



21
22
23
# File 'lib/extree/seed.rb', line 21

def push object
  @@call_stack.push object
end

.rootsObject



13
14
15
# File 'lib/extree/seed.rb', line 13

def roots
  @@call_roots
end

.stackObject



9
10
11
# File 'lib/extree/seed.rb', line 9

def stack
  @@call_stack
end

Instance Method Details

#extree_method_missing(name, *a, **na, &b) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/extree/seed.rb', line 47

def extree_method_missing name, *a, **na, &b
  top = Seed.current self
  branch_method = "#{BRANCH_METHOD_PREFIX}#{name}".to_sym
  return top.send(branch_method, *a, **na, &b) if top.respond_to? branch_method
  setter = "#{name}=".to_sym
  if top.respond_to? setter
    return top.send(setter, a) if !a.empty?
    return top.send(setter, na) if !na.empty?
    return top.send(setter, b) if block_given?
    return top.send(setter)
  end
  no_method_error = NoMethodError.new("extree method missing `#{name}!` for #{top.class}")
  raise no_method_error
end

#extree_respond_to?(name) ⇒ Boolean



42
43
44
45
# File 'lib/extree/seed.rb', line 42

def extree_respond_to? name
  top = Seed.current self
  top.respond_to?("#{name}=") || top.respond_to?("#{BRANCH_METHOD_PREFIX}#{name}")
end

#host!(&b) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/extree/seed.rb', line 67

def host! &b
  return self if !b
  Seed.push self
  result = b.call self
  Seed.pop
  result
end

#scope!(scoped = nil, *a, **na, &b) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/extree/seed.rb', line 75

def scope! scoped = nil, *a, **na, &b
  scoped ||= self
  Seed.open_scope self
  scoped.host! *a, **na, &b
  Seed.close_scope self
  scoped
end

#self!Object



38
39
40
# File 'lib/extree/seed.rb', line 38

def self!
  Seed.current self
end

#send!(method, *a, **na, &b) ⇒ Object

Call method on receiver, ignore call stack



63
64
65
# File 'lib/extree/seed.rb', line 63

def send! method, *a, **na, &b
  send "#{BRANCH_METHOD_PREFIX}#{method}", *a, **na, &b
end