ehon 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 26332159b5ba10a4e88371c85265c39a3a088f8a
4
+ data.tar.gz: a7a7588f7a1b3fd8fea85c18464eec0f750957cc
5
+ SHA512:
6
+ metadata.gz: da617d040c65e82e0b3e6c569a27855baa70509b35cd2bf91bf173e07ba7336b6c61664a04f4efc1538af2dc419094f96408366f59913b2a88471dff2a24929d
7
+ data.tar.gz: 77abcc82ffabafec73fb62fb2181df3a31d65a25465d46c9d420a5207cc669b882b01d1149328c2ebe6ea9de960a21025183ca8dd7cb263f59a93054760a2aa2
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ehon.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tomohiro Nishimura
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.
@@ -0,0 +1,75 @@
1
+
2
+ # Ehon
3
+ [![Build Status](https://travis-ci.org/hekk/ehon.png?branch=master)](https://travis-ci.org/hekk/ehon)
4
+
5
+ Ehon is a simple `enum` library.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'ehon'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ehon
20
+
21
+ ## Usage
22
+
23
+ You can create your enum class easily.
24
+
25
+ ```ruby
26
+ class DeviceType
27
+ include Ehon
28
+
29
+ default user_agent_regexp: //
30
+
31
+ UNKNOWN = enum 0, name: 'unknown'
32
+ IOS = enum 1, name: 'iOS', user_agent_regexp: /(iPhone|iPad|iPod)/
33
+ ANDROID = enum 2, name: 'Android', user_agent_regexp: /Android/
34
+ end
35
+ ```
36
+
37
+ Using `DeviceType` class.
38
+
39
+ ```ruby
40
+ DeviceType::IOS.id #=> 1
41
+ DeviceType::ANDROID.user_agent_regexp #=> /Android/
42
+ ```
43
+
44
+ Finding.
45
+
46
+ ```ruby
47
+ DeviceType[1, 2] #=> [#<DeviceType:...(iOS)>, #<DeviceType:...(Android)>]
48
+ DeviceType.find(name: 'iOS') #=> #<DeviceType:...(iOS)>
49
+ ```
50
+
51
+ Custom method.
52
+
53
+ ```ruby
54
+ class DeviceType
55
+ %w[iOS Android unknown].each do |name|
56
+ define_method :"#{name.downcase}?" do
57
+ self.name == name
58
+ end
59
+ end
60
+ end
61
+ ```
62
+
63
+ ```ruby
64
+ DeviceTypel::IOS.ios? #=> true
65
+ DeviceTypel::IOS.android? #=> false
66
+ ```
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it ( http://github.com/<my-github-username>/ehon/fork )
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ 4. Push to the branch (`git push origin my-new-feature`)
74
+ 5. Create new Pull Request
75
+
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :console do
9
+ require 'irb'
10
+ require 'irb/completion'
11
+ require 'ehon'
12
+ ARGV.clear
13
+ IRB.start
14
+ end
15
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ehon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ehon"
8
+ spec.version = Ehon::VERSION
9
+ spec.authors = ["Tomohiro Nishimura"]
10
+ spec.email = ["tomohiro68@gmail.com"]
11
+ spec.summary = %q{Ehon is a simple `enum` library.}
12
+ spec.description = %q{Ehon is a simple `enum` library.}
13
+ spec.homepage = "https://github.com/hekk/ehon"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,63 @@
1
+ require "ehon/version"
2
+
3
+ module Ehon
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.contents = {}
7
+ base.default_options = {}
8
+ end
9
+
10
+ def initialize(id, options = {})
11
+ @options = options.merge(id: id)
12
+ end
13
+
14
+ def options
15
+ self.class.default_options.merge(@options)
16
+ end
17
+
18
+ def ==(other)
19
+ (self.class == other.class) && (self.id == other.id)
20
+ end
21
+
22
+ def respond_to_missing?(symbol, include_private = false)
23
+ self.options.has_key?(symbol)
24
+ end
25
+
26
+ def method_missing(symbol, *args)
27
+ return self.options[symbol] if respond_to?(symbol)
28
+ super
29
+ end
30
+
31
+ module ClassMethods
32
+ attr_accessor :contents, :default_options
33
+
34
+ def page(id, options = {}, &block)
35
+ instance = new(id, options)
36
+ instance.instance_eval(&block) if block_given?
37
+ self.contents[id] = instance
38
+ instance
39
+ end
40
+ alias enum page
41
+
42
+ def default(options = {})
43
+ self.default_options.merge!(options)
44
+ end
45
+
46
+ def all
47
+ self.contents.values
48
+ end
49
+
50
+ def find(*queries)
51
+ queries.flatten!
52
+ findeds = queries.map {|query|
53
+ next self.contents[query] unless query.is_a?(Hash)
54
+ self.contents.values.find {|instance|
55
+ query.all? {|key, value| instance.options[key] == value }
56
+ }
57
+ }.compact
58
+ queries.size == 1 ? findeds.first : findeds
59
+ end
60
+ alias [] find
61
+ end
62
+ end
63
+
@@ -0,0 +1,3 @@
1
+ module Ehon
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,284 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ehon do
4
+ it 'should have a version number' do
5
+ expect(Ehon::VERSION).to_not be_nil
6
+ end
7
+
8
+ subject { Item }
9
+
10
+ before :all do
11
+ class Item
12
+ include Ehon
13
+ end
14
+ end
15
+
16
+ after do
17
+ Item.contents = {}
18
+ Item.default_options = {}
19
+ end
20
+
21
+ describe '.page' do
22
+
23
+ shared_examples_for 'a page' do
24
+ it 'should create one instance' do
25
+ expect(subject.contents.size).to eq(1)
26
+ end
27
+
28
+ it 'instance id equals to argument' do
29
+ expect(@instance.id).to eq(expected_id)
30
+ end
31
+ end
32
+
33
+ context 'without options' do
34
+ 3.times do
35
+ random_id = rand(10)
36
+
37
+ before { @instance = subject.page(expected_id) }
38
+
39
+ context "id is #{random_id}" do
40
+ let(:expected_id) { random_id }
41
+ it_behaves_like "a page"
42
+ end
43
+ end
44
+ end
45
+
46
+ context 'with options' do
47
+ before { @instance = subject.page(expected_id, expected_options) }
48
+
49
+ let(:expected_id) { 3 }
50
+ let(:expected_options) { {name: 'potion', value: 5} }
51
+
52
+ it_behaves_like "a page"
53
+
54
+ it 'instance options equals to argument with id: 3' do
55
+ expect(@instance.options).to eq(expected_options.merge(id: 3))
56
+ end
57
+
58
+ 3.times do
59
+ random_key = ('a'..'z').to_a.shuffle.take(5).join.to_sym
60
+ random_value = rand(100)
61
+
62
+ context "with {#{random_key}: #{random_value}}" do
63
+ let(:expected_options) { {random_key => random_value} }
64
+
65
+ it "responds to #{random_key}" do
66
+ expect(@instance).to be_respond_to(random_key)
67
+ end
68
+
69
+ it "instance should return #{random_value} when call #{random_key}" do
70
+ expect(@instance.__send__(random_key)).to eq(random_value)
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ context 'with default options' do
77
+ before do
78
+ class Item
79
+ default key: 'value'
80
+ end
81
+ @instance = subject.page(1)
82
+ end
83
+
84
+ it "should has default option {key: 'value'}" do
85
+ expect(Item.default_options).to eq({key: 'value'})
86
+ end
87
+
88
+ it "instacne should respond to key and return 'value'" do
89
+ expect(@instance.key).to eq('value')
90
+ end
91
+ end
92
+
93
+ context 'with block' do
94
+ before do
95
+ @instance_with_block = subject.page(1, name: 'potion') {
96
+ def potion?
97
+ true
98
+ end
99
+ }
100
+ @instance_without_block = subject.page(2, name: 'pass')
101
+ end
102
+
103
+ it 'instance_with_block should respond to `potion?`' do
104
+ expect(@instance_with_block).to be_respond_to(:potion?)
105
+ end
106
+
107
+ it 'instance_with_block should *not* respond to `potion?`' do
108
+ expect(@instance_without_block).to_not be_respond_to(:potion?)
109
+ end
110
+ end
111
+ end
112
+
113
+ describe '#==(other)' do
114
+ context 'same class' do
115
+ before do
116
+ @instance = Item.page(1)
117
+ @other = @instance.dup
118
+ end
119
+ it 'instance should equal to cpy' do
120
+ expect(@instance).to eq(@other)
121
+ end
122
+ end
123
+
124
+ context 'same id but other class' do
125
+ before do
126
+ class Other
127
+ include Ehon
128
+ end
129
+ @instance = Item.page(1)
130
+ @other = Other.page(1)
131
+ end
132
+ it "instance should not equal to other" do
133
+ expect(@instance).to_not eq(@other)
134
+ end
135
+ end
136
+
137
+ context 'with case' do
138
+ before do
139
+ class Difficulty
140
+ include Ehon
141
+
142
+ EASY = enum 1, name: 'Easy mode'
143
+ NORMAL = enum 2, name: 'Normal mode'
144
+ HARD = enum 3, name: 'Hard mode'
145
+ end
146
+ @selected = Difficulty::HARD
147
+ end
148
+
149
+ it 'selected mode should be hard' do
150
+ should satisfy {
151
+ case @selected
152
+ when Difficulty::EASY then false
153
+ when Difficulty::NORMAL then false
154
+ when Difficulty::HARD then true
155
+ else; false
156
+ end
157
+ }
158
+ end
159
+ end
160
+ end
161
+
162
+ describe '.all' do
163
+ before do
164
+ subject.page 1
165
+ subject.page 2
166
+ subject.page 3
167
+ end
168
+
169
+ it 'should has 3 pages' do
170
+ expect(subject.all.size).to eq(3)
171
+ end
172
+
173
+ # FIXME: it depends an implementation
174
+ it 'all should return contents.values' do
175
+ expect(subject.all).to eq(subject.contents.values)
176
+ end
177
+ end
178
+
179
+ describe '.find(id_or_query)' do
180
+ before do
181
+ subject.page 1, name: 'potion', value: 5
182
+ subject.page 2, name: 'high potion', value: 10
183
+ subject.page '3', name: 'scroll'
184
+ end
185
+
186
+ context 'with key' do
187
+ it 'find with id 1 should return potion' do
188
+ finded = subject.find(1)
189
+ expect(finded.name).to eq('potion')
190
+ end
191
+
192
+ it "find with id '3' should return scroll" do
193
+ finded = subject.find('3')
194
+ expect(finded.name).to eq('scroll')
195
+ end
196
+
197
+ it 'find with id 12 should return nil' do
198
+ finded = subject.find(12)
199
+ expect(finded).to be_nil
200
+ end
201
+ end
202
+
203
+ context 'with multiple id' do
204
+ it 'find with id 1 and 2 should return `potion` and `high potion`' do
205
+ finded = subject.find(1, 2)
206
+ expect(finded.map(&:name)).to eq(['potion', 'high potion'])
207
+ end
208
+
209
+ it 'find with id 1 and 9 should return `potion` only but array' do
210
+ finded = subject.find(1, 9)
211
+ expect(finded.map(&:name)).to eq(['potion'])
212
+ end
213
+
214
+ it 'find with id [1, 2] should return `potion` only but array' do
215
+ finded = subject.find([1, 2])
216
+ expect(finded.map(&:name)).to eq(['potion', 'high potion'])
217
+ end
218
+
219
+ it 'find with id [4, 8] should return empty array' do
220
+ finded = subject.find([4, 8])
221
+ expect(finded).to eq([])
222
+ end
223
+ end
224
+
225
+ context 'with query' do
226
+ it "find with `{name: 'high potion'}` should return item id 2" do
227
+ finded = subject.find(name: 'high potion')
228
+ expect(finded.id).to eq(2)
229
+ end
230
+
231
+ it "find with `{value: 5}` should return item id 1" do
232
+ finded = subject.find(value: 5)
233
+ expect(finded.id).to eq(1)
234
+ end
235
+
236
+ it "find with `{id: '3'}` should return item id '3'" do
237
+ finded = subject.find(id: '3')
238
+ expect(finded.id).to eq('3')
239
+ end
240
+
241
+ it "find with `{name: 'food'}` should return nil" do
242
+ finded = subject.find(name: 'food')
243
+ expect(finded).to be_nil
244
+ end
245
+
246
+ context 'AND search' do
247
+ it "find with `{name: 'potion', value: 5}` should return item id 1" do
248
+ finded = subject.find(name: 'potion', value: 5)
249
+ expect(finded.id).to eq(1)
250
+ end
251
+
252
+ it "find with `{name: 'potion', value: 10}` should return nil" do
253
+ finded = subject.find(name: 'potion', value: 10)
254
+ expect(finded).to be_nil
255
+ end
256
+ end
257
+
258
+ context 'OR search' do
259
+ it "find with `[{name: 'potion'}, {value: 10}]` should return item id `potion` and `high potion`" do
260
+ finded = subject.find([{name: 'potion'}, {value: 10}])
261
+ expect(finded.map(&:name)).to eq(['potion', 'high potion'])
262
+ end
263
+
264
+ it "find with `{name: 'potion', value: 20}` should return potion only but array" do
265
+ finded = subject.find([{name: 'potion'}, {value: 20}])
266
+ expect(finded.map(&:name)).to eq(['potion'])
267
+ end
268
+
269
+ it "find with `{name: 'fire potion', value: 20}` should return empty array" do
270
+ finded = subject.find([{name: 'fire potion'}, {value: 20}])
271
+ expect(finded).to eq([])
272
+ end
273
+ end
274
+ end
275
+
276
+ context 'with id and query' do
277
+ it "find with `[1, {value: 10}]` should return item id `potion` and `high potion`" do
278
+ finded = subject.find([1, {value: 10}])
279
+ expect(finded.map(&:name)).to eq(['potion', 'high potion'])
280
+ end
281
+ end
282
+ end
283
+ end
284
+
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'ehon'
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ehon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomohiro Nishimura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Ehon is a simple `enum` library.
56
+ email:
57
+ - tomohiro68@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - ehon.gemspec
70
+ - lib/ehon.rb
71
+ - lib/ehon/version.rb
72
+ - spec/ehon_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: https://github.com/hekk/ehon
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Ehon is a simple `enum` library.
98
+ test_files:
99
+ - spec/ehon_spec.rb
100
+ - spec/spec_helper.rb