gamera-symbolmatrix 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +10 -0
- data/Gemfile +18 -0
- data/Guardfile +39 -0
- data/LICENSE +22 -0
- data/README.md +201 -0
- data/Rakefile +10 -0
- data/features/parse_serialization.feature +26 -0
- data/features/print_to_yaml.feature +20 -0
- data/features/serialize.feature +43 -0
- data/features/stepdefs/parse_serialization.rb +11 -0
- data/features/stepdefs/print_to_yaml.rb +7 -0
- data/features/stepdefs/serialize.rb +11 -0
- data/features/support/env.rb +3 -0
- data/lib/reader/symbolmatrix.rb +37 -0
- data/lib/symbolmatrix.rb +9 -0
- data/lib/symbolmatrix/serialization.rb +43 -0
- data/lib/symbolmatrix/symbolmatrix.rb +122 -0
- data/lib/symbolmatrix/version.rb +3 -0
- data/lib/writer/symbolmatrix.rb +59 -0
- data/spec/complete_features_helper.rb +3 -0
- data/spec/plain_symbolmatrix_helper.rb +1 -0
- data/spec/reader/symbolmatrix_spec.rb +149 -0
- data/spec/symbolmatrix/serialization_spec.rb +133 -0
- data/spec/symbolmatrix/symbolmatrix_spec.rb +275 -0
- data/spec/writer/symbolmatrix_spec.rb +204 -0
- data/symbolmatrix.gemspec +22 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9534cb08aea3a9814730c6157232b713ea3682fe
|
4
|
+
data.tar.gz: 0a0c75fde4ee1a69bb7dc465d0f857fcea0abb42
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c634026a5ec9ee98c243e92be12da9e30370b78fbbd2f5496abd56b16b043268d68b6be88c24bf90ec9951da7862f54584c1e953959d55e9cbdc2ad4faddefe0
|
7
|
+
data.tar.gz: bd829115ecb7b58826f10d6f4de13a590b102c8555affc0fb36eb18c0c85939f311b2e005e23dba410d7d777c58a418c78ad2ab2e7a5fad04a5e5595e6673a91
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in symbolmatrix.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
# Travis CI me lo pide
|
7
|
+
group :test do
|
8
|
+
gem "rake"
|
9
|
+
end
|
10
|
+
|
11
|
+
group :development do
|
12
|
+
gem "guard-rspec"
|
13
|
+
gem "guard-cucumber"
|
14
|
+
gem "libnotify"
|
15
|
+
gem "cucumber"
|
16
|
+
gem "pry"
|
17
|
+
gem "rb-inotify"
|
18
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :cli => "--color --format doc" do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
8
|
+
watch('spec/spec_helper.rb') { "spec" }
|
9
|
+
# watch('spec/complete_features_helper.rb') { "spec" }
|
10
|
+
# watch('spec/plain_symbolmatrix_helper.rb') { "spec" }
|
11
|
+
|
12
|
+
# Rails example
|
13
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
14
|
+
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
15
|
+
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"] }
|
16
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
17
|
+
watch('config/routes.rb') { "spec/routing" }
|
18
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
19
|
+
|
20
|
+
# Capybara features specs
|
21
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
22
|
+
|
23
|
+
# Turnip features and steps
|
24
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
25
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
#guard 'cucumber', :cli => "-ci" do
|
30
|
+
# watch(%r{^features/.+\.feature$})
|
31
|
+
# watch(%r{^features/support/.+$}) { 'features' }
|
32
|
+
# watch(%r{^features/stepdefs/(.+)\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
|
33
|
+
#end
|
34
|
+
|
35
|
+
guard 'cucumber' do
|
36
|
+
watch(%r{^features/.+\.feature$})
|
37
|
+
watch(%r{^features/support/.+$}) { 'features' }
|
38
|
+
watch(%r{^features/stepdefs/(.+)\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
|
39
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Fernando Via
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
_This is a (hopefully temporary) fork of http://github.com/fetcher/symbolmatrix which incorporates some changes from @diminish7 that we need for https://github.com/gamera-team/gamera_
|
2
|
+
|
3
|
+
SymbolMatrix
|
4
|
+
============
|
5
|
+
|
6
|
+
> Strongly inspired in [SymbolTable][symboltable] gem by [Michael Jackson][michael-jackson-home] (thanks a lot!)
|
7
|
+
|
8
|
+
> **New!**: SymbolMatrix now features a serialization syntax from which it can be parsed and to which it can be serialized. I'm calling it SMAS (SymbolMAtrix Serialization) and it's loosely inspired in both [rake][rake-link] and [thor][thor-link]'s argument syntax styles. Read more about it in the *SMAS* section downstairs.
|
9
|
+
|
10
|
+
**SymbolMatrix** is a simple Ruby Gem that extends `Hash` so that only `Symbol`s can be used as keys. Why? Because it also allows you to query the SymbolMatrix object using the dot syntax and a string key equivalent to the symbol one, making it more Principle of Least Surprise.
|
11
|
+
|
12
|
+
The showdown:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
h = Hash.new
|
16
|
+
h[:a] = 1
|
17
|
+
h[:a] # => 1
|
18
|
+
h['a'] # => nil
|
19
|
+
h.a # => NoMethodError: undefined method `a' for {}:Hash
|
20
|
+
|
21
|
+
require 'symbolmatrix'
|
22
|
+
|
23
|
+
m = SymbolMatrix.new
|
24
|
+
m[:a] = 1
|
25
|
+
m[:a] # => 1
|
26
|
+
m['a'] # => 1
|
27
|
+
m.a # => 1
|
28
|
+
```
|
29
|
+
|
30
|
+
If you are familiar with SymbolTable you may have noticed that, so far, the same functionality is provided in the original gem. SymbolMatrix adds three key features:
|
31
|
+
|
32
|
+
1. **Recursive convertion to SymbolMatrix**
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require "symbolmatrix"
|
36
|
+
|
37
|
+
m = SymbolMatrix :quantum => { :nano => "data" }
|
38
|
+
|
39
|
+
m.quatum.nano # => "data"
|
40
|
+
```
|
41
|
+
|
42
|
+
2. **JSON/YAML autoloading**
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
require "symbolmatrix"
|
46
|
+
|
47
|
+
m = SymbolMatrix "quantum: of solace"
|
48
|
+
m.quantum # => "of solace"
|
49
|
+
|
50
|
+
# Or alternatively
|
51
|
+
m.from.yaml "quantum: of solace"
|
52
|
+
|
53
|
+
# Provided a file exists "configuration.yaml" with "database_uri: mysql://root@localhost/database"
|
54
|
+
m.from.file "configuration.yaml"
|
55
|
+
m.database_uri # => "mysql://root@localhost/database"
|
56
|
+
|
57
|
+
# Or simply
|
58
|
+
m = SymbolMatrix "configuration.yaml"
|
59
|
+
```
|
60
|
+
|
61
|
+
Since all JSON is valid YAML... yey, it works with JSON too!
|
62
|
+
```ruby
|
63
|
+
require 'symbolmatrix'
|
64
|
+
|
65
|
+
data = SymbolMatrix '{"message":"Awesome"}'
|
66
|
+
data.message # => 'Awesome'
|
67
|
+
```
|
68
|
+
|
69
|
+
3. **Own serialization format for command line**
|
70
|
+
|
71
|
+
Look the next section
|
72
|
+
|
73
|
+
[symboltable]: https://github.com/mjijackson/symboltable
|
74
|
+
[michael-jackson-home]: http://mjijackson.com/
|
75
|
+
[rake-link]: https://github.com/jimweirich/rake
|
76
|
+
[thor-link]: https://github.com/wycats/thor
|
77
|
+
|
78
|
+
SMAS: SymbolMatrix Serialization
|
79
|
+
--------------------------------
|
80
|
+
|
81
|
+
The serialization format is designed to serve as an alternative for the command line environment, when multiline (YAML) and special characters (JSON) are not well suited. It's very intuitive and designed to closely resemble the way SymbolMatrix is used within Ruby code. For example:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
require 'symbolmatrix'
|
85
|
+
|
86
|
+
s = SymbolMatrix 'this.serialization:format i.think:rocks!'
|
87
|
+
|
88
|
+
s.this.serialization # => "format"
|
89
|
+
s.i.think # => "rocks!"
|
90
|
+
|
91
|
+
s.from.serialization 'works.with.numbers:793'
|
92
|
+
|
93
|
+
s.works.with.numbers # => 793
|
94
|
+
```
|
95
|
+
|
96
|
+
You can get the serialization from any SymbolMatrix:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
require 'symbolmatrix'
|
100
|
+
|
101
|
+
SymbolMatrix(:where => {
|
102
|
+
:is => {
|
103
|
+
:paris => "france",
|
104
|
+
:roma => "italia"
|
105
|
+
}
|
106
|
+
).to.serialization # => 'where.is.paris:france where.is.roma:italia'
|
107
|
+
```
|
108
|
+
|
109
|
+
> This serialization format is better exploited in the [Empezar Ruby Gem][empezar-link]
|
110
|
+
|
111
|
+
[empezar-link]: https://github.com/Fetcher/empezar
|
112
|
+
|
113
|
+
Further details
|
114
|
+
---------------
|
115
|
+
|
116
|
+
### Recursive `#to.hash` convertion
|
117
|
+
|
118
|
+
SymbolMatrix provides the `#to.hash` method that recursively transforms the matrix into a n-dimentional Hash.
|
119
|
+
|
120
|
+
### Disabling the extras
|
121
|
+
|
122
|
+
If for some reason you don't want this class to use YAML nor the serialization format SMAS, you can just require `symbolmatrix/symbolmatrix` and those functionalities will not be loaded.
|
123
|
+
|
124
|
+
### Exceptions
|
125
|
+
|
126
|
+
Whenever a key is not found in the SymbolMatrix, a custom `SymbolMatrix::KeyNotDefinedException` will be raised.
|
127
|
+
Similarly, if you attempt to use an inconvertible key (inconvertible to `Symbol`) a `SymbolMatrix::InvalidKeyException` will be raised.
|
128
|
+
|
129
|
+
Changelog for 1.0.0
|
130
|
+
-------------------
|
131
|
+
|
132
|
+
Serialization format aside, there had been some minor changes to the API worth noting:
|
133
|
+
|
134
|
+
### Deprecations
|
135
|
+
|
136
|
+
As of version 1.0.0, Writers and Readers has been added, so now the regular methods `from_yaml`, `from_file` and `to_hash` are deprecated in favor of `from.yaml`, `from.file` and `to.hash`. Please change to the new API is you're working with SymbolMatrix right now.
|
137
|
+
|
138
|
+
### `.new` no longer needed
|
139
|
+
|
140
|
+
`SymbolMatrix :a => 'b'` is now equivalent to `SymbolMatrix.new(:a => 'b')`.
|
141
|
+
|
142
|
+
SymbolMatrix is mostly about the developers having an easier time with code so I place strong priority in these small changes for comfort.
|
143
|
+
|
144
|
+
### `#recursive_merge` available
|
145
|
+
|
146
|
+
> Note: this is not yet working with any Hash, just SymbolMatrices.
|
147
|
+
|
148
|
+
Yey! This one was missing quite a while from Ruby Hashes (if you are like me and spend a lot of time dealing with multidimensional hashes).
|
149
|
+
|
150
|
+
The API is straightforward:
|
151
|
+
```ruby
|
152
|
+
sm = SymbolMatrix(:a => { :b => "c" })
|
153
|
+
sm.recursive_merge SymbolMatrix(:a => { :c => "d"})
|
154
|
+
|
155
|
+
sm.a.b # => "c"
|
156
|
+
sm.a.c # => "d"
|
157
|
+
```
|
158
|
+
|
159
|
+
Installation
|
160
|
+
------------
|
161
|
+
|
162
|
+
gem install symbolmatrix
|
163
|
+
|
164
|
+
### Or using Bundler
|
165
|
+
Add this line to your application's Gemfile:
|
166
|
+
|
167
|
+
gem 'symbolmatrix'
|
168
|
+
|
169
|
+
And then execute:
|
170
|
+
|
171
|
+
bundle install
|
172
|
+
|
173
|
+
## Known issues
|
174
|
+
|
175
|
+
- When loading a YAML/JSON document that has an sequence –array– as the root item, SymbolMatrix will crash. This is because SymbolMatrix wasn't intended for arrays, just Hashes, and the recursive conversion from Hash to SymbolMatrix is not working with Arrays.
|
176
|
+
- Some keys overlap with method names, so can't be used with the dot syntax. For example, `key`. Just use them with the hash syntax instead :)
|
177
|
+
|
178
|
+
## Future
|
179
|
+
|
180
|
+
- Make SymbolMatrix raise a more relevant error when the file does not exist (something along the lines of "There was an error parsing the YAML string, may be it was a file path and the file does not exist?"). It is possible to increase the chances of detecting properly with a really simple regular expression...
|
181
|
+
|
182
|
+
## Testing
|
183
|
+
|
184
|
+
RSpec specs are provided to check functionality.
|
185
|
+
|
186
|
+
## License
|
187
|
+
|
188
|
+
Copyright (C) 2012 Fetcher
|
189
|
+
|
190
|
+
This program is free software: you can redistribute it and/or modify
|
191
|
+
it under the terms of the GNU General Public License as published by
|
192
|
+
the Free Software Foundation, either version 3 of the License, or
|
193
|
+
(at your option) any later version.
|
194
|
+
|
195
|
+
This program is distributed in the hope that it will be useful,
|
196
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
197
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
198
|
+
GNU General Public License for more details.
|
199
|
+
|
200
|
+
You should have received a copy of the GNU General Public License
|
201
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Feature: Parsing a SymbolMatrix serialization
|
2
|
+
In order to be able to build multidimensional hashes from
|
3
|
+
single line contexts, such as terminals
|
4
|
+
As a developer
|
5
|
+
I want to have a simple way to parse a serialization
|
6
|
+
|
7
|
+
Scenario: Quite basic structure
|
8
|
+
Given "data:datum"
|
9
|
+
When I parse it
|
10
|
+
Then I should see (serialized in yaml)
|
11
|
+
"""
|
12
|
+
data: datum
|
13
|
+
"""
|
14
|
+
|
15
|
+
Scenario: Needlessly complicated
|
16
|
+
Given "remote.client.host:fetcher remote.client.path:/ remote.client.port:bridge.fetcher remote.server:8080"
|
17
|
+
When I parse it
|
18
|
+
Then I should see (serialized in yaml)
|
19
|
+
"""
|
20
|
+
remote:
|
21
|
+
client:
|
22
|
+
host: fetcher
|
23
|
+
path: /
|
24
|
+
port: bridge.fetcher
|
25
|
+
server: 8080
|
26
|
+
"""
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Feature: Print to YAML
|
2
|
+
I want to be able to print a SymbolMatrix into YAML
|
3
|
+
with a simple Hash format
|
4
|
+
|
5
|
+
Scenario: A simple SymbolMatrix
|
6
|
+
Given the SymbolMatrix:
|
7
|
+
"""
|
8
|
+
a:
|
9
|
+
simple: symbolmatrix
|
10
|
+
with: data
|
11
|
+
"""
|
12
|
+
When I write it to YAML
|
13
|
+
Then I should have
|
14
|
+
"""
|
15
|
+
---
|
16
|
+
a:
|
17
|
+
simple: symbolmatrix
|
18
|
+
with: data
|
19
|
+
|
20
|
+
"""
|
@@ -0,0 +1,43 @@
|
|
1
|
+
Feature: Serializing a SymbolMatrix
|
2
|
+
In order to be able to send SymbolMatrices
|
3
|
+
in command line environments
|
4
|
+
As a developer
|
5
|
+
I want to be able to serialize SymbolMatrices
|
6
|
+
|
7
|
+
Scenario: Nice SymbolMatrix serialized
|
8
|
+
Given the SymbolMatrix:
|
9
|
+
"""
|
10
|
+
roses:
|
11
|
+
are: red
|
12
|
+
violets:
|
13
|
+
are: blue
|
14
|
+
I:
|
15
|
+
get: joke
|
16
|
+
like: poetry
|
17
|
+
say:
|
18
|
+
to: you
|
19
|
+
some: stuff
|
20
|
+
"""
|
21
|
+
When I serialize it
|
22
|
+
Then I should get "roses.are:red violets.are:blue I.get:joke I.like:poetry I.say.to:you I.say.some:stuff"
|
23
|
+
|
24
|
+
Scenario: Serializing and parsing back
|
25
|
+
Given the SymbolMatrix:
|
26
|
+
"""
|
27
|
+
greetings:
|
28
|
+
aloha: hawaii
|
29
|
+
cities:
|
30
|
+
roma: italy
|
31
|
+
paris: france
|
32
|
+
"""
|
33
|
+
When I serialize it
|
34
|
+
Then I should get "greetings.aloha:hawaii cities.roma:italy cities.paris:france"
|
35
|
+
When I parse it
|
36
|
+
Then I should see (serialized in yaml)
|
37
|
+
"""
|
38
|
+
greetings:
|
39
|
+
aloha: hawaii
|
40
|
+
cities:
|
41
|
+
roma: italy
|
42
|
+
paris: france
|
43
|
+
"""
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Given /^"(.+?)"$/ do |serialization|
|
2
|
+
@serialization = serialization
|
3
|
+
end
|
4
|
+
|
5
|
+
When /^I parse it$/ do
|
6
|
+
@parsed = SymbolMatrix.new @serialization
|
7
|
+
end
|
8
|
+
|
9
|
+
Then /^I should see \(serialized in yaml\)$/ do |data|
|
10
|
+
@parsed.to.hash.should include SymbolMatrix.new(data).to.hash
|
11
|
+
end
|