narabi 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,11 +2,6 @@
2
2
 
3
3
  [![Dependency Status](https://gemnasium.com/usutani/narabi-gem.png?travis)](https://gemnasium.com/usutani/narabi-gem)
4
4
 
5
- ## TODO
6
-
7
- - Support instance alias
8
- - Support title
9
-
10
5
  ## Copyright
11
6
 
12
7
  Copyright (c) 2012 Technical Group Laboratory, Inc. See LICENSE for details.
@@ -78,3 +78,26 @@
78
78
  | 入力値 | タイトル |
79
79
  | "title title #1" | "title #1" |
80
80
  | "title タイトル #1" | "タイトル #1" |
81
+
82
+ シナリオ: エイリアスの解析に成功
83
+ 前提 次のエイリアスを含むテキストを入力する:
84
+ """
85
+ title w/ instance alias
86
+
87
+ instance User as U
88
+ instance System
89
+
90
+ note U: This is a note.
91
+ U->System: Login request
92
+ System-->User: Response
93
+ """
94
+ もし エイリアスを含むテキストを解析する
95
+ ならば 次のハッシュの配列となる:
96
+ """
97
+ {:title=>"w/ instance alias"}
98
+ {:name=>"User", :alias=>"U"}
99
+ {:name=>"System"}
100
+ {:from=>"User", :to=>"User", :body=>"This is a note.", :is_note=>true}
101
+ {:from=>"User", :to=>"System", :body=>"Login request"}
102
+ {:from=>"System", :to=>"User", :body=>"Response", :is_return=>true}
103
+ """
@@ -9,7 +9,7 @@ end
9
9
  end
10
10
 
11
11
  もし /^一行の文字列を解析する$/ do
12
- @output = Narabi.parse_line(@input)
12
+ @output = Narabi::Message.parse_line(@input)
13
13
  end
14
14
 
15
15
  ならば /^送信元は"(#{CAPTURE_STRING})"となること$/ do |expected_from|
@@ -61,3 +61,36 @@ end
61
61
  ならば /^タイトルは"(#{CAPTURE_STRING})"となること$/ do |expected_title|
62
62
  @output[:title].should == expected_title
63
63
  end
64
+
65
+ 前提 /^次のエイリアスを含むテキストを入力する:$/ do |text|
66
+ @input = text
67
+ @output = []
68
+ end
69
+
70
+ もし /^エイリアスを含むテキストを解析する$/ do
71
+ Narabi::Alias.scope do |parser|
72
+ @input.each_line do |line|
73
+ next if line.empty?
74
+
75
+ if ret = parser.parse_line_for_instance(line)
76
+ @output << ret
77
+ next
78
+ end
79
+ if ret = parser.parse_line_for_message(line)
80
+ @output << ret
81
+ next
82
+ end
83
+ if ret = parser.parse_line_for_diagram(line)
84
+ @output << ret
85
+ next
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ ならば /^次のハッシュの配列となる:$/ do |expected_lines|
92
+ lines = []
93
+ expected_lines.each_line { |l| lines << eval(l.strip) }
94
+ @output.size.should == lines.size
95
+ @output.each { |h| h.should == lines.shift }
96
+ end
@@ -3,6 +3,7 @@ module Narabi
3
3
  RESPONSE_REGEXP = /^(?<from>.+)-->(?<to>.+):\s?(?<body>.*)/
4
4
  NOTE_REGEXP = /^note\s(?<from>(\s|\b|\d|\w|\W|^:)+):\s?(?<body>.*)/
5
5
  INSTANCE_REGEXP = /^instance\s?(?<name>.+)/
6
+ INSTANCE_ALIAS_REGEXP = /^instance\s?(?<name>.+).*\sas\s(?<alias>.+)/
6
7
  TITLE_REGEXP = /^title\s(?<title>.+)/
7
8
 
8
9
  class Base
@@ -18,25 +19,33 @@ module Narabi
18
19
  end
19
20
 
20
21
  class Message
22
+ def self.parse_line(src)
23
+ if msg = Message.create_return(src)
24
+ return msg
25
+ end
26
+ if msg = Message.create_normal(src)
27
+ return msg
28
+ end
29
+ if msg = Message.create_note(src)
30
+ return msg
31
+ end
32
+ end
33
+
34
+ private
35
+
21
36
  def self.create_normal(src)
22
- msg = Base.try_to_create(NORMAL_REGEXP, src)
23
- #msg[:is_return] = false if msg
24
- #msg[:is_note] = false if msg
25
- msg
37
+ Base.try_to_create(NORMAL_REGEXP, src)
26
38
  end
27
39
 
28
40
  def self.create_return(src)
29
41
  msg = Base.try_to_create(RESPONSE_REGEXP, src)
30
42
  msg[:is_return] = true if msg
31
- #msg[:is_note] = false if msg
32
43
  msg
33
44
  end
34
45
 
35
46
  def self.create_note(src)
36
47
  msg = Base.try_to_create(NOTE_REGEXP, src)
37
48
  msg[:to] = msg[:from] if msg
38
- #msg[:is_return] = false if msg
39
- #msg[:to] = "" if msg
40
49
  msg[:is_note] = true if msg
41
50
  msg
42
51
  end
@@ -44,25 +53,59 @@ module Narabi
44
53
 
45
54
  class Instance
46
55
  def self.parse_line(src)
47
- msg = Base.try_to_create(INSTANCE_REGEXP, src)
56
+ if ins = Base.try_to_create(INSTANCE_ALIAS_REGEXP, src)
57
+ return ins
58
+ end
59
+ if ins = Base.try_to_create(INSTANCE_REGEXP, src)
60
+ return ins
61
+ end
48
62
  end
49
63
  end
50
64
 
51
65
  class Diagram
52
66
  def self.parse_line(src)
53
- msg = Base.try_to_create(TITLE_REGEXP, src)
67
+ Base.try_to_create(TITLE_REGEXP, src)
54
68
  end
55
69
  end
56
70
 
57
- def self.parse_line(src)
58
- if msg = Message.create_return(src)
59
- return msg
71
+ class Alias
72
+ def initialize
73
+ @aliases = {}
74
+ end
75
+
76
+ def parse_line_for_instance(src)
77
+ if hash = Instance.parse_line(src)
78
+ define_alias(hash)
79
+ end
80
+ hash
81
+ end
82
+
83
+ def parse_line_for_message(src)
84
+ if hash = Message.parse_line(src)
85
+ return replace_alias(hash, [:from, :to])
86
+ end
60
87
  end
61
- if msg = Message.create_normal(src)
62
- return msg
88
+
89
+ def parse_line_for_diagram(src)
90
+ Diagram.parse_line(src)
91
+ end
92
+
93
+ def self.scope
94
+ yield Narabi::Alias.new
63
95
  end
64
- if msg = Message.create_note(src)
65
- return msg
96
+
97
+ private
98
+
99
+ def define_alias(hash)
100
+ @aliases[hash[:alias]] = hash[:name]
101
+ end
102
+
103
+ def replace_alias(hash, keys)
104
+ keys.each do |key|
105
+ next unless @aliases[hash[key]]
106
+ hash[key] = @aliases[hash[key]]
107
+ end
108
+ hash
66
109
  end
67
110
  end
68
111
  end
@@ -1,3 +1,3 @@
1
1
  module Narabi
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: narabi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-06 00:00:00.000000000 Z
12
+ date: 2012-06-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -91,3 +91,4 @@ test_files:
91
91
  - features/parser.feature
92
92
  - features/step_definitions/parser_steps.rb
93
93
  - features/support/env.rb
94
+ has_rdoc: