Module: LittleBoxes::Strategy

Defined in:
lib/little_boxes/strategy.rb

Class Method Summary collapse

Class Method Details

.configure(block) ⇒ Object



49
50
51
# File 'lib/little_boxes/strategy.rb', line 49

def configure(block)
  -> (bx) { do_configure(block.call(bx), bx) }
end

.configure_then(block, then_block) ⇒ Object



57
58
59
60
61
# File 'lib/little_boxes/strategy.rb', line 57

def configure_then(block, then_block)
  -> (bx) do
    do_configure(block.call(bx), bx).tap{ |v| then_block.call v, bx }
  end
end

.default(block) ⇒ Object



63
64
65
# File 'lib/little_boxes/strategy.rb', line 63

def default(block)
  block
end

.do_configure(subject, box) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/little_boxes/strategy.rb', line 67

def do_configure(subject, box)
  config = {box: box}

  config.default_proc = Proc.new do |h, name|
    h[name] = h[:box][name]
  end

  subject.config = config

  subject
end

.for(block, memo: false, configure: false, then_block: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/little_boxes/strategy.rb', line 5

def for(block, memo: false, configure: false, then_block: nil)
  case [memo, configure, !!then_block]
  when [true, true, true]
    memo_configure_then(block, then_block)
  when [true, true, false]
    memo_configure(block)
  when [true, false, true]
    memo_then(block, then_block)
  when [true, false, false]
    memo(block)
  when [false, true, true]
    configure_then(block, then_block)
  when [false, true, false]
    configure(block)
  when [false, false, true]
    then_block(block, then_block)
  else
    default(block)
  end
end

.memo(block) ⇒ Object



44
45
46
47
# File 'lib/little_boxes/strategy.rb', line 44

def memo(block)
  value = nil
  -> (bx) { value ||= block.call(bx) }
end

.memo_configure(block) ⇒ Object



26
27
28
29
# File 'lib/little_boxes/strategy.rb', line 26

def memo_configure(block)
  value = nil
  -> (bx) { value ||= do_configure(block.call(bx), bx) }
end

.memo_configure_then(block, then_block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/little_boxes/strategy.rb', line 36

def memo_configure_then(block, then_block)
  value = nil
  -> (bx) do
    value ||= do_configure(block.call(bx), bx)
      .tap{ |v| then_block.call v, bx }
  end
end

.memo_then(block, then_block) ⇒ Object



31
32
33
34
# File 'lib/little_boxes/strategy.rb', line 31

def memo_then(block, then_block)
  value = nil
  -> (bx) { value ||= block.call(bx).tap { |v| then_block.call v, bx } }
end

.then_block(block, then_block) ⇒ Object



53
54
55
# File 'lib/little_boxes/strategy.rb', line 53

def then_block(block, then_block)
  -> (bx) { block.call(bx).tap { |v| then_block.call v, bx } }
end