gimei 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,141 +1,40 @@
1
- require "gimei/version"
1
+ require 'forwardable'
2
2
  require 'yaml'
3
+ require 'gimei/version'
4
+ require 'gimei/name'
5
+ require 'gimei/address'
3
6
 
4
7
  class Gimei
8
+ extend Forwardable
9
+
5
10
  NAMES = YAML.load_file(File.expand_path(File.join('..', 'data', 'names.yml'), __FILE__))
11
+ ADDRESSES = YAML.load_file(File.expand_path(File.join('..', 'data', 'addresses.yml'), __FILE__))
6
12
  GENDER = [:male, :female].freeze
7
13
 
8
- class << self
9
- def male
10
- new(:male)
11
- end
14
+ def_delegators :@name, :kanji, :hiragana, :katakana, :first, :last, :male?, :female?
15
+ def_delegators :@address, :prefecture, :city, :town
16
+ alias_method :to_s, :kanji
12
17
 
13
- def female
14
- new(:female)
15
- end
18
+ attr_reader :name, :address
16
19
 
17
- def kanji
18
- name = new
19
- "#{name.last.kanji} #{name.first.kanji}"
20
- end
21
-
22
- def hiragana
23
- name = new
24
- "#{name.last.hiragana} #{name.first.hiragana}"
25
- end
20
+ class << self
21
+ extend Forwardable
26
22
 
27
- def katakana
28
- name = new
29
- "#{name.last.katakana} #{name.first.katakana}"
30
- end
23
+ def_delegators :name, :kanji, :hiragana, :katakana, :first, :last
24
+ def_delegators Gimei::Name, :male, :female
25
+ def_delegators :address, :prefecture, :city, :town
31
26
 
32
- def first
33
- new.first
27
+ def name
28
+ Name.new
34
29
  end
35
30
 
36
- def last
37
- new.last
31
+ def address
32
+ Address.new
38
33
  end
39
34
  end
40
35
 
41
36
  def initialize(gender = nil)
42
- @gender = gender || GENDER.sample
43
- @first = First.new @gender
44
- @last = Last.new
45
- end
46
-
47
- def kanji
48
- "#{last.kanji} #{first.kanji}"
49
- end
50
-
51
- def hiragana
52
- "#{last.hiragana} #{first.hiragana}"
53
- end
54
-
55
- def katakana
56
- "#{last.katakana} #{first.katakana}"
57
- end
58
-
59
- def to_s
60
- kanji
61
- end
62
-
63
- def first
64
- @first
65
- end
66
-
67
- def last
68
- @last
69
- end
70
-
71
- def male?
72
- @gender == :male
73
- end
74
-
75
- def female?
76
- @gender == :female
77
- end
78
-
79
- class First
80
- class << self
81
- def male
82
- new(:male)
83
- end
84
-
85
- def female
86
- new(:female)
87
- end
88
- end
89
-
90
- def initialize(gender = nil)
91
- @gender = gender || GENDER.sample
92
- @name = NAMES['first_name'][@gender.to_s].sample
93
- end
94
-
95
- def male?
96
- @gender == :male
97
- end
98
-
99
- def female?
100
- @gender == :female
101
- end
102
-
103
- def kanji
104
- @name[0]
105
- end
106
-
107
- def hiragana
108
- @name[1]
109
- end
110
-
111
- def katakana
112
- @name[2]
113
- end
114
-
115
- def to_s
116
- kanji
117
- end
118
- end
119
-
120
- class Last
121
- def initialize
122
- @name = NAMES['last_name'].sample
123
- end
124
-
125
- def kanji
126
- @name[0]
127
- end
128
-
129
- def hiragana
130
- @name[1]
131
- end
132
-
133
- def katakana
134
- @name[2]
135
- end
136
-
137
- def to_s
138
- kanji
139
- end
37
+ @name = Name.new(gender)
38
+ @address = Address.new
140
39
  end
141
40
  end
