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

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.



10
11
12
13
14
15
16
17
# File 'lib/text/gen/result.rb', line 10

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.



8
9
10
# File 'lib/text/gen/result.rb', line 8

def components
  @components
end

#metaObject (readonly)

Returns the value of attribute meta.



8
9
10
# File 'lib/text/gen/result.rb', line 8

def meta
  @meta
end

#multiplierObject (readonly)

Returns the value of attribute multiplier.



8
9
10
# File 'lib/text/gen/result.rb', line 8

def multiplier
  @multiplier
end

#textObject (readonly)

Returns the value of attribute text.



8
9
10
# File 'lib/text/gen/result.rb', line 8

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/text/gen/result.rb', line 8

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/text/gen/result.rb', line 8

def value
  @value
end

Class Method Details

.from(text:, type:, result:, value: nil, multiplier: nil) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/text/gen/result.rb', line 67

def from(text:, type:, result:, value: nil, multiplier: nil)
  value ||= result.value
  multiplier ||= result.multiplier
  new(text:, type:, value:, multiplier:).tap do |s|
    s.components.append(result)
    s.merge_meta(result.meta)
  end
end

.merge(results, value: nil, multiplier: nil, filters: [], meta: {}, type: :sequence) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/text/gen/result.rb', line 76

def merge(results, value: nil, multiplier: nil, filters: [], meta: {}, type: :sequence)
  results = results.compact
  sep = Filter.separator(filters)
  mul = multiplier || results.reduce(1) { |acc, r| acc * r.multiplier }
  val = value || results.sum(&:value)
  new(
    text: results.map(&:text).join(sep),
    type: type,
    value: val * mul,
    multiplier: 1
  ).tap do |result|
    result.components.append(*results)
    result.merge_all_meta(results)
    result.merge_meta(meta)
  end
end

Instance Method Details

#clear_meta(key, val) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/text/gen/result.rb', line 50

def clear_meta(key, val)
  if key == "*" && val == "*"
    @meta = {}
  elsif key == "*"
    @meta.each_value { |v| v.delete(val) }
  elsif val == "*"
    @meta.delete(key)
  else
    @meta[key]&.delete(val)
  end
end

#merge_all_meta(results) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/text/gen/result.rb', line 30

def merge_all_meta(results)
  return unless results

  results.each do |r|
    r.meta.each { |k, v| merge_kv(k, v) }
  end
end

#merge_kv(key, val) ⇒ Object



44
45
46
47
48
# File 'lib/text/gen/result.rb', line 44

def merge_kv(key, val)
  key = key.to_s.downcase
  arr = val.is_a?(Array) ? val : Array(val)
  meta[key] = meta.key?(key) ? (meta[key] + arr).uniq : arr.dup
end

#merge_meta(hsh) ⇒ Object



38
39
40
41
42
# File 'lib/text/gen/result.rb', line 38

def merge_meta(hsh)
  return unless hsh

  hsh.each { |k, v| merge_kv(k, v) }
end

#to_hObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/text/gen/result.rb', line 19

def to_h
  {
    text:,
    type:,
    value:,
    multiplier:,
    meta:,
    components: components.map(&:to_h)
  }
end

#to_sObject



62
63
64
# File 'lib/text/gen/result.rb', line 62

def to_s
  @text
end