ehon 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/Guardfile +24 -0
- data/README.md +2 -0
- data/ehon.gemspec +1 -0
- data/lib/ehon.rb +32 -3
- data/lib/ehon/version.rb +1 -1
- data/spec/ehon_spec.rb +101 -22
- metadata +20 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cfe34ca1049b454d6f6c9ad5901e9655f2154c4
|
4
|
+
data.tar.gz: 3a036d5f298836e064c98d21d6a87f386797dbef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e9d45b8c90d1c7d628fab88eadbcfd87f277fe3297b2f60b8c09fcf02038d60312dde8fd7ed14cf52de034860ae5a0537a9a12aba11aec9182992d53adeb6a9
|
7
|
+
data.tar.gz: 827789868d73ed13b1f6e238b010cb9824ee9bcde60e60f8bffbf988a0f5051b8b1340bc9eea684abc212ccf526c4102bdf17b29a6d21d53933282c11f67df3f
|
data/CHANGELOG.md
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :rspec do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
11
|
+
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
12
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
13
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
14
|
+
watch('config/routes.rb') { "spec/routing" }
|
15
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
16
|
+
|
17
|
+
# Capybara features specs
|
18
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
19
|
+
|
20
|
+
# Turnip features and steps
|
21
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
22
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
23
|
+
end
|
24
|
+
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
|
2
2
|
# Ehon
|
3
3
|
[![Build Status](https://travis-ci.org/hekk/ehon.png?branch=master)](https://travis-ci.org/hekk/ehon)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/hekk/ehon.png)](https://codeclimate.com/github/hekk/ehon)
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/ehon.png)](http://badge.fury.io/rb/ehon)
|
4
6
|
|
5
7
|
Ehon is a simple `enum` library.
|
6
8
|
|
data/ehon.gemspec
CHANGED
data/lib/ehon.rb
CHANGED
@@ -35,18 +35,30 @@ module Ehon
|
|
35
35
|
module ClassMethods
|
36
36
|
attr_accessor :contents, :default_options
|
37
37
|
|
38
|
-
def
|
38
|
+
def enum(id, options = {}, &block)
|
39
39
|
instance = new(id, options)
|
40
40
|
instance.instance_eval(&block) if block_given?
|
41
41
|
self.contents[id] = instance
|
42
42
|
instance
|
43
43
|
end
|
44
|
-
alias enum page
|
45
44
|
|
46
45
|
def default(options = {})
|
47
46
|
self.default_options.merge!(options)
|
48
47
|
end
|
49
48
|
|
49
|
+
def create_readers!
|
50
|
+
options.each {|option| define_reader option }
|
51
|
+
end
|
52
|
+
|
53
|
+
def create_writers!
|
54
|
+
options.each {|option| define_writer option }
|
55
|
+
end
|
56
|
+
|
57
|
+
def create_accessors!
|
58
|
+
create_readers!
|
59
|
+
create_writers!
|
60
|
+
end
|
61
|
+
|
50
62
|
def all
|
51
63
|
self.contents.values
|
52
64
|
end
|
@@ -62,6 +74,23 @@ module Ehon
|
|
62
74
|
queries.size == 1 ? findeds.first : findeds
|
63
75
|
end
|
64
76
|
alias [] find
|
77
|
+
|
78
|
+
private
|
79
|
+
def options
|
80
|
+
self.contents.values.map {|e| e.options.keys }.flatten.uniq
|
81
|
+
end
|
82
|
+
|
83
|
+
def define_reader(symbol)
|
84
|
+
class_eval do
|
85
|
+
define_method(symbol) { read_attribute(symbol) }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def define_writer(symbol)
|
90
|
+
return if symbol == :id
|
91
|
+
class_eval do
|
92
|
+
define_method("#{symbol}=") {|value| @options[symbol] = value }
|
93
|
+
end
|
94
|
+
end
|
65
95
|
end
|
66
96
|
end
|
67
|
-
|
data/lib/ehon/version.rb
CHANGED
data/spec/ehon_spec.rb
CHANGED
@@ -18,9 +18,9 @@ describe Ehon do
|
|
18
18
|
Item.default_options = {}
|
19
19
|
end
|
20
20
|
|
21
|
-
describe '.
|
21
|
+
describe '.enum' do
|
22
22
|
|
23
|
-
shared_examples_for 'a
|
23
|
+
shared_examples_for 'a enum' do
|
24
24
|
it 'should create one instance' do
|
25
25
|
expect(subject.contents.size).to eq(1)
|
26
26
|
end
|
@@ -34,22 +34,22 @@ describe Ehon do
|
|
34
34
|
3.times do
|
35
35
|
random_id = rand(10)
|
36
36
|
|
37
|
-
before { @instance = subject.
|
37
|
+
before { @instance = subject.enum(expected_id) }
|
38
38
|
|
39
39
|
context "id is #{random_id}" do
|
40
40
|
let(:expected_id) { random_id }
|
41
|
-
it_behaves_like "a
|
41
|
+
it_behaves_like "a enum"
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
46
|
context 'with options' do
|
47
|
-
before { @instance = subject.
|
47
|
+
before { @instance = subject.enum(expected_id, expected_options) }
|
48
48
|
|
49
49
|
let(:expected_id) { 3 }
|
50
50
|
let(:expected_options) { {name: 'potion', value: 5} }
|
51
51
|
|
52
|
-
it_behaves_like "a
|
52
|
+
it_behaves_like "a enum"
|
53
53
|
|
54
54
|
it 'instance options equals to argument with id: 3' do
|
55
55
|
expect(@instance.options).to eq(expected_options.merge(id: 3))
|
@@ -76,7 +76,7 @@ describe Ehon do
|
|
76
76
|
let(:name) { 'scroll' }
|
77
77
|
|
78
78
|
before do
|
79
|
-
@instance = subject.
|
79
|
+
@instance = subject.enum(1, 'name' => name)
|
80
80
|
end
|
81
81
|
|
82
82
|
it 'should not be able to respond to name' do
|
@@ -94,7 +94,7 @@ describe Ehon do
|
|
94
94
|
class Item
|
95
95
|
default key: 'value'
|
96
96
|
end
|
97
|
-
@instance = subject.
|
97
|
+
@instance = subject.enum(1)
|
98
98
|
end
|
99
99
|
|
100
100
|
it "should has default option {key: 'value'}" do
|
@@ -108,12 +108,12 @@ describe Ehon do
|
|
108
108
|
|
109
109
|
context 'with block' do
|
110
110
|
before do
|
111
|
-
@instance_with_block = subject.
|
111
|
+
@instance_with_block = subject.enum(1, name: 'potion') {
|
112
112
|
def potion?
|
113
113
|
true
|
114
114
|
end
|
115
115
|
}
|
116
|
-
@instance_without_block = subject.
|
116
|
+
@instance_without_block = subject.enum(2, name: 'pass')
|
117
117
|
end
|
118
118
|
|
119
119
|
it 'instance_with_block should respond to `potion?`' do
|
@@ -134,7 +134,7 @@ describe Ehon do
|
|
134
134
|
"%s(%d)" % [name, value]
|
135
135
|
end
|
136
136
|
end
|
137
|
-
@instance = subject.
|
137
|
+
@instance = subject.enum(1, name: 'potion', value: 5)
|
138
138
|
end
|
139
139
|
|
140
140
|
after do
|
@@ -152,7 +152,7 @@ describe Ehon do
|
|
152
152
|
describe '#==(other)' do
|
153
153
|
context 'same class' do
|
154
154
|
before do
|
155
|
-
@instance = Item.
|
155
|
+
@instance = Item.enum(1)
|
156
156
|
@other = @instance.dup
|
157
157
|
end
|
158
158
|
it 'instance should equal to cpy' do
|
@@ -165,8 +165,8 @@ describe Ehon do
|
|
165
165
|
class Other
|
166
166
|
include Ehon
|
167
167
|
end
|
168
|
-
@instance = Item.
|
169
|
-
@other = Other.
|
168
|
+
@instance = Item.enum(1)
|
169
|
+
@other = Other.enum(1)
|
170
170
|
end
|
171
171
|
it "instance should not equal to other" do
|
172
172
|
expect(@instance).to_not eq(@other)
|
@@ -200,12 +200,12 @@ describe Ehon do
|
|
200
200
|
|
201
201
|
describe '.all' do
|
202
202
|
before do
|
203
|
-
subject.
|
204
|
-
subject.
|
205
|
-
subject.
|
203
|
+
subject.enum 1
|
204
|
+
subject.enum 2
|
205
|
+
subject.enum 3
|
206
206
|
end
|
207
207
|
|
208
|
-
it 'should has 3
|
208
|
+
it 'should has 3 enums' do
|
209
209
|
expect(subject.all.size).to eq(3)
|
210
210
|
end
|
211
211
|
|
@@ -217,9 +217,9 @@ describe Ehon do
|
|
217
217
|
|
218
218
|
describe '.find(id_or_query)' do
|
219
219
|
before do
|
220
|
-
subject.
|
221
|
-
subject.
|
222
|
-
subject.
|
220
|
+
subject.enum 1, name: 'potion', value: 5
|
221
|
+
subject.enum 2, name: 'high potion', value: 10
|
222
|
+
subject.enum '3', name: 'scroll'
|
223
223
|
end
|
224
224
|
|
225
225
|
context 'with key' do
|
@@ -319,5 +319,84 @@ describe Ehon do
|
|
319
319
|
end
|
320
320
|
end
|
321
321
|
end
|
322
|
-
end
|
323
322
|
|
323
|
+
describe '.create_xxxs!' do
|
324
|
+
before do
|
325
|
+
Item.enum 1, name: 'test'
|
326
|
+
end
|
327
|
+
|
328
|
+
after do
|
329
|
+
subject.class_eval do
|
330
|
+
remove_method :name rescue nil
|
331
|
+
remove_method :name= rescue nil
|
332
|
+
end
|
333
|
+
end
|
334
|
+
|
335
|
+
context 'create both' do
|
336
|
+
before do
|
337
|
+
subject.create_accessors!
|
338
|
+
end
|
339
|
+
|
340
|
+
it 'has id attribute reader' do
|
341
|
+
item = subject.find(1)
|
342
|
+
expect(item.id).to eq(1)
|
343
|
+
end
|
344
|
+
|
345
|
+
it 'does not have id attribute writer' do
|
346
|
+
expect { subject.public_instance_method(:id=) }.to raise_error
|
347
|
+
end
|
348
|
+
|
349
|
+
it 'has name attribute reader' do
|
350
|
+
expect { subject.public_instance_method(:name) }.not_to raise_error
|
351
|
+
item = subject.find(1)
|
352
|
+
expect(item.name).to eq('test')
|
353
|
+
end
|
354
|
+
|
355
|
+
it 'has name= attribute writer' do
|
356
|
+
expect { subject.public_instance_method(:name=) }.not_to raise_error
|
357
|
+
expect_name = 'cat'
|
358
|
+
item = subject.find(1)
|
359
|
+
item.name = expect_name
|
360
|
+
expect(item.name).to eq(expect_name)
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
context 'create only reader' do
|
365
|
+
before do
|
366
|
+
subject.create_readers!
|
367
|
+
end
|
368
|
+
|
369
|
+
it 'has name attribute readers' do
|
370
|
+
expect{ subject.public_instance_method(:name) }.not_to raise_error
|
371
|
+
end
|
372
|
+
|
373
|
+
it 'does not have name= attribute writer' do
|
374
|
+
expect { subject.public_instance_method(:name=) }.to raise_error
|
375
|
+
end
|
376
|
+
end
|
377
|
+
|
378
|
+
context 'create only writers' do
|
379
|
+
before do
|
380
|
+
subject.create_writers!
|
381
|
+
end
|
382
|
+
|
383
|
+
it 'does not have name attribute reader' do
|
384
|
+
expect{ subject.public_instance_method(:name) }.to raise_error
|
385
|
+
end
|
386
|
+
|
387
|
+
it 'has name= attribute writer' do
|
388
|
+
expect { subject.public_instance_method(:name=) }.not_to raise_error
|
389
|
+
end
|
390
|
+
end
|
391
|
+
|
392
|
+
context 'duplicate define' do
|
393
|
+
before do
|
394
|
+
subject.create_accessors!
|
395
|
+
end
|
396
|
+
|
397
|
+
it 'does nothing' do
|
398
|
+
expect { subject.create_accessors! }.to_not raise_error
|
399
|
+
end
|
400
|
+
end
|
401
|
+
end
|
402
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ehon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomohiro Nishimura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard-rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: Ehon is a simple `enum` library.
|
56
70
|
email:
|
57
71
|
- tomohiro68@gmail.com
|
@@ -62,7 +76,9 @@ files:
|
|
62
76
|
- ".gitignore"
|
63
77
|
- ".rspec"
|
64
78
|
- ".travis.yml"
|
79
|
+
- CHANGELOG.md
|
65
80
|
- Gemfile
|
81
|
+
- Guardfile
|
66
82
|
- LICENSE.txt
|
67
83
|
- README.md
|
68
84
|
- Rakefile
|
@@ -91,10 +107,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
107
|
version: '0'
|
92
108
|
requirements: []
|
93
109
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.2.
|
110
|
+
rubygems_version: 2.2.2
|
95
111
|
signing_key:
|
96
112
|
specification_version: 4
|
97
113
|
summary: Ehon is a simple `enum` library.
|
98
114
|
test_files:
|
99
115
|
- spec/ehon_spec.rb
|
100
116
|
- spec/spec_helper.rb
|
117
|
+
has_rdoc:
|