tekido 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +18 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +188 -0
- data/Rakefile +11 -0
- data/USAGE_ja.md +153 -0
- data/lib/tekido.rb +7 -0
- data/lib/tekido/methods.rb +15 -0
- data/lib/tekido/methods/array.rb +48 -0
- data/lib/tekido/methods/boolean.rb +11 -0
- data/lib/tekido/methods/date.rb +39 -0
- data/lib/tekido/methods/numeric.rb +25 -0
- data/lib/tekido/methods/percentage.rb +26 -0
- data/lib/tekido/methods/string.rb +64 -0
- data/lib/tekido/version.rb +4 -0
- data/spec/spec_helper.rb +40 -0
- data/spec/tekido/methods/array_spec.rb +81 -0
- data/spec/tekido/methods/boolean_spec.rb +36 -0
- data/spec/tekido/methods/date_spec.rb +131 -0
- data/spec/tekido/methods/numeric_spec.rb +138 -0
- data/spec/tekido/methods/percentage_spec.rb +102 -0
- data/spec/tekido/methods/string_spec.rb +273 -0
- data/tekido.gemspec +25 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a868ae3a97922903960421ce5ed892b5f5197f9a
|
4
|
+
data.tar.gz: 2371363b3ce8527d43572221895736b8fca5c0de
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e0fc9f67ef847f1248de07755fcf23ed196fbf4d2da917db5f64e6bf1f90610c8c8a4ea2732b165679effeeb579b88bd768fb67f2065bbf39f009846acd1f082
|
7
|
+
data.tar.gz: 5be19c39b74a86710c10ea207ea8130a4610165633a79727fabbeb37463fb55c3366fe901e5ac9af48df3eba644c4a0ca11b89941d25214feb5af88ed7dcf5a3
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 pinzolo
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
# Tekido
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/pinzolo/tekido.png)](http://travis-ci.org/pinzolo/tekido)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/pinzolo/tekido/badge.png)](https://coveralls.io/r/pinzolo/tekido)
|
5
|
+
|
6
|
+
`Tekido` is a module that generates various random value.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'tekido'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install tekido
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
`Tekido` has below methods. (All methods are dependent on `Kernel.rand`)
|
25
|
+
|
26
|
+
### .yes?
|
27
|
+
|
28
|
+
It returns random boolean.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
# true: 50%, false: 50%
|
32
|
+
Tekido.yes?
|
33
|
+
|
34
|
+
# true: 80%, false: 20%
|
35
|
+
Tekido.yes?(80)
|
36
|
+
```
|
37
|
+
|
38
|
+
### .percent
|
39
|
+
|
40
|
+
It retuens random percentage (0..100). It behaves the same as `rand(0..100)`.
|
41
|
+
|
42
|
+
### .percent_as
|
43
|
+
|
44
|
+
It returns random percentage. Can select return value type.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# returns integer (Tekido.percent behaves it, too)
|
48
|
+
Tekido.percent_as(:integer)
|
49
|
+
|
50
|
+
# returns float
|
51
|
+
Tekido.percent_as(:float)
|
52
|
+
|
53
|
+
# returns integer that is multiple of 5 (ex. 10, 35)
|
54
|
+
Tekido.percent_as(:mo5)
|
55
|
+
|
56
|
+
# returns integer that is multiple of 10 (ex. 10, 70)
|
57
|
+
Tekido.percent_as(:deca)
|
58
|
+
Tekido.percent_as(:mo10)
|
59
|
+
```
|
60
|
+
|
61
|
+
### .integer
|
62
|
+
|
63
|
+
It always returns random value as integer.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
# returns integer (0 <= value <= 1073741823)
|
67
|
+
Tekido.integer
|
68
|
+
|
69
|
+
# returns integer that not over argument
|
70
|
+
Tekido.integer(9999)
|
71
|
+
|
72
|
+
# returns integer that is included in argument
|
73
|
+
Tekido.integer(1..1234)
|
74
|
+
```
|
75
|
+
|
76
|
+
### .float
|
77
|
+
|
78
|
+
It always returns random value as float. The different with `rand` is return value type only.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# returns float (0 <= value < 1)
|
82
|
+
Tekido.float
|
83
|
+
|
84
|
+
# returns float that not over argument
|
85
|
+
Tekido.float(777.77)
|
86
|
+
|
87
|
+
# returns float that is included in argument
|
88
|
+
Tekido.float(1..1234)
|
89
|
+
Tekido.float(1.1..1234.5)
|
90
|
+
```
|
91
|
+
|
92
|
+
### .list
|
93
|
+
|
94
|
+
It returns Array instance that is filled by given sample values and ratio.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
# returns Array instance that contains 'one': 25%, 'two': 25%, 'three': 25%, 'four': 25%
|
98
|
+
Tekido.list(10000, %w(one two three four))
|
99
|
+
|
100
|
+
# returns Array instance that contains 'one': 11.1%, 'two': 22.2%, 'three': 33.3%, 'four': 33.4%
|
101
|
+
Tekido.list(10000, 'one' => 11.1, 'two' => 22.2, 'three' => 33.3, 'four' => 33.4)
|
102
|
+
|
103
|
+
# returns Array instance that contains 'one': 10%, 'two': 20%, 'three': 30%, nil: 40% (rests)
|
104
|
+
Tekido.list(10000, 'one' => 10, 'two' => 20, 'three' => 30)
|
105
|
+
```
|
106
|
+
|
107
|
+
### .date
|
108
|
+
|
109
|
+
It returns random Date instance.
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
# returns Date (0001-01-01 <= value <= 9999-12-31)
|
113
|
+
Tekido.date
|
114
|
+
|
115
|
+
# returns Date (2010-01-01 <= value <= 2010-12-31)
|
116
|
+
Tekido.date(2010)
|
117
|
+
|
118
|
+
# returns Date (1999-01-01 <= value <= 2007-12-31)
|
119
|
+
Tekido.date(1999..2007)
|
120
|
+
|
121
|
+
# returns Date (1999-02-21 <= value <= 2003-10-07)
|
122
|
+
Tekido.date(Date.new(1999, 2, 21)..Date.new(2003, 10, 7))
|
123
|
+
```
|
124
|
+
|
125
|
+
### .birthday
|
126
|
+
|
127
|
+
It returns random birthday as Date instance.
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
# returns Date as birthday satisfying that current age is more than 0 and less than 100
|
131
|
+
Tekido.birthday
|
132
|
+
|
133
|
+
# returns Date as birthday satisfying that current age is given argument
|
134
|
+
Tekido.birthday(28)
|
135
|
+
|
136
|
+
# returns Date as birthday satisfying that current age is within given argument
|
137
|
+
Tekido.birthday(21..25)
|
138
|
+
```
|
139
|
+
|
140
|
+
### .string
|
141
|
+
|
142
|
+
It returns random String instance.
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
# returns String instance that is costructed by upper characters, lower characters and numeric characters, and size is within 1..255.
|
146
|
+
Tekido.string
|
147
|
+
|
148
|
+
# returns String instance that is costructed by lower characters and numeric characters, and size is 10.
|
149
|
+
# components option accepts :upper or :lower or :number or combination of these.
|
150
|
+
Tekido.string(size: 10, components: [:upper, :number])
|
151
|
+
|
152
|
+
# returns String instance that is costructed by given characters (ex. x39yyz177x) , and size is within 8..16.
|
153
|
+
# chars option overrides components option
|
154
|
+
Tekido.string(size: 8..16, chars: %w(1 3 5 7 9 x y z))
|
155
|
+
```
|
156
|
+
|
157
|
+
### email
|
158
|
+
|
159
|
+
It returns random email address.
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
# returns email address that has example domain. (ex. eamh0aga@example.com)
|
163
|
+
Tekido.email
|
164
|
+
|
165
|
+
# returns email address that has given domain. (ex. oiuade@foobar.com)
|
166
|
+
Tekido.email('foobar.com')
|
167
|
+
|
168
|
+
# returns email address that has same domain as given email address. (ex. dkauy3akhf@baz.com)
|
169
|
+
Tekido.email("foo.bar@baz.com")
|
170
|
+
|
171
|
+
# .email accepts multiple domains or base email addresses.
|
172
|
+
Tekido.email('test@foo.com', 'bar.com', '@baz.com')
|
173
|
+
```
|
174
|
+
|
175
|
+
|
176
|
+
## Supported ruby versions
|
177
|
+
|
178
|
+
* 1.9.3
|
179
|
+
* 2.0.0
|
180
|
+
* 2.1.0
|
181
|
+
|
182
|
+
## Contributing
|
183
|
+
|
184
|
+
1. Fork it
|
185
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
186
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
187
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
188
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/USAGE_ja.md
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
# Tekido
|
2
|
+
|
3
|
+
`Tekido` とはさまざまなランダム値を出力するモジュールです。
|
4
|
+
|
5
|
+
`Tekido` は以下のメソッドを持っています。(全て `Kernel.rand` に依存しています)
|
6
|
+
|
7
|
+
### .yes?
|
8
|
+
|
9
|
+
ランダムな真偽値を返します。
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
# true: 50%, false: 50%
|
13
|
+
Tekido.yes?
|
14
|
+
|
15
|
+
# true: 80%, false: 20%
|
16
|
+
Tekido.yes?(80)
|
17
|
+
```
|
18
|
+
|
19
|
+
### .percent
|
20
|
+
|
21
|
+
ランダムで 0 から 100 までの百分率(数値)を返します。`rand(0..100)` と同じ動きをします。
|
22
|
+
|
23
|
+
### .percent_as
|
24
|
+
|
25
|
+
ランダムで 0 から 100 までの百分率(数値)を返します。戻り値の型などを指定できます。
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# 整数で返します。
|
29
|
+
Tekido.percent_as(:integer)
|
30
|
+
|
31
|
+
# 小数で返します。
|
32
|
+
Tekido.percent_as(:float)
|
33
|
+
|
34
|
+
# 5の倍数の整数を返します。
|
35
|
+
Tekido.percent_as(:mo5)
|
36
|
+
|
37
|
+
# 10の倍数の整数を返します。
|
38
|
+
Tekido.percent_as(:deca)
|
39
|
+
Tekido.percent_as(:mo10)
|
40
|
+
```
|
41
|
+
|
42
|
+
### .integer
|
43
|
+
|
44
|
+
ランダムな整数を返します。
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# 0 から 1073741823 までの範囲にある整数を返します。
|
48
|
+
Tekido.integer
|
49
|
+
|
50
|
+
# 指定の上限を超えない 0 以上の整数を返します。
|
51
|
+
Tekido.integer(9999)
|
52
|
+
|
53
|
+
# 指定の範囲内にある整数を返します。
|
54
|
+
Tekido.integer(1..1234)
|
55
|
+
```
|
56
|
+
|
57
|
+
### .float
|
58
|
+
|
59
|
+
ランダムな小数を返します。
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# 0 以上 1 未満の小数を返します。
|
63
|
+
Tekido.float
|
64
|
+
|
65
|
+
# 指定の上限を超えない 0 以上の小数を返します。
|
66
|
+
Tekido.float(777.77)
|
67
|
+
|
68
|
+
# 指定の範囲内にある少数を返します。
|
69
|
+
Tekido.float(1..1234)
|
70
|
+
Tekido.float(1.1..1234.5)
|
71
|
+
```
|
72
|
+
|
73
|
+
### .list
|
74
|
+
|
75
|
+
指定の値と割合でランダムに値を埋めた配列を返します。
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
# 等割合で埋めた配列を返します。('one': 25%, 'two': 25%, 'three': 25%, 'four': 25%)
|
79
|
+
Tekido.list(10000, %w(one two three four))
|
80
|
+
|
81
|
+
# 指定の配分で埋めた配列を返します。
|
82
|
+
Tekido.list(10000, 'one' => 11.1, 'two' => 22.2, 'three' => 33.3, 'four' => 33.4)
|
83
|
+
|
84
|
+
# 足りない部分は nil で埋められます。('one': 10%, 'two': 20%, 'three': 30%, nil: 40% (rests))
|
85
|
+
Tekido.list(10000, 'one' => 10, 'two' => 20, 'three' => 30)
|
86
|
+
```
|
87
|
+
|
88
|
+
### .date
|
89
|
+
|
90
|
+
ランダムな日付を返します。
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
# 0001-01-01 から 9999-12-31 までの日付を返します。
|
94
|
+
Tekido.date
|
95
|
+
|
96
|
+
# 指定の年に所属する日付を返します。
|
97
|
+
Tekido.date(2010)
|
98
|
+
|
99
|
+
# 指定された範囲に所属する年の日付を返します。
|
100
|
+
Tekido.date(1999..2007)
|
101
|
+
|
102
|
+
# 指定された範囲に所属する日付を返します。
|
103
|
+
Tekido.date(Date.new(1999, 2, 21)..Date.new(2003, 10, 7))
|
104
|
+
```
|
105
|
+
|
106
|
+
### .birthday
|
107
|
+
|
108
|
+
誕生日として使用できるランダムな日付を返します。
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
# 現時点で 0 歳から 100 歳までの年齢となる日付を返します。
|
112
|
+
Tekido.birthday
|
113
|
+
|
114
|
+
# 指定された年齢となるランダムな誕生日を返します。
|
115
|
+
Tekido.birthday(28)
|
116
|
+
|
117
|
+
# 指定された範囲に含まれる年齢となるランダムな誕生日を返します。
|
118
|
+
Tekido.birthday(21..25)
|
119
|
+
```
|
120
|
+
|
121
|
+
### .string
|
122
|
+
|
123
|
+
ランダムな文字列を返します。
|
124
|
+
It returns random String instance.
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
# 1 文字以上、255 文字以内の、大文字・小文字・数字からなるランダムな文字列を返します。
|
128
|
+
Tekido.string
|
129
|
+
|
130
|
+
# 長さが 10 で、大文字と数字からなるランダムな文字列を返します。
|
131
|
+
Tekido.string(size: 10, components: [:upper, :number])
|
132
|
+
|
133
|
+
# 長さが 8 文字以上、16文字以内の、:chars で指定された文字だけで構成されたランダムな文字列を返します。
|
134
|
+
Tekido.string(size: 8..16, chars: %w(1 3 5 7 9 x y z))
|
135
|
+
```
|
136
|
+
|
137
|
+
### email
|
138
|
+
|
139
|
+
ランダムなメールアドレスを返します。
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
# テスト用ドメインのランダムなメールアドレスを返します。
|
143
|
+
Tekido.email
|
144
|
+
|
145
|
+
# ドメインを指定したランダムなメールアドレスを返します。
|
146
|
+
Tekido.email('foobar.com')
|
147
|
+
|
148
|
+
# メールアドレス形式でドメインを指定できます。
|
149
|
+
Tekido.email('foo.bar@baz.com')
|
150
|
+
|
151
|
+
# ドメイン指定は複数可能です。(形式は混在できます)
|
152
|
+
Tekido.email('test@foo.com', 'bar.com', '@baz.com')
|
153
|
+
```
|
data/lib/tekido.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require "tekido/methods/percentage"
|
3
|
+
require "tekido/methods/boolean"
|
4
|
+
require "tekido/methods/numeric"
|
5
|
+
require "tekido/methods/array"
|
6
|
+
require "tekido/methods/date"
|
7
|
+
require "tekido/methods/string"
|
8
|
+
|
9
|
+
module Tekido
|
10
|
+
module Methods
|
11
|
+
def self.included(base)
|
12
|
+
base.extend(ClassMethods)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module Tekido
|
3
|
+
module Methods
|
4
|
+
module ClassMethods
|
5
|
+
def list(size, value_defs)
|
6
|
+
range_and_values = range_and_values_from(value_defs)
|
7
|
+
|
8
|
+
Array.new(size) do |i|
|
9
|
+
per = percent_as(:float)
|
10
|
+
match = range_and_values.find { |item| item.first.include?(per) }
|
11
|
+
match ? match.last : nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def range_and_values_from(value_defs)
|
17
|
+
if value_defs.is_a?(Array)
|
18
|
+
range_and_values_from_array(value_defs)
|
19
|
+
elsif value_defs.is_a?(Hash)
|
20
|
+
range_and_values_from_value_ratios(value_defs)
|
21
|
+
else
|
22
|
+
[]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
def range_and_values_from_array(values)
|
26
|
+
[].tap do |array|
|
27
|
+
values.each do |value|
|
28
|
+
min = array.empty? ? 0.0 : array.last.first.max
|
29
|
+
max = min + (100.0 / values.size)
|
30
|
+
array << [(min..max), value]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def range_and_values_from_value_ratios(value_ratios)
|
36
|
+
[].tap do |array|
|
37
|
+
value_ratios.each do |value, ratio|
|
38
|
+
min = array.empty? ? 0.0 : array.last.first.max
|
39
|
+
max = min + ratio.to_f
|
40
|
+
array << [(min..max), value]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|