seg 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61836aa10706913042131387b17b1a6ea413ea5e
4
+ data.tar.gz: d4b6729cf00f4c78f5129747bff920d40b0c4302
5
+ SHA512:
6
+ metadata.gz: 62a1d073dffa86ea3481270829b6980fc64ca1578050a45df4a1b5498f1a7f18ad236327c4c29f95d5a13e4616e6b3b5fb66d1401297b61db75823a77d77ffd7
7
+ data.tar.gz: 1771135988a616a990996baee20343c12eff4630f9f07da3d266119780e16c978d5ddd45a9c6dd901371c11e523999a232487177a12b65163643b796d5bd088b
data/.gems ADDED
@@ -0,0 +1 @@
1
+ cutest -v 1.2.2
data/CHANGELOG ADDED
File without changes
data/CONTRIBUTING ADDED
@@ -0,0 +1,19 @@
1
+ This code tries to solve a particular problem with a very simple
2
+ implementation. We try to keep the code to a minimum while making
3
+ it as clear as possible. The design is very likely finished, and
4
+ if some feature is missing it is possible that it was left out on
5
+ purpose. That said, new usage patterns may arise, and when that
6
+ happens we are ready to adapt if necessary.
7
+
8
+ A good first step for contributing is to meet us on IRC and discuss
9
+ ideas. We spend a lot of time on #lesscode at freenode, always ready
10
+ to talk about code and simplicity. If connecting to IRC is not an
11
+ option, you can create an issue explaining the proposed change and
12
+ a use case. We pay a lot of attention to use cases, because our
13
+ goal is to keep the code base simple. Usually the result of a
14
+ conversation is the creation of a different tool.
15
+
16
+ Please don't start the conversation with a pull request. The code
17
+ should come at last, and even though it may help to convey an idea,
18
+ more often than not it draws the attention to a particular
19
+ implementation.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2015 Michel Martens
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ Seg
2
+ ===
3
+
4
+ Segment matcher for paths.
5
+
6
+ Description
7
+ -----------
8
+
9
+ Seg provides two methods for consuming and capturing path segments.
10
+ A path is a string that starts with a slash and contains segments
11
+ separated by slashes, for example `/foo/bar/baz` or `/users/42`.
12
+
13
+ Usage
14
+ -----
15
+
16
+ Consider this interactive session:
17
+
18
+ ```ruby
19
+ >> s = Seg.new("/users/42")
20
+ => #<Seg ...>
21
+
22
+ >> s.prev
23
+ => ""
24
+
25
+ >> s.curr
26
+ => "/users/42"
27
+
28
+ >> s.consume("users")
29
+ => true
30
+
31
+ >> s.prev
32
+ => "/users"
33
+
34
+ >> s.curr
35
+ => "/42"
36
+
37
+ >> s.consume("42")
38
+ => true
39
+
40
+ >> s.prev
41
+ => "/users/42"
42
+
43
+ >> s.curr
44
+ => ""
45
+ ```
46
+
47
+ The previous example shows how to walk the path by
48
+ providing segments to consume. In the following
49
+ example, we'll see what happens when we try to
50
+ consume a segment with a string that doesn't match:
51
+
52
+ ```ruby
53
+ >> s = Seg.new("/users/42")
54
+ => #<Seg ...>
55
+
56
+ >> s.prev
57
+ => ""
58
+
59
+ >> s.curr
60
+ => "/users/42"
61
+
62
+ >> s.consume("admin")
63
+ => false
64
+
65
+ >> s.prev
66
+ => ""
67
+
68
+ >> s.curr
69
+ => "/users/42"
70
+ ```ruby
71
+
72
+ As you can see, the command fails and the `prev` and
73
+ `curr` strings are not altered. Now we'll see
74
+ how to capture segment values:
75
+
76
+ ```ruby
77
+ >> s = Seg.new("/users/42")
78
+ => #<Seg ...>
79
+
80
+ >> captures = {}
81
+ => {}
82
+
83
+ >> s.prev
84
+ => ""
85
+
86
+ >> s.curr
87
+ => "/users/42"
88
+
89
+ >> s.capture(:foo, captures)
90
+ => true
91
+
92
+ >> s.prev
93
+ => "/users"
94
+
95
+ >> s.curr
96
+ => "/42"
97
+
98
+ >> s.capture(:bar, captures)
99
+ => true
100
+
101
+ >> s.prev
102
+ => "/users/42"
103
+
104
+ >> s.curr
105
+ => ""
106
+
107
+ >> captures
108
+ => {:foo=>"users", :bar=>42}
109
+ ```ruby
110
+
111
+ Installation
112
+ ------------
113
+
114
+ ```
115
+ $ gem install seg
116
+ ```
data/lib/seg.rb ADDED
@@ -0,0 +1,88 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright (c) 2015 Michel Martens
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+ #
23
+ class Seg
24
+ SLASH = "/".freeze
25
+
26
+ def initialize(path)
27
+ @path = path
28
+ @size = path.size
29
+ @pos = 1
30
+ end
31
+
32
+ def head
33
+ @path[@pos]
34
+ end
35
+
36
+ def curr
37
+ @path[@pos - 1, @size]
38
+ end
39
+
40
+ def prev
41
+ @path[0, @pos - 1]
42
+ end
43
+
44
+ def find(index)
45
+ @path[@pos + index]
46
+ end
47
+
48
+ def subs(length)
49
+ @path[@pos, length]
50
+ end
51
+
52
+ def move(offset)
53
+ @pos += offset.succ
54
+ end
55
+
56
+ def root?
57
+ @pos >= @size
58
+ end
59
+
60
+ def consume(str)
61
+ return false if root?
62
+
63
+ len = str.size
64
+
65
+ case find(len)
66
+ when nil, SLASH
67
+ if subs(len) == str
68
+ move(len)
69
+ return true
70
+ else
71
+ return false
72
+ end
73
+ else
74
+ return false
75
+ end
76
+ end
77
+
78
+ def capture(sym, store)
79
+ return false if root?
80
+
81
+ len = (@path.index(SLASH, @pos) || @size) - @pos
82
+
83
+ store[sym] = @path[@pos, len]
84
+ move(len)
85
+
86
+ return true
87
+ end
88
+ end
data/makefile ADDED
@@ -0,0 +1,4 @@
1
+ .PHONY: test
2
+
3
+ test:
4
+ cutest -r ./test/helper.rb ./test/*.rb
data/seg.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "seg"
3
+ s.version = "0.0.1"
4
+ s.summary = %{Segment matcher for paths}
5
+ s.description = %Q{Segment matcher for paths.}
6
+ s.authors = ["Michel Martens"]
7
+ s.email = ["michel@soveran.com"]
8
+ s.homepage = "https://github.com/soveran/seg"
9
+ s.license = "MIT"
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+
13
+ s.add_development_dependency "cutest"
14
+ end
data/test/all.rb ADDED
@@ -0,0 +1,54 @@
1
+ test "consume" do
2
+ segment = Seg.new("/foo/bar/baz")
3
+
4
+ assert_equal segment.consume("bar"), false
5
+ assert_equal segment.prev, ""
6
+ assert_equal segment.curr, "/foo/bar/baz"
7
+
8
+ assert_equal segment.consume("foo"), true
9
+ assert_equal segment.prev, "/foo"
10
+ assert_equal segment.curr, "/bar/baz"
11
+
12
+ assert_equal segment.consume("foo"), false
13
+ assert_equal segment.prev, "/foo"
14
+ assert_equal segment.curr, "/bar/baz"
15
+
16
+ assert_equal segment.consume("bar"), true
17
+ assert_equal segment.prev, "/foo/bar"
18
+ assert_equal segment.curr, "/baz"
19
+
20
+ assert_equal segment.consume("baz"), true
21
+ assert_equal segment.prev, "/foo/bar/baz"
22
+ assert_equal segment.curr, ""
23
+
24
+ assert_equal segment.consume("baz"), false
25
+ assert_equal segment.prev, "/foo/bar/baz"
26
+ assert_equal segment.curr, ""
27
+ end
28
+
29
+ test "capture" do
30
+ segment = Seg.new("/foo/bar/baz")
31
+
32
+ captures = {}
33
+
34
+ assert_equal segment.capture(:c1, captures), true
35
+ assert_equal segment.prev, "/foo"
36
+ assert_equal segment.curr, "/bar/baz"
37
+
38
+ assert_equal segment.capture(:c2, captures), true
39
+ assert_equal segment.prev, "/foo/bar"
40
+ assert_equal segment.curr, "/baz"
41
+
42
+ assert_equal segment.capture(:c3, captures), true
43
+ assert_equal segment.prev, "/foo/bar/baz"
44
+ assert_equal segment.curr, ""
45
+
46
+ assert_equal segment.capture(:c4, captures), false
47
+ assert_equal segment.prev, "/foo/bar/baz"
48
+ assert_equal segment.curr, ""
49
+
50
+ assert_equal "foo", captures[:c1]
51
+ assert_equal "bar", captures[:c2]
52
+ assert_equal "baz", captures[:c3]
53
+ assert_equal nil, captures[:c4]
54
+ end
data/test/helper.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative "../lib/seg"
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michel Martens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
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'
27
+ description: Segment matcher for paths.
28
+ email:
29
+ - michel@soveran.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gems
35
+ - CHANGELOG
36
+ - CONTRIBUTING
37
+ - LICENSE
38
+ - README.md
39
+ - lib/seg.rb
40
+ - makefile
41
+ - seg.gemspec
42
+ - test/all.rb
43
+ - test/helper.rb
44
+ homepage: https://github.com/soveran/seg
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.0.14
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Segment matcher for paths
68
+ test_files: []