Module: KDoc::BlockProcessor

Includes:
KLog::Logging
Included in:
Container
Defined in:
lib/k_doc/mixins/block_processor.rb

Overview

A container acts a base data object for any data that requires tagging such as unique key, type and namespace.

example usage of the container using model as the basis:

KDoc.model do
  init do
    context.some_data = :xmen
  end
  settings do
    name context.some_data
  end
  action
    puts context.some_data
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#action_blockObject (readonly)

Returns the value of attribute action_block.



26
27
28
# File 'lib/k_doc/mixins/block_processor.rb', line 26

def action_block
  @action_block
end

#blockObject (readonly)

Returns the value of attribute block.



22
23
24
# File 'lib/k_doc/mixins/block_processor.rb', line 22

def block
  @block
end

#block_stateObject (readonly)

Returns the value of attribute block_state.



23
24
25
# File 'lib/k_doc/mixins/block_processor.rb', line 23

def block_state
  @block_state
end

#childrenObject (readonly)

Returns the value of attribute children.



27
28
29
# File 'lib/k_doc/mixins/block_processor.rb', line 27

def children
  @children
end

#init_blockObject (readonly)

Returns the value of attribute init_block.



25
26
27
# File 'lib/k_doc/mixins/block_processor.rb', line 25

def init_block
  @init_block
end

Instance Method Details

#action(&block) ⇒ Object

rubocop:enable Metrics/AbcSize



146
147
148
# File 'lib/k_doc/mixins/block_processor.rb', line 146

def action(&block)
  @action_block = block
end

#actioned?Boolean

The on_action method has been called.

Returns:

  • (Boolean)


73
74
75
# File 'lib/k_doc/mixins/block_processor.rb', line 73

def actioned?
  @block_state == :actioned
end

#add_child(block) ⇒ Object



111
112
113
# File 'lib/k_doc/mixins/block_processor.rb', line 111

def add_child(block)
  @children << block
end

#children_evaluated?Boolean

The block and the data it represents has been evaluated.

Returns:

  • (Boolean)


68
69
70
# File 'lib/k_doc/mixins/block_processor.rb', line 68

def children_evaluated?
  @block_state == :children_evaluated || actioned?
end

#debug_block_processorObject

rubocop:disable Metrics/AbcSize



167
168
169
170
171
172
173
# File 'lib/k_doc/mixins/block_processor.rb', line 167

def debug_block_processor
  log.kv 'block_state'        , block_state         , debug_pad_size
  log.kv 'new'                , new?                , debug_pad_size
  log.kv 'evaluated'          , evaluated?          , debug_pad_size
  log.kv 'children_evaluated' , children_evaluated? , debug_pad_size
  log.kv 'actioned'           , actioned?           , debug_pad_size
end

#evaluated?Boolean

The main block has been evaluated, but child blocks are still to be processed

Returns:

  • (Boolean)


58
59
60
# File 'lib/k_doc/mixins/block_processor.rb', line 58

def evaluated?
  @block_state == :evaluated || initialized?
end

#execute_block(run_actions: false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/k_doc/mixins/block_processor.rb', line 38

def execute_block(run_actions: false)
  # Evaluate the main block of code
  fire_eval # aka primary eval

  # Call the block of code attached to the init method
  fire_init

  # Call the each block in the child array of blocks in the order of creation (FIFO)
  fire_children

  # Call the block of code attached to the action method
  fire_action if run_actions
end

#fire_actionObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/k_doc/mixins/block_processor.rb', line 150

def fire_action
  return unless children_evaluated?

  if action_block
    instance_eval(&action_block)
    @block_state = :actioned
  end
rescue StandardError => e
  log.error('Standard error while running actions')
  # puts "key #{unique_key}"
  # puts "file #{KUtil.data.console_file_hyperlink(resource.file, resource.file)}"
  log.error(e.message)
  @error = e
  raise
end

#fire_childrenObject

Run blocks associated with the children

A child can follow one of three patterns:

  1. A block that is evaluated immediately against the parent class

  2. A class that has its own custom block evaluation

  3. A class that has a block which will be evaluated immediately against the child class

rubocop:disable Metrics/AbcSize



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/k_doc/mixins/block_processor.rb', line 122

def fire_children
  return unless initialized?

  children.each do |child|
    if child.is_a?(Proc)
      instance_eval(&child)
    elsif child.respond_to?(:fire_eval)
      child.fire_eval
    elsif child.respond_to?(:block)
      child.instance_eval(&child.block)
    end
  end

  @block_state = :children_evaluated
rescue StandardError => e
  log.error('Standard error in document with one of the child blocks')
  # puts "key #{unique_key}"
  # puts "file #{KUtil.data.console_file_hyperlink(resource.file, resource.file)}"
  log.error(e.message)
  @error = e
  raise
end

#fire_evalObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/k_doc/mixins/block_processor.rb', line 77

def fire_eval
  return unless new?

  instance_eval(&block) if block

  @block_state = :evaluated
rescue StandardError => e
  log.error('Standard error in document')
  # puts "key #{unique_key}"
  # puts "file #{KUtil.data.console_file_hyperlink(resource.file, resource.file)}"
  log.error(e.message)
  @error = e
  raise
end

#fire_initObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/k_doc/mixins/block_processor.rb', line 96

def fire_init
  return unless evaluated?

  instance_eval(&init_block) if init_block

  @block_state = :initialized
rescue StandardError => e
  log.error('Standard error in document on_init')
  # puts "key #{unique_key}"
  # puts "file #{KUtil.data.console_file_hyperlink(resource.file, resource.file)}"
  log.error(e.message)
  @error = e
  raise
end

#init(&block) ⇒ Object



92
93
94
# File 'lib/k_doc/mixins/block_processor.rb', line 92

def init(&block)
  @init_block = block
end

#initialize_block_processor(_opts, &block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/k_doc/mixins/block_processor.rb', line 29

def initialize_block_processor(_opts, &block)
  @block = block if block_given?
  @block_state = :new

  @init_block = nil
  @action_block = nil
  @children = []
end

#initialized?Boolean

Has the init block been called?

Returns:

  • (Boolean)


63
64
65
# File 'lib/k_doc/mixins/block_processor.rb', line 63

def initialized?
  @block_state == :initialized || children_evaluated?
end

#new?Boolean

The underlying container is created and in the case of k_manager, registered

Returns:

  • (Boolean)


53
54
55
# File 'lib/k_doc/mixins/block_processor.rb', line 53

def new?
  @block_state == :new
end