Class: Saga::Tokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/saga/tokenizer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ Tokenizer

Returns a new instance of Tokenizer.



3
4
5
# File 'lib/saga/tokenizer.rb', line 3

def initialize(parser)
  @parser = parser
end

Class Method Details

.interval(input) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/saga/tokenizer.rb', line 25

def self.interval(input)
  case input.strip
  when 'd'
    :days
  when 'w'
    :weeks
  else
    :hours
  end
end

.tokenize_author(input) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/saga/tokenizer.rb', line 80

def self.tokenize_author(input)
  author = {}
  parts = input[1..-1].split(',')
  author[:name]    = parts[0].strip if parts[0]
  author[:email]   = parts[1].strip if parts[1]
  author[:company] = parts[2].strip if parts[2]
  author[:website] = parts[3].strip if parts[3]
  author
end

.tokenize_definition(input) ⇒ Object



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

def self.tokenize_definition(input)
  if match = /^([^:]+)\s*:\s*(.+)\s*$/.match(input)
    {:title => match[1], :definition => match[2]}
  else
    {}
  end
end

.tokenize_story(input) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/saga/tokenizer.rb', line 61

def self.tokenize_story(input)
  parts = input.split('-')
  if parts.length > 1
    story = tokenize_story_attributes(parts[-1])
    story[:description] = parts[0..-2].join('-').strip
    story
  else
    { :description => input.strip }
  end
end

.tokenize_story_attributes(input) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/saga/tokenizer.rb', line 36

def self.tokenize_story_attributes(input)
  return {} if input.nil?
  
  attributes = {}
  rest       = []
  parts      = input.split(/\s/)
  
  parts.each do |part|
    if part.strip == ''
      next
    elsif match = /\#(\d+)/.match(part)
      attributes[:id] = match[1].to_i
    elsif match = /i(\d+)/.match(part)
      attributes[:iteration] = match[1].to_i
    elsif match = /(\d+)(d|w|h|)/.match(part)
      attributes[:estimate] = [match[1].to_i, interval(match[2])]
    else
      rest << part
    end
  end
  
  attributes[:status] = rest.join(' ') unless rest.empty?
  attributes
end

Instance Method Details

#process(input) ⇒ Object



19
20
21
22
23
# File 'lib/saga/tokenizer.rb', line 19

def process(input)
  input.split("\n").each do |line|
    process_line(line)
  end
end

#process_line(input) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/saga/tokenizer.rb', line 7

def process_line(input)
  if input[0,2].downcase == 'as'
    @parser.handle_story(self.class.tokenize_story(input))
  elsif input[0,1] == '-'
    @parser.handle_author(self.class.tokenize_author(input))
  elsif input =~ /^\w(\w|[\s-])+:/
    @parser.handle_definition(self.class.tokenize_definition(input))
  else
    @parser.handle_string(input)
  end
end