Class: Text::Gen::Result

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#componentsObject (readonly)

Returns the value of attribute components.



10
11
12
# File 'lib/text/gen/result.rb', line 10

def components
  @components
end

#metaObject (readonly)

Returns the value of attribute meta.



10
11
12
# File 'lib/text/gen/result.rb', line 10

def meta
  @meta
end

#multiplierObject (readonly)

Returns the value of attribute multiplier.



10
11
12
# File 'lib/text/gen/result.rb', line 10

def multiplier
  @multiplier
end

#textObject (readonly)

Returns the value of attribute text.



10
11
12
# File 'lib/text/gen/result.rb', line 10

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/text/gen/result.rb', line 10

def type
  @type
end

#valueObject (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.merge_meta(meta || result.meta)
  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.merge_all_meta(results)
    s.merge_meta(meta)
  end
end

Instance Method Details

#clear_meta(key, val) ⇒ Object



55
56
57
# File 'lib/text/gen/result.rb', line 55

def clear_meta(key, val)
  @meta = Text::Gen::Meta.clear_kv(meta, key, val)
end

#filter?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/text/gen/result.rb', line 32

def filter?
  type.start_with?("fn(")
end

#filter_match?(component_key) ⇒ Boolean

Returns:

  • (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 merge_all_meta(results)
  (results || []).each { |r| @meta = Text::Gen::Meta.merge_meta(meta, r.meta) }
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(meta, key, val)
end

#merge_meta(hsh) ⇒ Object



47
48
49
# File 'lib/text/gen/result.rb', line 47

def merge_meta(hsh)
  @meta = Text::Gen::Meta.merge_meta(meta, hsh)
end

#to_hObject



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_sObject



59
60
61
# File 'lib/text/gen/result.rb', line 59

def to_s
  @text
end