lono 1.1.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitmodules +3 -0
- data/CHANGELOG.md +8 -0
- data/README.md +150 -39
- data/bin/lono +2 -2
- data/circle.yml +4 -0
- data/lib/lono.rb +16 -7
- data/lib/lono/cfn.rb +64 -0
- data/lib/lono/cfn/aws_services.rb +37 -0
- data/lib/lono/cfn/base.rb +144 -0
- data/lib/lono/cfn/create.rb +34 -0
- data/lib/lono/cfn/delete.rb +26 -0
- data/lib/lono/cfn/diff.rb +43 -0
- data/lib/lono/cfn/help.rb +93 -0
- data/lib/lono/cfn/preview.rb +133 -0
- data/lib/lono/cfn/update.rb +62 -0
- data/lib/lono/cfn/util.rb +21 -0
- data/lib/lono/cli.rb +19 -10
- data/lib/lono/command.rb +25 -0
- data/lib/lono/help.rb +59 -0
- data/lib/lono/new.rb +3 -2
- data/lib/lono/param.rb +20 -0
- data/lib/lono/param/generator.rb +90 -0
- data/lib/lono/param/help.rb +15 -0
- data/lib/lono/project_checker.rb +44 -0
- data/lib/lono/template.rb +22 -248
- data/lib/lono/template/bashify.rb +39 -0
- data/lib/lono/template/dsl.rb +139 -0
- data/lib/lono/template/help.rb +25 -0
- data/lib/lono/template/template.rb +251 -0
- data/lib/lono/version.rb +1 -1
- data/lib/{starter_project_yaml → starter_projects/json_project}/Gemfile +0 -1
- data/lib/{starter_project_json → starter_projects/json_project}/Guardfile +0 -0
- data/lib/{starter_project_json → starter_projects/json_project}/config/lono.rb +0 -0
- data/lib/{starter_project_json → starter_projects/json_project}/config/lono/api.rb +0 -0
- data/lib/starter_projects/json_project/params/api-web-prod.txt +20 -0
- data/lib/{starter_project_json → starter_projects/json_project}/templates/db.json.erb +0 -0
- data/lib/{starter_project_json → starter_projects/json_project}/templates/partial/host_record.json.erb +0 -0
- data/lib/{starter_project_json → starter_projects/json_project}/templates/partial/server.json.erb +0 -0
- data/lib/{starter_project_json → starter_projects/json_project}/templates/user_data/app.sh.erb +0 -0
- data/lib/{starter_project_json → starter_projects/json_project}/templates/user_data/db.sh.erb +0 -0
- data/lib/{starter_project_json → starter_projects/json_project}/templates/user_data/db2.sh.erb +0 -0
- data/lib/{starter_project_json → starter_projects/json_project}/templates/user_data/ruby_script.rb.erb +0 -0
- data/lib/{starter_project_json → starter_projects/json_project}/templates/web.json.erb +0 -0
- data/lib/{starter_project_json → starter_projects/yaml_project}/Gemfile +0 -1
- data/lib/{starter_project_yaml → starter_projects/yaml_project}/Guardfile +0 -0
- data/lib/{starter_project_yaml → starter_projects/yaml_project}/config/lono.rb +0 -0
- data/lib/{starter_project_yaml → starter_projects/yaml_project}/config/lono/api.rb +0 -0
- data/lib/starter_projects/yaml_project/params/api-web-prod.txt +20 -0
- data/lib/{starter_project_yaml → starter_projects/yaml_project}/templates/db.yml.erb +0 -0
- data/lib/{starter_project_yaml → starter_projects/yaml_project}/templates/partial/host_record.yml.erb +0 -0
- data/lib/{starter_project_yaml → starter_projects/yaml_project}/templates/partial/server.yml.erb +0 -0
- data/lib/{starter_project_yaml → starter_projects/yaml_project}/templates/partial/user_data/bootstrap.sh.erb +0 -0
- data/lib/{starter_project_yaml → starter_projects/yaml_project}/templates/web.yml.erb +0 -0
- data/lono.gemspec +15 -10
- data/spec/fixtures/my_project/config/lono.rb +1 -0
- data/spec/fixtures/my_project/params/my-stack.txt +3 -0
- data/spec/fixtures/my_project/templates/.gitkeep +0 -0
- data/spec/fixtures/my_project/templates/my-stack.yml.erb +0 -0
- data/spec/lib/lono/cfn_spec.rb +35 -0
- data/spec/lib/lono/new_spec.rb +3 -3
- data/spec/lib/lono/param_spec.rb +15 -0
- data/spec/lib/lono/{dsl_spec.rb → template/dsl_spec.rb} +9 -9
- data/spec/lib/lono/template/template_spec.rb +104 -0
- data/spec/lib/lono/template_spec.rb +22 -37
- data/spec/lib/lono_spec.rb +6 -83
- data/vendor/plissken/Gemfile +14 -0
- data/vendor/plissken/LICENSE.txt +20 -0
- data/vendor/plissken/README.md +46 -0
- data/vendor/plissken/Rakefile +56 -0
- data/vendor/plissken/VERSION +1 -0
- data/vendor/plissken/lib/plissken.rb +1 -0
- data/vendor/plissken/lib/plissken/ext/hash/to_snake_keys.rb +45 -0
- data/vendor/plissken/plissken.gemspec +61 -0
- data/vendor/plissken/spec/lib/to_snake_keys_spec.rb +177 -0
- data/vendor/plissken/spec/spec_helper.rb +90 -0
- data/vendor/plissken/test/helper.rb +20 -0
- data/vendor/plissken/test/plissken/ext/hash/to_snake_keys_test.rb +184 -0
- data/vendor/plissken/test/test_plissken.rb +2 -0
- metadata +115 -39
- data/lib/lono/bashify.rb +0 -41
- data/lib/lono/cli/help.rb +0 -37
- data/lib/lono/dsl.rb +0 -132
data/spec/lib/lono_spec.rb
CHANGED
@@ -1,88 +1,11 @@
|
|
1
|
-
|
1
|
+
require_relative "../spec_helper"
|
2
2
|
|
3
3
|
describe Lono do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
name = File.basename(@project_root)
|
9
|
-
FileUtils.mkdir(dir) unless File.exist?(dir)
|
10
|
-
execute("cd #{dir} && #{lono_bin} new #{name} -f -q --format json")
|
11
|
-
end
|
12
|
-
|
13
|
-
after(:each) do
|
14
|
-
FileUtils.rm_rf(@project_root) unless ENV['KEEP_TMP_PROJECT']
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "bashify" do
|
18
|
-
it "should convert cfn user_data to bash script" do
|
19
|
-
path = "#{$root}/spec/fixtures/cfn.json"
|
20
|
-
out = execute("./bin/lono bashify #{path}")
|
21
|
-
expect(out).to match /bash -lexv/
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "parsing" do
|
26
|
-
it "should transform bash script into json array with cloudformation objects" do
|
27
|
-
block = Proc.new { }
|
28
|
-
template = Lono::Template.new("foo", block)
|
29
|
-
|
30
|
-
line = '0{2345}7'
|
31
|
-
expect(template.bracket_positions(line)).to eq [[0,0],[1,6],[7,7]]
|
32
|
-
line = '0{2}4{6}' # more than one bracket
|
33
|
-
expect(template.bracket_positions(line)).to eq [[0,0],[1,3],[4,4],[5,7]]
|
34
|
-
line = '0{2}4{6{8}0}2' # nested brackets
|
35
|
-
expect(template.bracket_positions(line)).to eq [[0,0],[1,3],[4,4],[5,11],[12,12]]
|
36
|
-
|
37
|
-
line = '{'
|
38
|
-
expect(template.decompose(line)).to eq ['{']
|
39
|
-
|
40
|
-
##########################
|
41
|
-
line = '1{"Ref"=>"A"}{"Ref"=>"B"}'
|
42
|
-
expect(template.decompose(line)).to eq ['1','{"Ref"=>"A"}','{"Ref"=>"B"}']
|
43
|
-
|
44
|
-
line = '{"Ref"=>"A"}{"Ref"=>"B"}2'
|
45
|
-
expect(template.decompose(line)).to eq ['{"Ref"=>"A"}','{"Ref"=>"B"}','2']
|
46
|
-
|
47
|
-
line = '1{"Ref"=>"A"}{"Ref"=>"B"}2'
|
48
|
-
expect(template.decompose(line)).to eq ['1','{"Ref"=>"A"}','{"Ref"=>"B"}','2']
|
49
|
-
|
50
|
-
line = '{"Ref"=>"A"}{"Ref"=>"B"}'
|
51
|
-
expect(template.decompose(line)).to eq ['{"Ref"=>"A"}','{"Ref"=>"B"}']
|
52
|
-
|
53
|
-
line = 'Ref{"Ref"=>"B"}'
|
54
|
-
expect(template.decompose(line)).to eq ['Ref','{"Ref"=>"B"}']
|
55
|
-
##############################
|
56
|
-
|
57
|
-
# only allow whitelist
|
58
|
-
line = 'a{b}{"foo"=>"bar"}h'
|
59
|
-
expect(template.decompose(line)).to eq ['a{b}{"foo"=>"bar"}h']
|
60
|
-
|
61
|
-
line = 'a{b}{"Ref"=>"bar"}h'
|
62
|
-
expect(template.decompose(line)).to eq ['a{b}','{"Ref"=>"bar"}','h']
|
63
|
-
line = 'a{"Ref"=>"bar"}c{"Ref"=>{"cat"=>"mouse"}}e' # nested brackets
|
64
|
-
expect(template.decompose(line)).to eq ['a','{"Ref"=>"bar"}','c','{"Ref"=>{"cat"=>"mouse"}}','e']
|
65
|
-
|
66
|
-
line = 'test{"Ref"=>"world"}me' # nested brackets
|
67
|
-
decomposition = template.decompose(line)
|
68
|
-
result = template.recompose(decomposition)
|
69
|
-
expect(result).to eq ["test", {"Ref" => "world"}, "me"]
|
70
|
-
|
71
|
-
line = 'test{"Ref"=>"world"}me'
|
72
|
-
expect(template.transform(line)).to eq ["test", {"Ref" => "world"}, "me\n"]
|
73
|
-
line = '{"Ref"=>"world"}'
|
74
|
-
expect(template.transform(line)).to eq [{"Ref" => "world"}, "\n"]
|
75
|
-
line = '{'
|
76
|
-
expect(template.transform(line)).to eq ["{\n"]
|
77
|
-
line = 'Ref{"Ref"=>"B"}'
|
78
|
-
expect(template.transform(line)).to eq ['Ref',{"Ref"=>"B"}, "\n"]
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
describe "cli specs" do
|
83
|
-
it "should generate templates" do
|
84
|
-
out = execute("./bin/lono generate -c --project-root #{@project_root}")
|
85
|
-
expect(out).to match /Generating CloudFormation templates/
|
4
|
+
describe "lono" do
|
5
|
+
it "generate should build templates" do
|
6
|
+
out = execute("./bin/lono generate --project-root spec/fixtures/my_project")
|
7
|
+
expect(out).to match /Generating both CloudFormation template and parameter/
|
86
8
|
end
|
87
9
|
end
|
88
10
|
end
|
11
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
# gem 'debugger'
|
10
|
+
gem 'rspec', '~> 3.1'
|
11
|
+
gem 'rdoc', '~> 4.2'
|
12
|
+
gem 'bundler', '~> 1.6'
|
13
|
+
gem 'jeweler', '~> 2.3'
|
14
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Dave Hrycyszyn
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,46 @@
|
|
1
|
+
=======
|
2
|
+
plissken
|
3
|
+
========
|
4
|
+
|
5
|
+
Have you ever needed to automatically convert JSON-style `camelBack` or `CamelCase` hash keys into more Rubyish `snake_case`?
|
6
|
+
|
7
|
+
Plissken to the rescue.
|
8
|
+
|
9
|
+
This gem recursively converts all camelBack or CamelCase keys in a hash structure to snake_case.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'plissken'
|
15
|
+
my_hash = {"firstKey" => 1, "fooBars" => [{"bazBaz" => "value"}, {"blahBlah" => "value"}]}
|
16
|
+
snaked_hash = my_hash.to_snake_keys
|
17
|
+
# => {"first_key" => 1, "foo_bars" => [{"baz_baz" => "value"}, {"blah_blah" => "value"}]}
|
18
|
+
```
|
19
|
+
|
20
|
+
Plissken works on either string keys or symbolized keys. It has no dependencies, as it has its own `underscore` method lifted out of ActiveSupport.
|
21
|
+
|
22
|
+
## Limitations
|
23
|
+
|
24
|
+
* Your keys must be camelBack or CamelCase. The key "Foo Bar" will output as "foo bar".
|
25
|
+
* Unlike the original Snake Plissken in the seminal film [Escape from New York](http://en.wikipedia.org/wiki/Escape_from_New_York), the plissken gem is non-destructive. There is no `Hash#to_snake_keys!` form.
|
26
|
+
|
27
|
+
# Going the other way
|
28
|
+
|
29
|
+
If you've already got `snake_case` and need to `CamelCase` it, you are encouraged to try
|
30
|
+
the [Awrence](http://github.com/futurechimp/awrence) gem.
|
31
|
+
|
32
|
+
## Contributing to plissken
|
33
|
+
|
34
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
35
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
36
|
+
* Fork the project.
|
37
|
+
* Start a feature/bugfix branch.
|
38
|
+
* Commit and push until you are happy with your contribution.
|
39
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
40
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
41
|
+
|
42
|
+
== Copyright
|
43
|
+
|
44
|
+
Copyright (c) 2013 Dave Hrycyszyn. See LICENSE.txt for
|
45
|
+
further details.
|
46
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
RSpec::Core::RakeTask.new('spec')
|
7
|
+
|
8
|
+
begin
|
9
|
+
Bundler.setup(:default, :development)
|
10
|
+
rescue Bundler::BundlerError => e
|
11
|
+
$stderr.puts e.message
|
12
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
13
|
+
exit e.status_code
|
14
|
+
end
|
15
|
+
require 'rake'
|
16
|
+
|
17
|
+
require 'jeweler'
|
18
|
+
Jeweler::Tasks.new do |gem|
|
19
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
20
|
+
gem.name = "plissken"
|
21
|
+
gem.homepage = "http://github.com/futurechimp/plissken"
|
22
|
+
gem.license = "MIT"
|
23
|
+
gem.summary = %Q{Snakify your camel keys when working with JSON APIs}
|
24
|
+
gem.description = %Q{
|
25
|
+
Have you ever needed to automatically convert JSON-style camelBack or
|
26
|
+
CamelCase hash keys into more Rubyish snake_case?
|
27
|
+
|
28
|
+
Plissken to the rescue.
|
29
|
+
|
30
|
+
This gem recursively converts all camelBack or CamelCase keys in a hash
|
31
|
+
structure to snake_case.
|
32
|
+
}
|
33
|
+
gem.email = "dave.hrycyszyn@headlondon.com"
|
34
|
+
gem.authors = ["Dave Hrycyszyn", "Michael Chrisco"]
|
35
|
+
# dependencies defined in Gemfile
|
36
|
+
end
|
37
|
+
Jeweler::RubygemsDotOrgTasks.new
|
38
|
+
|
39
|
+
require 'rake/testtask'
|
40
|
+
Rake::TestTask.new(:test) do |test|
|
41
|
+
test.libs << 'lib' << 'test'
|
42
|
+
test.pattern = 'test/**/test_*.rb'
|
43
|
+
test.verbose = true
|
44
|
+
end
|
45
|
+
|
46
|
+
task :default => :spec
|
47
|
+
|
48
|
+
require 'rdoc/task'
|
49
|
+
Rake::RDocTask.new do |rdoc|
|
50
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "plissken #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
0.3.0
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/plissken/ext/hash/to_snake_keys'
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# Hash.to_snake_keys
|
2
|
+
class Hash
|
3
|
+
# Recursively converts CamelCase and camelBack JSON-style hash keys to
|
4
|
+
# Rubyish snake_case, suitable for use during instantiation of Ruby
|
5
|
+
# model attributes.
|
6
|
+
#
|
7
|
+
def to_snake_keys(value = self)
|
8
|
+
case value
|
9
|
+
when Array
|
10
|
+
value.map { |v| to_snake_keys(v) }
|
11
|
+
when Hash
|
12
|
+
snake_hash(value)
|
13
|
+
else
|
14
|
+
value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def snake_hash(value)
|
21
|
+
Hash[value.map { |k, v| [underscore_key(k).to_sym, to_snake_keys(v)] }]
|
22
|
+
end
|
23
|
+
|
24
|
+
def underscore_key(k)
|
25
|
+
if k.is_a? Symbol
|
26
|
+
underscore(k.to_s).to_sym
|
27
|
+
elsif k.is_a? String
|
28
|
+
underscore(k)
|
29
|
+
else
|
30
|
+
k # Plissken can't snakify anything except strings and symbols
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def underscore(string)
|
35
|
+
@__memoize_underscore ||= {}
|
36
|
+
return @__memoize_underscore[string] if @__memoize_underscore[string]
|
37
|
+
@__memoize_underscore[string] =
|
38
|
+
string.gsub(/::/, '/')
|
39
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
40
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
41
|
+
.tr('-', '_')
|
42
|
+
.downcase
|
43
|
+
@__memoize_underscore[string]
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: plissken 0.3.0 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "plissken"
|
9
|
+
s.version = "0.3.0"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib"]
|
13
|
+
s.authors = ["Dave Hrycyszyn", "Michael Chrisco"]
|
14
|
+
s.date = "2017-01-13"
|
15
|
+
s.description = "\n Have you ever needed to automatically convert JSON-style camelBack or\n CamelCase hash keys into more Rubyish snake_case?\n\n Plissken to the rescue.\n\n This gem recursively converts all camelBack or CamelCase keys in a hash\n structure to snake_case.\n "
|
16
|
+
s.email = "dave.hrycyszyn@headlondon.com"
|
17
|
+
s.extra_rdoc_files = [
|
18
|
+
"LICENSE.txt",
|
19
|
+
"README.md"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
".document",
|
23
|
+
".rspec",
|
24
|
+
".rubocop.yml",
|
25
|
+
"Gemfile",
|
26
|
+
"Gemfile.lock",
|
27
|
+
"LICENSE.txt",
|
28
|
+
"README.md",
|
29
|
+
"Rakefile",
|
30
|
+
"VERSION",
|
31
|
+
"lib/plissken.rb",
|
32
|
+
"lib/plissken/ext/hash/to_snake_keys.rb",
|
33
|
+
"plissken.gemspec",
|
34
|
+
"spec/lib/to_snake_keys_spec.rb",
|
35
|
+
"spec/spec_helper.rb",
|
36
|
+
"test/helper.rb",
|
37
|
+
"test/plissken/ext/hash/to_snake_keys_test.rb",
|
38
|
+
"test/test_plissken.rb"
|
39
|
+
]
|
40
|
+
s.homepage = "http://github.com/futurechimp/plissken"
|
41
|
+
s.licenses = ["MIT"]
|
42
|
+
s.rubygems_version = "2.5.1"
|
43
|
+
s.summary = "Snakify your camel keys when working with JSON APIs"
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
s.specification_version = 4
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_development_dependency(%q<rspec>, ["~> 3.1"])
|
50
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.6"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<rspec>, ["~> 3.1"])
|
53
|
+
s.add_dependency(%q<rdoc>, ["~> 4.2"])
|
54
|
+
s.add_dependency(%q<bundler>, ["~> 1.6"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rspec>, ["~> 3.1"])
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.6"])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../lib/plissken')
|
4
|
+
|
5
|
+
RSpec.describe 'A Hash' do
|
6
|
+
describe 'with camelBack keys' do
|
7
|
+
describe 'which are JSON-style strings' do
|
8
|
+
describe 'in the simplest case' do
|
9
|
+
let(:hash) { [{ 'firstKey' => 'fooBar' }] }
|
10
|
+
|
11
|
+
describe 'non-destructive snakification' do
|
12
|
+
let(:snaked) { hash.map(&:to_snake_keys) }
|
13
|
+
it 'snakifies the key' do
|
14
|
+
expect(snaked.first).to have_key(:first_key)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'leaves the key as a string' do
|
18
|
+
expect(snaked.first.keys).to eq([:first_key])
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'leaves the value untouched' do
|
22
|
+
expect(hash.first.values).to eq(['fooBar'])
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'leaves the original hash untouched' do
|
26
|
+
expect(hash.first.keys).to eq(['firstKey'])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'containing an array of other hashes' do
|
32
|
+
let(:hash) do
|
33
|
+
{
|
34
|
+
'appleType' => 'Granny Smith',
|
35
|
+
'vegetableTypes' => [
|
36
|
+
{ 'potatoType' => 'Golden delicious' },
|
37
|
+
{ 'otherTuberType' => 'peanut' },
|
38
|
+
{ 'peanutNamesAndSpouses' => [
|
39
|
+
{ 'billThePeanut' => 'sallyPeanut' },
|
40
|
+
{ 'sammyThePeanut' => 'jillPeanut' }
|
41
|
+
] }
|
42
|
+
] }
|
43
|
+
end
|
44
|
+
|
45
|
+
describe 'non-destructive snakification' do
|
46
|
+
subject(:snaked) { hash.to_snake_keys }
|
47
|
+
#
|
48
|
+
it 'recursively snakifies the keys on the top level of the hash' do
|
49
|
+
expect(snaked.include?(:apple_type)).to be_truthy
|
50
|
+
expect(snaked.include?(:vegetable_types)).to be_truthy
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'leaves the values on the top level alone' do
|
54
|
+
expect(snaked[:apple_type]).to eq('Granny Smith')
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'converts second-level keys' do
|
58
|
+
expect(snaked[:vegetable_types].first)
|
59
|
+
.to have_key(:potato_type)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'leaves second-level values alone' do
|
63
|
+
expect(snaked[:vegetable_types].first)
|
64
|
+
.to have_value('Golden delicious')
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'converts third-level keys' do
|
68
|
+
expect(snaked[:vegetable_types].last[:peanut_names_and_spouses]
|
69
|
+
.last).to have_value('jillPeanut')
|
70
|
+
expect(snaked[:vegetable_types]
|
71
|
+
.last[:peanut_names_and_spouses]
|
72
|
+
.last).to have_key(:sammy_the_peanut)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'leaves third-level values alone' do
|
76
|
+
expect(snaked[:vegetable_types]
|
77
|
+
.last[:peanut_names_and_spouses]
|
78
|
+
.first[:bill_the_peanut]).to eq('sallyPeanut')
|
79
|
+
expect(snaked[:vegetable_types]
|
80
|
+
.last[:peanut_names_and_spouses]
|
81
|
+
.last[:sammy_the_peanut]).to eq('jillPeanut')
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe 'which are symbols' do
|
88
|
+
describe 'in the simplest case' do
|
89
|
+
let(:hash) { { :firstKey => 'fooBar' } }
|
90
|
+
|
91
|
+
describe 'non-destructive snakification' do
|
92
|
+
subject(:snaked) { hash.to_snake_keys }
|
93
|
+
|
94
|
+
it 'snakifies the key' do
|
95
|
+
expect(snaked).to have_key(:first_key)
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'leaves the key as a symbol' do
|
99
|
+
expect(:first_key).to eq(snaked.keys.first)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'leaves the value untouched' do
|
103
|
+
expect(snaked.values.first).to eq('fooBar')
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'leaves the original hash untouched' do
|
107
|
+
expect(hash).to have_key(:firstKey)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
#
|
112
|
+
describe 'containing an array of other hashes' do
|
113
|
+
let(:hash) do
|
114
|
+
{
|
115
|
+
:appleType => 'Granny Smith',
|
116
|
+
:vegetableTypes => [
|
117
|
+
{ :potatoType => 'Golden delicious' },
|
118
|
+
{ :otherTuberType => 'peanut' },
|
119
|
+
{ :peanutNamesAndSpouses => [
|
120
|
+
{ :billThePeanut => 'sallyPeanut' },
|
121
|
+
{ :sammyThePeanut => 'jillPeanut' }
|
122
|
+
] }
|
123
|
+
] }
|
124
|
+
end
|
125
|
+
#
|
126
|
+
describe 'non-destructive snakification' do
|
127
|
+
subject(:snaked) { hash.to_snake_keys }
|
128
|
+
#
|
129
|
+
it 'recursively snakifies the keys on the top level of the hash' do
|
130
|
+
expect(snaked.include?(:apple_type)).to be_truthy
|
131
|
+
expect(snaked.include?(:vegetable_types)).to be_truthy
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'leaves the values on the top level alone' do
|
135
|
+
expect(snaked[:apple_type]).to eq('Granny Smith')
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'converts second-level keys' do
|
139
|
+
expect(snaked[:vegetable_types].first)
|
140
|
+
.to have_key(:potato_type)
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'leaves second-level values alone' do
|
144
|
+
expect(snaked[:vegetable_types].first)
|
145
|
+
.to have_value('Golden delicious')
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'converts third-level keys' do
|
149
|
+
expect(snaked[:vegetable_types].last[:peanut_names_and_spouses]
|
150
|
+
.last).to have_value('jillPeanut')
|
151
|
+
expect(snaked[:vegetable_types]
|
152
|
+
.last[:peanut_names_and_spouses]
|
153
|
+
.last).to have_key(:sammy_the_peanut)
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'leaves third-level values alone' do
|
157
|
+
expect(snaked[:vegetable_types]
|
158
|
+
.last[:peanut_names_and_spouses]
|
159
|
+
.first[:bill_the_peanut]).to eq('sallyPeanut')
|
160
|
+
expect(snaked[:vegetable_types]
|
161
|
+
.last[:peanut_names_and_spouses]
|
162
|
+
.last[:sammy_the_peanut]).to eq('jillPeanut')
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
describe 'strings with spaces in them' do
|
170
|
+
let(:hash) { { 'With Spaces' => 'FooBar' } }
|
171
|
+
subject { hash.to_snake_keys }
|
172
|
+
|
173
|
+
it "doesn't get snaked, although it does get downcased" do
|
174
|
+
is_expected.to have_key(:'with spaces')
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|