Sha256: a9d5e5879b7d902f5fac00da1ede4a7ee86ca67dbb7ff33f50abd103678f826d

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

Seg
===

Segment matcher for paths.

Description
-----------

Seg provides two methods for consuming and capturing path segments.
A path is a string that starts with a slash and contains segments
separated by slashes, for example `/foo/bar/baz` or `/users/42`.

Usage
-----

Consider this interactive session:

```ruby
>> s = Seg.new("/users/42")
=> #<Seg ...>

>> s.prev
=> ""

>> s.curr
=> "/users/42"

>> s.consume("users")
=> true

>> s.prev
=> "/users"

>> s.curr
=> "/42"

>> s.consume("42")
=> true

>> s.prev
=> "/users/42"

>> s.curr
=> ""
```

The previous example shows how to walk the path by
providing segments to consume. In the following
example, we'll see what happens when we try to
consume a segment with a string that doesn't match:

```ruby
>> s = Seg.new("/users/42")
=> #<Seg ...>

>> s.prev
=> ""

>> s.curr
=> "/users/42"

>> s.consume("admin")
=> false

>> s.prev
=> ""

>> s.curr
=> "/users/42"
```ruby

As you can see, the command fails and the `prev` and
`curr` strings are not altered. Now we'll see
how to capture segment values:

```ruby
>> s = Seg.new("/users/42")
=> #<Seg ...>

>> captures = {}
=> {}

>> s.prev
=> ""

>> s.curr
=> "/users/42"

>> s.capture(:foo, captures)
=> true

>> s.prev
=> "/users"

>> s.curr
=> "/42"

>> s.capture(:bar, captures)
=> true

>> s.prev
=> "/users/42"

>> s.curr
=> ""

>> captures
=> {:foo=>"users", :bar=>42}
```ruby

Installation
------------

```
$ gem install seg
```

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
seg-0.0.1 README.md