@@ -0,0 +1,83 @@
1
+ class Gimei::Address
2
+ attr_reader :prefecture, :city, :town
3
+
4
+ def initialize
5
+ @prefecture = Prefecture.new
6
+ @city = City.new
7
+ @town = Town.new
8
+ end
9
+
10
+ def kanji
11
+ "#{prefecture.kanji}#{city.kanji}#{town.kanji}"
12
+ end
13
+
14
+ def hiragana
15
+ "#{prefecture.hiragana}#{city.hiragana}#{town.hiragana}"
16
+ end
17
+
18
+ def katakana
19
+ "#{prefecture.katakana}#{city.katakana}#{town.katakana}"
20
+ end
21
+
22
+ alias_method :to_s, :kanji
23
+
24
+ class Prefecture
25
+ def kanji
26
+ @prefectures[0]
27
+ end
28
+
29
+ def hiragana
30
+ @prefectures[1]
31
+ end
32
+
33
+ def katakana
34
+ @prefectures[2]
35
+ end
36
+
37
+ def initialize
38
+ @prefectures = Gimei::ADDRESSES['addresses']['prefecture'].sample
39
+ end
40
+
41
+ alias_method :to_s, :kanji
42
+ end
43
+
44
+ class City
45
+ def kanji
46
+ @cities[0]
47
+ end
48
+
49
+ def hiragana
50
+ @cities[1]
51
+ end
52
+
53
+ def katakana
54
+ @cities[2]
55
+ end
56
+
57
+ def initialize
58
+ @cities = Gimei::ADDRESSES['addresses']['city'].sample
59
+ end
60
+
61
+ alias_method :to_s, :kanji
62
+ end
63
+
64
+ class Town
65
+ def kanji
66
+ @towns[0]
67
+ end
68
+
69
+ def hiragana
70
+ @towns[1]
71
+ end
72
+
73
+ def katakana
74
+ @towns[2]
75
+ end
76
+
77
+ def initialize
78
+ @towns = Gimei::ADDRESSES['addresses']['town'].sample
79
+ end
80
+
81
+ alias_method :to_s, :kanji
82
+ end
83
+ end
@@ -0,0 +1,103 @@
1
+ class Gimei::Name
2
+ attr_reader :first, :last
3
+
4
+ class << self
5
+ extend Forwardable
6
+ def_delegators :new, :kanji, :hiragana, :katakana, :first, :last
7
+
8
+ def male
9
+ new(:male)
10
+ end
11
+
12
+ def female
13
+ new(:female)
14
+ end
15
+ end
16
+
17
+ def initialize(gender = nil)
18
+ @gender = gender || Gimei::GENDER.sample
19
+ @first = First.new @gender
20
+ @last = Last.new
21
+ end
22
+
23
+ def kanji
24
+ "#{last.kanji} #{first.kanji}"
25
+ end
26
+
27
+ def hiragana
28
+ "#{last.hiragana} #{first.hiragana}"
29
+ end
30
+
31
+ def katakana
32
+ "#{last.katakana} #{first.katakana}"
33
+ end
34
+
35
+ def male?
36
+ @gender == :male
37
+ end
38
+
39
+ def female?
40
+ @gender == :female
41
+ end
42
+
43
+ alias_method :to_s, :kanji
44
+
45
+ class First
46
+ class << self
47
+ def male
48
+ new(:male)
49
+ end
50
+
51
+ def female
52
+ new(:female)
53
+ end
54
+ end
55
+
56
+ def initialize(gender = nil)
57
+ @gender = gender || Gimei::GENDER.sample
58
+ @name = Gimei::NAMES['first_name'][@gender.to_s].sample
59
+ end
60
+
61
+ def male?
62
+ @gender == :male
63
+ end
64
+
65
+ def female?
66
+ @gender == :female
67
+ end
68
+
69
+ def kanji
70
+ @name[0]
71
+ end
72
+
73
+ def hiragana
74
+ @name[1]
75
+ end
76
+
77
+ def katakana
78
+ @name[2]
79
+ end
80
+
81
+ alias_method :to_s, :kanji
82
+ end
83
+
84
+ class Last
85
+ def initialize
86
+ @name = Gimei::NAMES['last_name'].sample
87
+ end
88
+
89
+ def kanji
90
+ @name[0]
91
+ end
92
+
93
+ def hiragana
94
+ @name[1]
95
+ end
96
+
97
+ def katakana
98
+ @name[2]
99
+ end
100
+
101
+ alias_method :to_s, :kanji
102
+ end
103
+ end
@@ -1,3 +1,3 @@
1
1
  class Gimei
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,46 @@
1
+ # -*- coding: utf-8 -*-
2
+ require_relative 'spec_helper'
3
+
4
+ describe Gimei do
5
+
6
+ it '全角文字が返ること' do
7
+ Gimei.address.kanji.must_match /\A[#{Moji.zen}]+\z/
8
+ Gimei.address.to_s.must_match /\A[#{Moji.zen}]+\z/
9
+ Gimei.address.prefecture.kanji.must_match /\A[#{Moji.zen}]+\z/
10
+ Gimei.address.prefecture.to_s.must_match /\A[#{Moji.zen}]+\z/
11
+ Gimei.address.city.kanji.must_match /\A[#{Moji.zen}]+\z/
12
+ Gimei.address.city.to_s.must_match /\A[#{Moji.zen}]+\z/
13
+ Gimei.address.town.kanji.must_match /\A[#{Moji.zen}]+\z/
14
+ Gimei.address.town.to_s.must_match /\A[#{Moji.zen}]+\z/
15
+
16
+ Gimei.prefecture.kanji.must_match /\A[#{Moji.zen}]+\z/
17
+ Gimei.prefecture.to_s.must_match /\A[#{Moji.zen}]+\z/
18
+ Gimei.city.kanji.must_match /\A[#{Moji.zen}]+\z/
19
+ Gimei.city.to_s.must_match /\A[#{Moji.zen}]+\z/
20
+ Gimei.town.kanji.must_match /\A[#{Moji.zen}]+\z/
21
+ Gimei.town.to_s.must_match /\A[#{Moji.zen}]+\z/
22
+ end
23
+
24
+ it 'ひらがなが返ること' do
25
+ Gimei.address.hiragana.must_match /\A[\p{hiragana}]+\z/
26
+ Gimei.address.prefecture.hiragana.must_match /\A[\p{hiragana}]+\z/
27
+ Gimei.address.city.hiragana.must_match /\A[\p{hiragana}]+\z/
28
+ Gimei.address.town.hiragana.must_match /\A[\p{hiragana}]+\z/
29
+
30
+ Gimei.prefecture.hiragana.must_match /\A[\p{hiragana}]+\z/
31
+ Gimei.city.hiragana.must_match /\A[\p{hiragana}]+\z/
32
+ Gimei.town.hiragana.must_match /\A[\p{hiragana}]+\z/
33
+ end
34
+
35
+ it 'カタカナが返ること' do
36
+ Gimei.address.katakana.must_match /\A[\p{katakana}]+\z/
37
+ Gimei.address.prefecture.katakana.must_match /\A[\p{katakana}]+\z/
38
+ Gimei.address.city.katakana.must_match /\A[\p{katakana}]+\z/
39
+ Gimei.address.town.katakana.must_match /\A[\p{katakana}]+\z/
40
+
41
+ Gimei.prefecture.katakana.must_match /\A[\p{katakana}]+\z/
42
+ Gimei.city.katakana.must_match /\A[\p{katakana}]+\z/
43
+ Gimei.town.katakana.must_match /\A[\p{katakana}]+\z/
44
+ end
45
+
46
+ end
@@ -0,0 +1,154 @@
1
+ # coding: utf-8
2
+ require_relative 'spec_helper'
3
+
4
+ describe Gimei do
5
+ describe '.male' do
6
+ before { @name = Gimei.male }
7
+
8
+ it 'Gimei::Name オブジェクトが返ること' do
9
+ @name.must_be_instance_of Gimei::Name
10
+ end
11
+
12
+ it '#male? が true を返すこと' do
13
+ @name.male?.must_equal true
14
+ end
15
+ end
16
+
17
+ describe '.female' do
18
+ before { @name = Gimei.female }
19
+
20
+ it 'Gimei::Name オブジェクトが返ること' do
21
+ @name.must_be_instance_of Gimei::Name
22
+ end
23
+
24
+ it '#female? が true を返すこと' do
25
+ @name.female?.must_equal true
26
+ end
27
+ end
28
+
29
+ describe '#kanji' do
30
+ it '全角文字とスペースが返ること' do
31
+ Gimei.new.kanji.must_match /\A[#{Moji.zen}\s]+\z/
32
+ end
33
+ end
34
+
35
+ describe '#to_s' do
36
+ it '全角文字とスペースが返ること' do
37
+ Gimei.new.to_s.must_match /\A[#{Moji.zen}\s]+\z/
38
+ end
39
+ end
40
+
41
+ describe '.kanji' do
42
+ it '全角文字とスペースが返ること' do
43
+ Gimei.kanji.must_match /\A[#{Moji.zen}\s]+\z/
44
+ end
45
+ end
46
+
47
+ describe '#hiragana' do
48
+ it 'ひらがなとスペースが返ること' do
49
+ Gimei.new.hiragana.must_match /\A[\p{hiragana}\s]+\z/
50
+ end
51
+ end
52
+
53
+ describe '.hiragana' do
54
+ it 'ひらがなとスペースが返ること' do
55
+ Gimei.hiragana.must_match /\A[\p{hiragana}\s]+\z/
56
+ end
57
+ end
58
+
59
+ describe '#katakana' do
60
+ it 'カタカナとスペースが返ること' do
61
+ Gimei.new.katakana.must_match /\A[\p{katakana}\s]+\z/
62
+ end
63
+ end
64
+
65
+ describe '.katakana' do
66
+ it 'カタカナとスペースが返ること' do
67
+ Gimei.katakana.must_match /\A[\p{katakana}\s]+\z/
68
+ end
69
+ end
70
+
71
+ describe '.name' do
72
+ it 'Gimei::Name オブジェクトが返ること' do
73
+ Gimei.name.must_be_instance_of Gimei::Name
74
+ end
75
+ end
76
+
77
+ describe '#name' do
78
+ it 'Gimei::Name オブジェクトが返ること' do
79
+ Gimei.new.name.must_be_instance_of Gimei::Name
80
+ end
81
+ end
82
+
83
+ describe '.first' do
84
+ it 'Gimei::Name::First オブジェクトが返ること' do
85
+ Gimei.first.must_be_instance_of Gimei::Name::First
86
+ end
87
+ end
88
+
89
+ describe '#first' do
90
+ it 'Gimei::Name::First オブジェクトが返ること' do
91
+ Gimei.new.first.must_be_instance_of Gimei::Name::First
92
+ end
93
+ end
94
+
95
+ describe '.last' do
96
+ it 'Gimei::Name::First オブジェクトが返ること' do
97
+ Gimei.last.must_be_instance_of Gimei::Name::Last
98
+ end
99
+ end
100
+
101
+ describe '#last' do
102
+ it 'Gimei::Name::First オブジェクトが返ること' do
103
+ Gimei.new.last.must_be_instance_of Gimei::Name::Last
104
+ end
105
+ end
106
+
107
+ describe '.address' do
108
+ it 'Gimei::Address オブジェクトが返ること' do
109
+ Gimei.address.must_be_instance_of Gimei::Address
110
+ end
111
+ end
112
+
113
+ describe '#address' do
114
+ it 'Gimei::Address オブジェクトが返ること' do
115
+ Gimei.new.address.must_be_instance_of Gimei::Address
116
+ end
117
+ end
118
+
119
+ describe '.prefecture' do
120
+ it 'Gimei::Address::Prefecture オブジェクトが返ること' do
121
+ Gimei.prefecture.must_be_instance_of Gimei::Address::Prefecture
122
+ end
123
+ end
124
+
125
+ describe '#prefecture' do
126
+ it 'Gimei::Address::Prefecture オブジェクトが返ること' do
127
+ Gimei.new.prefecture.must_be_instance_of Gimei::Address::Prefecture
128
+ end
129
+ end
130
+
131
+ describe '.city' do
132
+ it 'Gimei::Address::City オブジェクトが返ること' do
133
+ Gimei.city.must_be_instance_of Gimei::Address::City
134
+ end
135
+ end
136
+
137
+ describe '#city' do
138
+ it 'Gimei::Address::City オブジェクトが返ること' do
139
+ Gimei.new.city.must_be_instance_of Gimei::Address::City
140
+ end
141
+ end
142
+
143
+ describe '.town' do
144
+ it 'Gimei::Address::Town オブジェクトが返ること' do
145
+ Gimei.town.must_be_instance_of Gimei::Address::Town
146
+ end
147
+ end
148
+
149
+ describe '#town' do
150
+ it 'Gimei::Address::Town オブジェクトが返ること' do
151
+ Gimei.new.town.must_be_instance_of Gimei::Address::Town
152
+ end
153
+ end
154
+ end