Class: HT::Cascade

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Cascade

Returns a new instance of Cascade.



6
7
8
9
10
11
12
# File 'lib/ht/cascade.rb', line 6

def initialize(name)
  self.name = name
  @cascade = {base: nil}
  if block_given?
    yield(self)
  end
end

Instance Attribute Details

#cascadeObject (readonly)

Returns the value of attribute cascade.



4
5
6
# File 'lib/ht/cascade.rb', line 4

def cascade
  @cascade
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/ht/cascade.rb', line 3

def name
  @name
end

Instance Method Details

#base(&block) ⇒ Object



14
15
16
# File 'lib/ht/cascade.rb', line 14

def base(&block)
  @cascade[:base] = {depends: nil, block: block}
end

#build(name, opts) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ht/cascade.rb', line 22

def build(name, opts)
  @result = {}
  
  top = @cascade[name]
  dependency_list = build_dependency_list(name).reverse
  dependency_list.each do |dependency|
    next unless @cascade.has_key?(dependency) && @cascade[dependency].has_key?(:block)
    @cascade[dependency][:block].call(self, opts)
  end
  
  top[:block].call(self, opts)
  
  @result
end

#build_dependency_list(name, cascade = @cascade) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/ht/cascade.rb', line 37

def build_dependency_list(name, cascade = @cascade)
  if direct_dependency = cascade[name][:depends]
    [direct_dependency] + build_dependency_list(direct_dependency, cascade)
  else 
    []
  end
end

#get_direct_dependencies(name, cascade) ⇒ Object



45
46
47
# File 'lib/ht/cascade.rb', line 45

def get_direct_dependencies(name, cascade)
  (cascade[name] || {})[:depends] || []
end

#get_value(k) ⇒ Object



55
56
57
# File 'lib/ht/cascade.rb', line 55

def get_value(k)
  @result[k] if @result
end

#layer(name, dependency = :base, &block) ⇒ Object



18
19
20
# File 'lib/ht/cascade.rb', line 18

def layer(name, dependency=:base, &block)
  @cascade[name] = {depends: dependency, block: block}
end

#set_value(k, v) ⇒ Object



49
50
51
52
53
# File 'lib/ht/cascade.rb', line 49

def set_value(k, v)
  return unless @result
  
  @result[k] = v
end