Class: Text::Gen::Context
- Inherits:
-
Object
- Object
- Text::Gen::Context
- Defined in:
- lib/text/gen/context.rb
Overview
Keep the context for one run.
Constant Summary collapse
- KNOWN_STRATEGIES =
Set.new(%w[random weighted sequence sample])
Instance Attribute Summary collapse
-
#depth ⇒ Object
readonly
Returns the value of attribute depth.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
- #apply_builder_filters(builder) ⇒ Object
- #apply_item_filters(item) ⇒ Object
- #apply_item_list_filters(items) ⇒ Object
- #apply_result_filters(result) ⇒ Object
- #apply_segment_filters(segment) ⇒ Object
- #ascend! ⇒ Object
- #current_key ⇒ Object
- #current_modifier ⇒ Object
- #current_strategy ⇒ Object
- #descend!(builder) ⇒ Object
-
#initialize(store:, filters: nil, meta: nil, max_recursion: 10) ⇒ Context
constructor
A new instance of Context.
- #remember(result) ⇒ Object
- #remember_checkpoint ⇒ Object
- #remembered(key = nil) ⇒ Object
- #restore_remember(checkpoint) ⇒ Object
- #strategy_to_stack_item(builder) ⇒ Object
- #with_filters(filters, offset: 0) ⇒ Object
Constructor Details
#initialize(store:, filters: nil, meta: nil, max_recursion: 10) ⇒ Context
Returns a new instance of Context.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/text/gen/context.rb', line 11 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 = @keys = [] @strategy_stack = [] @remembered = [] @max_recursion = max_recursion end |
Instance Attribute Details
#depth ⇒ Object (readonly)
Returns the value of attribute depth.
9 10 11 |
# File 'lib/text/gen/context.rb', line 9 def depth @depth end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
9 10 11 |
# File 'lib/text/gen/context.rb', line 9 def store @store end |
Instance Method Details
#apply_builder_filters(builder) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/text/gen/context.rb', line 89 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
108 109 110 111 112 113 114 115 |
# File 'lib/text/gen/context.rb', line 108 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
99 100 101 102 103 104 105 106 |
# File 'lib/text/gen/context.rb', line 99 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
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/text/gen/context.rb', line 126 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
117 118 119 120 121 122 123 124 |
# File 'lib/text/gen/context.rb', line 117 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
40 41 42 43 44 |
# File 'lib/text/gen/context.rb', line 40 def ascend! @depth -= 1 @keys.pop @strategy_stack.pop end |
#current_key ⇒ Object
59 60 61 |
# File 'lib/text/gen/context.rb', line 59 def current_key @keys.last end |
#current_modifier ⇒ Object
67 68 69 |
# File 'lib/text/gen/context.rb', line 67 def current_modifier @strategy_stack.last&.[](1) end |
#current_strategy ⇒ Object
63 64 65 |
# File 'lib/text/gen/context.rb', line 63 def current_strategy @strategy_stack.last&.first || "weighted" end |
#descend!(builder) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/text/gen/context.rb', line 24 def descend!(builder) @depth += 1 @keys << builder["key"] @strategy_stack << strategy_to_stack_item(builder) raise MaxRecursionError if @depth > @max_recursion end |
#remember(result) ⇒ Object
71 72 73 74 |
# File 'lib/text/gen/context.rb', line 71 def remember(result) @remembered << [current_key, result] result end |
#remember_checkpoint ⇒ Object
81 82 83 |
# File 'lib/text/gen/context.rb', line 81 def remember_checkpoint @remembered.length end |
#remembered(key = nil) ⇒ Object
76 77 78 79 |
# File 'lib/text/gen/context.rb', line 76 def remembered(key = nil) key ||= current_key @remembered.select { |k, _r| k == key }.map(&:last) end |
#restore_remember(checkpoint) ⇒ Object
85 86 87 |
# File 'lib/text/gen/context.rb', line 85 def restore_remember(checkpoint) @remembered = @remembered[0...checkpoint] end |
#strategy_to_stack_item(builder) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/text/gen/context.rb', line 31 def strategy_to_stack_item(builder) strategy, modifier = builder.fetch("strategy", "weighted").split(":", 2) unless KNOWN_STRATEGIES.include?(strategy) modifier = strategy strategy = "weighted" end [strategy, modifier] end |
#with_filters(filters, offset: 0) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/text/gen/context.rb', line 46 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 |