curio 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +15 -11
- data/Rakefile +1 -3
- data/curio.gemspec +8 -7
- data/lib/curio.rb +57 -8
- data/lib/curio/version.rb +1 -3
- data/test/auto_increment_test.rb +50 -0
- data/test/integration_test.rb +1 -3
- data/test/test_helper.rb +0 -2
- metadata +26 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3deec75594c85a9537d56bf024173def85d503ae
|
4
|
+
data.tar.gz: a299bfbdf527d20373cdbe3d6770a1e63cb735c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 229f0809fd860285d25f0a69766615d10f51d521c1b7a6b93755e974f0d9659d503474bdd43b2168a17f52e706b1b566c7bd1b0f90a1d84063dd91b4758159a8
|
7
|
+
data.tar.gz: ae8f2e8563bc95149350c336bd95d9c512dff8c0eaf3bf859dc6c1e6103e36228b236b2de8c42ccf365d8b5494ed00f2facdefb06da6864565a46e7e0b977b7a
|
data/.travis.yml
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
language: ruby
|
2
|
+
sudo: false
|
3
|
+
cache: bundler
|
4
|
+
script:
|
5
|
+
- bundle exec rake
|
2
6
|
rvm:
|
3
|
-
- 1.9.3
|
4
7
|
- 2.0.0
|
5
|
-
- 2.1.
|
6
|
-
- 2.2.
|
7
|
-
-
|
8
|
-
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
- 2.1.10
|
9
|
+
- 2.2.5
|
10
|
+
- 2.3.1
|
11
|
+
- jruby-9.1.5.0
|
12
|
+
- rbx-3.60
|
13
|
+
env:
|
14
|
+
global:
|
15
|
+
- JRUBY_OPTS='--dev -J-Xmx1024M'
|
16
|
+
matrix:
|
17
|
+
allow_failures:
|
18
|
+
- rvm: rbx-3.60
|
data/Rakefile
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require 'bundler/setup'
|
4
2
|
require 'bundler/gem_tasks'
|
5
3
|
|
@@ -8,7 +6,7 @@ require 'rubocop/rake_task'
|
|
8
6
|
|
9
7
|
RuboCop::RakeTask.new
|
10
8
|
|
11
|
-
task default: %w(rubocop
|
9
|
+
task default: %w(test rubocop)
|
12
10
|
task test: 'test:all'
|
13
11
|
|
14
12
|
namespace :test do
|
data/curio.gemspec
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
lib = File.expand_path('../lib', __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
3
|
require 'curio/version'
|
@@ -8,20 +7,22 @@ Gem::Specification.new do |spec|
|
|
8
7
|
spec.version = Curio::VERSION
|
9
8
|
spec.authors = ['Terje Larsen']
|
10
9
|
spec.email = ['terlar@gmail.com']
|
10
|
+
spec.license = 'MIT'
|
11
|
+
|
11
12
|
spec.summary = 'Mixin for enumerable maps'
|
12
13
|
spec.description = spec.summary
|
13
14
|
spec.homepage = 'https://github.com/terlar/curio'
|
14
|
-
spec.license = 'MIT'
|
15
15
|
|
16
|
-
spec.files = `git ls-files`.split(
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = %w(lib)
|
19
19
|
spec.extra_rdoc_files = %w(LICENSE README.md)
|
20
20
|
|
21
|
-
spec.required_ruby_version = '>=
|
21
|
+
spec.required_ruby_version = '>= 2.0.0'
|
22
22
|
|
23
|
+
spec.add_development_dependency 'pry'
|
23
24
|
spec.add_development_dependency 'bundler'
|
24
|
-
spec.add_development_dependency 'rake', '~>
|
25
|
-
spec.add_development_dependency 'minitest', '~> 5.
|
26
|
-
spec.add_development_dependency 'rubocop', '~> 0.
|
25
|
+
spec.add_development_dependency 'rake', '~> 11.0'
|
26
|
+
spec.add_development_dependency 'minitest', '~> 5.9'
|
27
|
+
spec.add_development_dependency 'rubocop', '~> 0.45'
|
27
28
|
end
|
data/lib/curio.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require 'curio/version'
|
4
2
|
|
5
3
|
require 'forwardable'
|
@@ -14,6 +12,8 @@ class Curio < Module
|
|
14
12
|
end
|
15
13
|
end
|
16
14
|
|
15
|
+
ConstraintError = Class.new(TypeError)
|
16
|
+
|
17
17
|
# Initialize a collection object with given key and type
|
18
18
|
#
|
19
19
|
# :key will be used to determine which method to call on items to get
|
@@ -28,12 +28,16 @@ class Curio < Module
|
|
28
28
|
# @return [undefined]
|
29
29
|
#
|
30
30
|
# @api private
|
31
|
-
def initialize(key, type = String)
|
31
|
+
def initialize(key, type = String, opts = { }.freeze)
|
32
32
|
@key = key
|
33
33
|
@type = type
|
34
|
+
@hooks = []
|
35
|
+
|
36
|
+
add_auto_increment_hook if opts[:auto_increment]
|
34
37
|
|
35
38
|
define_key_method
|
36
39
|
define_coerce_key_method
|
40
|
+
define_hooks_method
|
37
41
|
|
38
42
|
freeze
|
39
43
|
end
|
@@ -55,6 +59,38 @@ class Curio < Module
|
|
55
59
|
end
|
56
60
|
end
|
57
61
|
|
62
|
+
# Add auto increment hook.
|
63
|
+
#
|
64
|
+
# This adds a hook that sets the item key to the next integer based on
|
65
|
+
# the collection count if no item key is available.
|
66
|
+
#
|
67
|
+
# @return [undefined]
|
68
|
+
#
|
69
|
+
# @api private
|
70
|
+
def add_auto_increment_hook
|
71
|
+
unless @type == Integer
|
72
|
+
fail ConstraintError,
|
73
|
+
'auto-increment requires a key type of Integer'
|
74
|
+
end
|
75
|
+
|
76
|
+
@hooks << lambda do |collection, item|
|
77
|
+
return unless item.send(collection.key_method).nil?
|
78
|
+
item.send(:"#{collection.key_method}=", collection.count + 1)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Define #hooks based on @hooks
|
83
|
+
#
|
84
|
+
# @return [Array]
|
85
|
+
#
|
86
|
+
# @api private
|
87
|
+
def define_hooks_method
|
88
|
+
hooks = @hooks
|
89
|
+
define_method :hooks do
|
90
|
+
hooks
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
58
94
|
# Define #key_method based on @key
|
59
95
|
#
|
60
96
|
# @return [undefined]
|
@@ -76,9 +112,9 @@ class Curio < Module
|
|
76
112
|
type = @type
|
77
113
|
define_method :coerce_key do |value|
|
78
114
|
case type.to_s
|
79
|
-
when 'String' then
|
115
|
+
when 'String' then value.to_s
|
80
116
|
when 'Symbol' then :"#{value}"
|
81
|
-
when 'Integer' then
|
117
|
+
when 'Integer' then value.to_i
|
82
118
|
else value
|
83
119
|
end
|
84
120
|
end
|
@@ -122,7 +158,7 @@ class Curio < Module
|
|
122
158
|
def values
|
123
159
|
@map.values
|
124
160
|
end
|
125
|
-
|
161
|
+
alias all values
|
126
162
|
|
127
163
|
# Add an item to the collection
|
128
164
|
#
|
@@ -132,11 +168,12 @@ class Curio < Module
|
|
132
168
|
#
|
133
169
|
# @api public
|
134
170
|
def add(item)
|
171
|
+
call_add_hooks item
|
135
172
|
key = coerce_key item.send(key_method)
|
136
173
|
@map[key.freeze] = item
|
137
174
|
self
|
138
175
|
end
|
139
|
-
|
176
|
+
alias << add
|
140
177
|
|
141
178
|
# Fetch an item from the collection
|
142
179
|
#
|
@@ -176,7 +213,7 @@ class Curio < Module
|
|
176
213
|
def key?(key)
|
177
214
|
@map.key? coerce_key(key)
|
178
215
|
end
|
179
|
-
|
216
|
+
alias has? key?
|
180
217
|
|
181
218
|
# Returns a hash representation of the collection
|
182
219
|
#
|
@@ -208,5 +245,17 @@ class Curio < Module
|
|
208
245
|
@map.freeze
|
209
246
|
super
|
210
247
|
end
|
248
|
+
|
249
|
+
private
|
250
|
+
|
251
|
+
# Run all attached hooks
|
252
|
+
#
|
253
|
+
# Currently there is only the hook for auto increment if the
|
254
|
+
# collection was configured to use this.
|
255
|
+
#
|
256
|
+
# @api private
|
257
|
+
def call_add_hooks(item)
|
258
|
+
hooks.each { |hook| hook.call(self, item) }
|
259
|
+
end
|
211
260
|
end
|
212
261
|
end
|
data/lib/curio/version.rb
CHANGED
@@ -0,0 +1,50 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class AutoIncrementTest < Minitest::Test
|
4
|
+
class Collection
|
5
|
+
include Curio.new(:id, Integer, auto_increment: true)
|
6
|
+
end
|
7
|
+
|
8
|
+
Item = Struct.new(:id)
|
9
|
+
|
10
|
+
attr_reader :collection
|
11
|
+
|
12
|
+
def setup
|
13
|
+
@collection = Collection.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_auto_increment_sets_item_key
|
17
|
+
item1 = Item.new
|
18
|
+
item2 = Item.new
|
19
|
+
collection << item1 << item2
|
20
|
+
|
21
|
+
assert_equal 1, item1.id
|
22
|
+
assert_equal 2, item2.id
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_auto_increment_only_activates_with_missing_item_key
|
26
|
+
item1 = Item.new 5
|
27
|
+
item2 = Item.new
|
28
|
+
collection << item1 << item2
|
29
|
+
|
30
|
+
assert_equal 5, item1.id
|
31
|
+
assert_equal 2, item2.id
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_auto_increment_items_can_be_fetched
|
35
|
+
item1 = Item.new
|
36
|
+
item2 = Item.new
|
37
|
+
collection << item1 << item2
|
38
|
+
|
39
|
+
assert_equal item1, collection[1]
|
40
|
+
assert_equal item2, collection[2]
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_auto_increment_with_non_integer_raises_contstraint_error
|
44
|
+
assert_raises Curio::ConstraintError do
|
45
|
+
Class.new do
|
46
|
+
include Curio.new(:id, String, auto_increment: true)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/test/integration_test.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require_relative 'test_helper'
|
4
2
|
|
5
3
|
# rubocop: disable ClassLength
|
@@ -38,7 +36,7 @@ class IntegrationTest < Minitest::Test
|
|
38
36
|
assert_equal [item1, item2], collection.values
|
39
37
|
end
|
40
38
|
|
41
|
-
def
|
39
|
+
def test_retrieving_an_item
|
42
40
|
item = Item.new 'a'
|
43
41
|
collection << item
|
44
42
|
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: curio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Terje Larsen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pry
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -30,42 +44,42 @@ dependencies:
|
|
30
44
|
requirements:
|
31
45
|
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
47
|
+
version: '11.0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
52
|
- - "~>"
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
54
|
+
version: '11.0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: minitest
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: '5.
|
61
|
+
version: '5.9'
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: '5.
|
68
|
+
version: '5.9'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rubocop
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
73
|
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0.
|
75
|
+
version: '0.45'
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
80
|
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0.
|
82
|
+
version: '0.45'
|
69
83
|
description: Mixin for enumerable maps
|
70
84
|
email:
|
71
85
|
- terlar@gmail.com
|
@@ -85,6 +99,7 @@ files:
|
|
85
99
|
- curio.gemspec
|
86
100
|
- lib/curio.rb
|
87
101
|
- lib/curio/version.rb
|
102
|
+
- test/auto_increment_test.rb
|
88
103
|
- test/integration_test.rb
|
89
104
|
- test/test_helper.rb
|
90
105
|
homepage: https://github.com/terlar/curio
|
@@ -99,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
114
|
requirements:
|
100
115
|
- - ">="
|
101
116
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
117
|
+
version: 2.0.0
|
103
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
119
|
requirements:
|
105
120
|
- - ">="
|
@@ -107,10 +122,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
122
|
version: '0'
|
108
123
|
requirements: []
|
109
124
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.5.1
|
111
126
|
signing_key:
|
112
127
|
specification_version: 4
|
113
128
|
summary: Mixin for enumerable maps
|
114
129
|
test_files:
|
130
|
+
- test/auto_increment_test.rb
|
115
131
|
- test/integration_test.rb
|
116
132
|
- test/test_helper.rb
|