Class: Text::Gen::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/text/gen/context.rb

Overview

Keep the context for one run.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store:, filters: nil, meta: nil, max_recursion: 10) ⇒ Context

Returns a new instance of Context.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/text/gen/context.rb', line 9

def initialize(store:, filters: nil, meta: nil, max_recursion: 10)
  @depth = 0
  @store = store

  # Request filters are added to the top level builder
  @filters = (filters || []).map { |hsh| Filter.build(hsh, 1) }.compact
  @meta = meta
  @keys = []
  @strategy_stack = []
  @remembered = []
  @max_recursion = max_recursion
end

Instance Attribute Details

#depthObject (readonly)

Returns the value of attribute depth.



7
8
9
# File 'lib/text/gen/context.rb', line 7

def depth
  @depth
end

#storeObject (readonly)

Returns the value of attribute store.



7
8
9
# File 'lib/text/gen/context.rb', line 7

def store
  @store
end

Instance Method Details

#apply_builder_filters(builder) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/text/gen/context.rb', line 78

def apply_builder_filters(builder)
  @filters.each do |filter|
    builder = filter.builder(self, builder) if filter.respond_to?(:builder)
    return unless builder
    return builder if builder.is_a?(Text::Gen::Result)
  end

  builder
end

#apply_item_filters(item) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/text/gen/context.rb', line 97

def apply_item_filters(item)
  @filters.each do |filter|
    item = filter.item(self, item) if filter.respond_to?(:item)
    return unless item
  end

  item
end

#apply_item_list_filters(items) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/text/gen/context.rb', line 88

def apply_item_list_filters(items)
  @filters.each do |filter|
    items = filter.items(self, items) if filter.respond_to?(:items)
    return [] if items.nil? || items.empty?
  end

  items
end

#apply_result_filters(result) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/text/gen/context.rb', line 115

def apply_result_filters(result)
  @filters.each do |filter|
    next unless filter.respond_to?(:result)
    next if result.filter_match?(filter.component_key)

    result = filter.result(self, result)
    return unless result
  end

  result
end

#apply_segment_filters(segment) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/text/gen/context.rb', line 106

def apply_segment_filters(segment)
  @filters.each do |filter|
    segment = filter.segment(self, segment) if filter.respond_to?(:segment)
    return if segment.nil? || segment.empty?
  end

  [segment]
end

#ascend!Object



29
30
31
32
33
# File 'lib/text/gen/context.rb', line 29

def ascend!
  @depth -= 1
  @keys.pop
  @strategy_stack.pop
end

#current_keyObject



48
49
50
# File 'lib/text/gen/context.rb', line 48

def current_key
  @keys.last
end

#current_modifierObject



56
57
58
# File 'lib/text/gen/context.rb', line 56

def current_modifier
  @strategy_stack.last&.[](1)
end

#current_strategyObject



52
53
54
# File 'lib/text/gen/context.rb', line 52

def current_strategy
  @strategy_stack.last&.first || "random"
end

#descend!(builder) ⇒ Object

Raises:



22
23
24
25
26
27
# File 'lib/text/gen/context.rb', line 22

def descend!(builder)
  @depth += 1
  @keys << builder["key"]
  @strategy_stack << (builder["strategy"] || "random").split(":", 2)
  raise MaxRecursionError if @depth > @max_recursion
end

#remember(result) ⇒ Object



60
61
62
63
# File 'lib/text/gen/context.rb', line 60

def remember(result)
  @remembered << [current_key, result]
  result
end

#remember_checkpointObject



70
71
72
# File 'lib/text/gen/context.rb', line 70

def remember_checkpoint
  @remembered.length
end

#remembered(key = nil) ⇒ Object



65
66
67
68
# File 'lib/text/gen/context.rb', line 65

def remembered(key = nil)
  key ||= current_key
  @remembered.select { |k, _r| k == key }.map(&:last)
end

#restore_remember(checkpoint) ⇒ Object



74
75
76
# File 'lib/text/gen/context.rb', line 74

def restore_remember(checkpoint)
  @remembered = @remembered[0...checkpoint]
end

#with_filters(filters, offset: 0) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/text/gen/context.rb', line 35

def with_filters(filters, offset: 0)
  filters_to_add = (filters || []).filter_map { |hsh| Filter.build(hsh, depth + offset) }
  count = filters_to_add.size

  @filters.unshift(*filters_to_add)

  begin
    yield
  ensure
    @filters.shift(count)
  end
end