Class: Text::Gen::Result
- Inherits:
-
Object
- Object
- Text::Gen::Result
- Defined in:
- lib/text/gen/result.rb
Overview
Result stores the generated text, value and metadata and context about how it was created
Constant Summary collapse
- SEPARATORS =
{ "tab" => "\t", "newline" => "\n", "space" => " " }.freeze
Instance Attribute Summary collapse
-
#components ⇒ Object
readonly
Returns the value of attribute components.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#multiplier ⇒ Object
readonly
Returns the value of attribute multiplier.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .from(result, type:, text: nil, value: nil, multiplier: nil, meta: nil) ⇒ Object
- .merge(results, value: nil, multiplier: nil, separator: "", meta: {}, type: :sequence) ⇒ Object
Instance Method Summary collapse
- #clear_meta(key, val) ⇒ Object
- #filter? ⇒ Boolean
- #filter_match?(component_key) ⇒ Boolean
-
#initialize(text:, type:, value: 0, multiplier: 1) ⇒ Result
constructor
A new instance of Result.
- #merge_all_meta(results) ⇒ Object
- #merge_kv(key, val) ⇒ Object
- #merge_meta(hsh) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(text:, type:, value: 0, multiplier: 1) ⇒ Result
Returns a new instance of Result.
12 13 14 15 16 17 18 19 |
# File 'lib/text/gen/result.rb', line 12 def initialize(text:, type:, value: 0, multiplier: 1) @text = text @type = type.to_s @value = [0, (value || 0).to_i].max @multiplier = [1, (multiplier || 1).to_i].max @meta = {} @components = [] end |
Instance Attribute Details
#components ⇒ Object (readonly)
Returns the value of attribute components.
10 11 12 |
# File 'lib/text/gen/result.rb', line 10 def components @components end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
10 11 12 |
# File 'lib/text/gen/result.rb', line 10 def @meta end |
#multiplier ⇒ Object (readonly)
Returns the value of attribute multiplier.
10 11 12 |
# File 'lib/text/gen/result.rb', line 10 def multiplier @multiplier end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
10 11 12 |
# File 'lib/text/gen/result.rb', line 10 def text @text end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
10 11 12 |
# File 'lib/text/gen/result.rb', line 10 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
10 11 12 |
# File 'lib/text/gen/result.rb', line 10 def value @value end |
Class Method Details
.from(result, type:, text: nil, value: nil, multiplier: nil, meta: nil) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/text/gen/result.rb', line 64 def from(result, type:, text: nil, value: nil, multiplier: nil, meta: nil) text ||= result.text value ||= result.value multiplier ||= result.multiplier new(text:, type:, value:, multiplier:).tap do |s| s.components.append(result) s.( || result.) end end |
.merge(results, value: nil, multiplier: nil, separator: "", meta: {}, type: :sequence) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/text/gen/result.rb', line 74 def merge(results, value: nil, multiplier: nil, separator: "", meta: {}, type: :sequence) results = results.compact mul = multiplier || results.reduce(1) { |acc, r| acc * r.multiplier } val = value || results.sum(&:value) sep = SEPARATORS[separator] || separator new( text: results.map(&:text).join(sep), type: type, value: val * mul, multiplier: val.zero? ? mul : 1 ).tap do |s| s.components.append(*results) s.(results) s.() end end |
Instance Method Details
#clear_meta(key, val) ⇒ Object
55 56 57 |
# File 'lib/text/gen/result.rb', line 55 def (key, val) @meta = Text::Gen::Meta.clear_kv(, key, val) end |
#filter? ⇒ Boolean
32 33 34 |
# File 'lib/text/gen/result.rb', line 32 def filter? type.start_with?("fn(") end |
#filter_match?(component_key) ⇒ Boolean
36 37 38 39 40 41 |
# File 'lib/text/gen/result.rb', line 36 def filter_match?(component_key) return true if type == component_key return false unless filter? components.any? { |c| c.filter_match?(component_key) } end |
#merge_all_meta(results) ⇒ Object
43 44 45 |
# File 'lib/text/gen/result.rb', line 43 def (results) (results || []).each { |r| @meta = Text::Gen::Meta.(, r.) } end |
#merge_kv(key, val) ⇒ Object
51 52 53 |
# File 'lib/text/gen/result.rb', line 51 def merge_kv(key, val) Text::Gen::Meta.append_kv(, key, val) end |
#merge_meta(hsh) ⇒ Object
47 48 49 |
# File 'lib/text/gen/result.rb', line 47 def (hsh) @meta = Text::Gen::Meta.(, hsh) end |
#to_h ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/text/gen/result.rb', line 21 def to_h { text:, type:, value:, multiplier:, meta:, components: components.map(&:to_h) } end |
#to_s ⇒ Object
59 60 61 |
# File 'lib/text/gen/result.rb', line 59 def to_s @text end |