class SimpleMunou::Brain

Attributes

words[R]

Public Class Methods

new(talk_pattern) click to toggle source
# File lib/simple_munou.rb, line 12
def initialize(talk_pattern)
  @pattern = talk_pattern
end

Public Instance Methods

pattern=(talk_pattern) click to toggle source
# File lib/simple_munou.rb, line 35
def pattern=(talk_pattern)
  if talk_pattern.class == Array
    @pattern = talk_pattern
  else
    fail TypeError, "Pattern must be Array."
  end
end
say(variables = nil) click to toggle source
# File lib/simple_munou.rb, line 43
def say(variables = nil)
  @variables = variables
  @variables = {} if @variables.nil?
  @pattern.map do |p|
    case p
    when Fixnum
      choice_words p
    when Symbol
      key, kind_num = p.to_s.split("_")
      if value = @variables[key]
        value
      else
        @variables[key] = choice_words kind_num.to_i
      end
    when String
      p
    end
  end.join
end
valid_words(kind_num) click to toggle source
# File lib/simple_munou.rb, line 63
def valid_words(kind_num)
  kind_num > 0 ? @words.select {|word| word.kind == kind_num} : @words
end
words=(words) click to toggle source
# File lib/simple_munou.rb, line 16
def words=(words)
  @words = words.map do |elem|
    word = case elem
           when String then
             SimpleMunou::Word.new(elem, 0)
           when Array then        
             if (elem.size == 2) && (valid_word_array?(elem))
               SimpleMunou::Word.new(elem[0], elem[1])
             end
           when SimpleMunou::Word
             elem
           end
    if word.nil?
      fail TypeError, "Not Support Type" 
    end
    word
  end
end

Private Instance Methods

choice_words(kind_num) click to toggle source
# File lib/simple_munou.rb, line 68
def choice_words(kind_num)
  valid_words(kind_num).sample
end
valid_word_array?(elem) click to toggle source
# File lib/simple_munou.rb, line 72
def valid_word_array?(elem)
  (elem[0].class == String) and (elem[1].class == Fixnum)
end