Class: ContentSpinning::Sentence

Inherits:
Array
  • Object
show all
Defined in:
lib/content_spinning/sentence.rb

Instance Method Summary collapse

Constructor Details

#initialize(*items) ⇒ Sentence

Returns a new instance of Sentence.



6
7
8
# File 'lib/content_spinning/sentence.rb', line 6

def initialize(*items)
  push(*items)
end

Instance Method Details

#cleanedObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/content_spinning/sentence.rb', line 10

def cleaned
  map!(&:cleaned)

  case length
  when 0
    ::ContentSpinning::String.new("")
  when 1
    first
  else
    self
  end
end

#countObject



23
24
25
26
27
28
29
30
# File 'lib/content_spinning/sentence.rb', line 23

def count
  case length
  when 0
    1
  else
    map(&:count).inject(:*)
  end
end

#inspectObject



32
33
34
# File 'lib/content_spinning/sentence.rb', line 32

def inspect
  "<Sentence [#{map(&:inspect).join(", ")}]>"
end

#randomObject



36
37
38
# File 'lib/content_spinning/sentence.rb', line 36

def random
  map(&:random).join
end

#spinObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/content_spinning/sentence.rb', line 40

def spin
  spinned = map(&:spin)

  case spinned.length
  when 1
    spinned[0]
  else
    spinned[0].product(*spinned[1..-1]).tap do |products|
      products.map!(&:join)
    end
  end
end

#to_sourceObject



53
54
55
# File 'lib/content_spinning/sentence.rb', line 53

def to_source
  map(&:to_source).join
end