hash_generator 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +2 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +71 -0
- data/Rakefile +7 -0
- data/hash_generator.gemspec +20 -0
- data/lib/hash_generator.rb +57 -0
- data/spec/hash_generator_spec.rb +50 -0
- data/spec/spec_helper.rb +87 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 11486c609a6d854ccfc88383ba41b6f67fffa149
|
4
|
+
data.tar.gz: 0fbf0032512abf232454e4fe9d52c4309e6097c8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c1190f1e92a7abb3a2b0f468094f98860f1d02842098231b23e1002271b1d5e839724ff97b8de17a054b154ccd26f03e48b58d04a054da44ea982166d1962969
|
7
|
+
data.tar.gz: 8fedf12e04e6e589cb4d15da287189ffc702088a0ea5eb63759eb43397c4a4ec0382306b5b6074a7ee71ee95fb2c469618df862da07edfd34df795fc6d742160
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Dray Lacy
|
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,71 @@
|
|
1
|
+
# HashGenerator
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/IvyApp/hash_generator.svg?branch=master)](https://travis-ci.org/IvyApp/hash_generator)
|
4
|
+
|
5
|
+
Simple programmatic building of hashes.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'hash_generator'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
```sh
|
18
|
+
bundle
|
19
|
+
```
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
```sh
|
24
|
+
gem install hash_generator
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Use `#store` to store primitive values:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'hash_generator'
|
33
|
+
|
34
|
+
generator = HashGenerator.new
|
35
|
+
generator.store(:hello, 'world!')
|
36
|
+
generator.to_h # => {:hello=>"world!"}
|
37
|
+
```
|
38
|
+
|
39
|
+
Or use `#new_object` and `#store_scope` to nest hashes:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'hash_generator'
|
43
|
+
|
44
|
+
generator = HashGenerator.new
|
45
|
+
generator.new_object
|
46
|
+
generator.store(:hello, 'world!')
|
47
|
+
generator.store_scope(:object)
|
48
|
+
generator.to_h # => {:object=>{:hello=>"world!"}}
|
49
|
+
```
|
50
|
+
|
51
|
+
Or use `#new_array` and `#push_scope` to push objects into an array:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
require 'hash_generator'
|
55
|
+
|
56
|
+
generator = HashGenerator.new
|
57
|
+
generator.new_array
|
58
|
+
generator.new_object
|
59
|
+
generator.store(:hello, 'world!')
|
60
|
+
generator.push_scope
|
61
|
+
generator.store_scope(:array)
|
62
|
+
generator.to_h # => {:array=>[{:hello=>"world!"}]}
|
63
|
+
```
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
1. Fork it (https://github.com/IvyApp/hash\_generator/fork)
|
68
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
69
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
70
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
71
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path('../lib/hash_generator', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'hash_generator'
|
5
|
+
spec.version = HashGenerator::VERSION
|
6
|
+
spec.authors = ['Dray Lacy']
|
7
|
+
spec.email = ['dray@envylabs.com']
|
8
|
+
spec.summary = 'Simple programmatic building of hashes.'
|
9
|
+
spec.homepage = ''
|
10
|
+
spec.license = 'MIT'
|
11
|
+
|
12
|
+
spec.files = `git ls-files -z`.split("\x0")
|
13
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
14
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
15
|
+
spec.require_paths = ['lib']
|
16
|
+
|
17
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
18
|
+
spec.add_development_dependency 'rake'
|
19
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
20
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
class HashGenerator
|
2
|
+
VERSION = '1.0.0'
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@value = @scope = {}
|
6
|
+
@stack = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def end_scope
|
10
|
+
@scope = @stack.pop
|
11
|
+
end
|
12
|
+
|
13
|
+
def new_array
|
14
|
+
begin_scope([])
|
15
|
+
end
|
16
|
+
|
17
|
+
def new_object
|
18
|
+
begin_scope({})
|
19
|
+
end
|
20
|
+
|
21
|
+
def push_scope
|
22
|
+
value = @scope
|
23
|
+
end_scope
|
24
|
+
push(value)
|
25
|
+
end
|
26
|
+
|
27
|
+
def reopen_scope(key)
|
28
|
+
begin_scope(@scope.fetch(key))
|
29
|
+
end
|
30
|
+
|
31
|
+
def store(key, value)
|
32
|
+
@scope.store(key, value)
|
33
|
+
end
|
34
|
+
|
35
|
+
def store_scope(key)
|
36
|
+
value = @scope
|
37
|
+
end_scope
|
38
|
+
store(key, value)
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_h
|
42
|
+
@value
|
43
|
+
end
|
44
|
+
|
45
|
+
alias to_hash to_h
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def begin_scope(scope)
|
50
|
+
@stack.push(@scope)
|
51
|
+
@scope = scope
|
52
|
+
end
|
53
|
+
|
54
|
+
def push(value)
|
55
|
+
@scope.push(value)
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'hash_generator'
|
2
|
+
|
3
|
+
RSpec.describe HashGenerator do
|
4
|
+
let(:generator) { described_class.new }
|
5
|
+
|
6
|
+
subject { generator.to_h }
|
7
|
+
|
8
|
+
describe '#push_scope' do
|
9
|
+
it 'pushes an object into the outer scope' do
|
10
|
+
generator.new_array
|
11
|
+
generator.new_object
|
12
|
+
generator.store(:key, 1)
|
13
|
+
generator.push_scope
|
14
|
+
generator.store_scope(:array)
|
15
|
+
|
16
|
+
should eq(:array => [{:key => 1}])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#reopen_scope' do
|
21
|
+
it 'reopens the scope at the given key' do
|
22
|
+
generator.new_object
|
23
|
+
generator.store(:key1, 1)
|
24
|
+
generator.store_scope(:object)
|
25
|
+
generator.reopen_scope(:object)
|
26
|
+
generator.store(:key2, 2)
|
27
|
+
generator.end_scope
|
28
|
+
|
29
|
+
should eq(:object => {:key1 => 1, :key2 => 2})
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#store' do
|
34
|
+
it 'stores a value in the current scope at the given key' do
|
35
|
+
generator.store(:key, 1)
|
36
|
+
|
37
|
+
should eq(:key => 1)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#store_scope' do
|
42
|
+
it 'stores the current scope in the outer scope at the given key' do
|
43
|
+
generator.new_object
|
44
|
+
generator.store(:key, 1)
|
45
|
+
generator.store_scope(:object)
|
46
|
+
|
47
|
+
should eq(:object => {:key => 1})
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# These two settings work together to allow you to limit a spec run
|
44
|
+
# to individual examples or groups you care about by tagging them with
|
45
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
46
|
+
# get run.
|
47
|
+
config.filter_run :focus
|
48
|
+
config.run_all_when_everything_filtered = true
|
49
|
+
|
50
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
51
|
+
# recommended. For more details, see:
|
52
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
53
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
54
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
55
|
+
config.disable_monkey_patching!
|
56
|
+
|
57
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
58
|
+
# be too noisy due to issues in dependencies.
|
59
|
+
config.warnings = true
|
60
|
+
|
61
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
62
|
+
# file, and it's useful to allow more verbose output when running an
|
63
|
+
# individual spec file.
|
64
|
+
if config.files_to_run.one?
|
65
|
+
# Use the documentation formatter for detailed output,
|
66
|
+
# unless a formatter has already been configured
|
67
|
+
# (e.g. via a command-line flag).
|
68
|
+
config.default_formatter = 'doc'
|
69
|
+
end
|
70
|
+
|
71
|
+
# Print the 10 slowest examples and example groups at the
|
72
|
+
# end of the spec run, to help surface which specs are running
|
73
|
+
# particularly slow.
|
74
|
+
config.profile_examples = 10
|
75
|
+
|
76
|
+
# Run specs in random order to surface order dependencies. If you find an
|
77
|
+
# order dependency and want to debug it, you can fix the order by providing
|
78
|
+
# the seed, which is printed after each run.
|
79
|
+
# --seed 1234
|
80
|
+
config.order = :random
|
81
|
+
|
82
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
83
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
84
|
+
# test failures related to randomization by passing the same `--seed` value
|
85
|
+
# as the one that triggered the failure.
|
86
|
+
Kernel.srand config.seed
|
87
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dray Lacy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- dray@envylabs.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- hash_generator.gemspec
|
70
|
+
- lib/hash_generator.rb
|
71
|
+
- spec/hash_generator_spec.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
homepage: ''
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.4.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Simple programmatic building of hashes.
|
97
|
+
test_files:
|
98
|
+
- spec/hash_generator_spec.rb
|
99
|
+
- spec/spec_helper.rb
|