Class: Yodaism::Quote

Inherits:
Object
  • Object
show all
Defined in:
lib/yodaism/quote.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuote

Returns a new instance of Quote.



6
7
8
9
10
# File 'lib/yodaism/quote.rb', line 6

def initialize
  @quotes = Yodaism.config.quotes
  @@quote_array = Array.new
  @@word_limit = 8
end

Instance Attribute Details

#quote_arrayObject

Returns the value of attribute quote_array.



3
4
5
# File 'lib/yodaism/quote.rb', line 3

def quote_array
  @quote_array
end

#word_limitObject

Returns the value of attribute word_limit.



4
5
6
# File 'lib/yodaism/quote.rb', line 4

def word_limit
  @word_limit
end

Instance Method Details

#asciiObject



18
19
20
21
# File 'lib/yodaism/quote.rb', line 18

def ascii
  @@quote_array = Array.new
  quote_with_yoda(split_quote_text(random))
end

#populate_quote_array(text_array, lines, remainder) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/yodaism/quote.rb', line 63

def populate_quote_array(text_array, lines, remainder)
  limit = 0
  (1..lines).each do | line_num |
    put_text_in_array(text_array[limit, @@word_limit])
    limit = @@word_limit * line_num
  end
  put_text_in_array(text_array[limit, remainder])
end

#put_text_in_array(word_array) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/yodaism/quote.rb', line 72

def put_text_in_array(word_array)
  text = ""
  word_array.each do |word|
    text = text.empty? ? word : text + " " + word
  end
  @@quote_array.push(text)
end

#quote_with_yoda(yoda_quote) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/yodaism/quote.rb', line 23

def quote_with_yoda(yoda_quote)
    yoda = %q{
                 .--.           
       ::\`--._,'.::.`._.--'/:: 0
       ::::.  ` __::__ '  .:::: 1
       ::::::-:.`'..`'.:-:::::: 2
       ::::::::\ `--' /:::::::: 3
       :::::::::------::::::::: 4
                                5
                                6
                                7
                                8

    }.gsub(/^ {10}/,'')
    return replace_identifiers_with_quote_text yoda, yoda_quote
end

#randomObject



12
13
14
15
16
# File 'lib/yodaism/quote.rb', line 12

def random
  r = Random.new
  c = r.rand(0..@quotes.size - 1)  
  @quotes[c].strip
end

#replace_identifiers_with_quote_text(text, yoda_quote) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/yodaism/quote.rb', line 40

def replace_identifiers_with_quote_text(text, yoda_quote)
  #replaces the number from the template with a quote and
  #also replaces any left over number with a blank string
  #
  index = 0
  (0..8).each do |line_num|
    replace_text = ""
    if index <= yoda_quote.size
      replace_text = yoda_quote[index] == nil ? "" : yoda_quote[index] 
    end
    text = text.gsub(index.to_s, replace_text)
    index += 1
  end
  return text
end

#split_quote_text(text) ⇒ Object



56
57
58
59
60
61
# File 'lib/yodaism/quote.rb', line 56

def split_quote_text(text)
  text_array = text.split(" ")
  size = text_array.size.divmod(@@word_limit)
  populate_quote_array(text_array, size[0], size[1])
  return @@quote_array
end