folder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d15b12223bc0da1ff62ea9159966cf6c73ad55ed
4
+ data.tar.gz: 8b429003b51a1e811e043dac79d0914d35b11243
5
+ SHA512:
6
+ metadata.gz: 2f2fe9957211c7c5a0950552d13c5725e5b2e36ebc4c7c94911f8b8ef36e86f0a8082c6d9a34ea30cc21dfb52f1ca42d3536dc8436fb4abc7314ced320b4db2d
7
+ data.tar.gz: 17f0eac243efc67f946a209841b93939d082618741b92763eee9ef9523d1ace8fd7d82ff4f3a7cd4f444a55e2d917ed987cd781783e9d459d6b6052d6ea8a653
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in folder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 tatat
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,70 @@
1
+ # Folder
2
+
3
+ String keys make directory structur
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'folder'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install folder
18
+
19
+ ## Usage
20
+
21
+ Like this
22
+
23
+ ```ruby
24
+ h = {'a.b.c' => 'd'}
25
+
26
+ p h.fold #=> {"a"=>{"b"=>{"c"=>"d"}}}
27
+ p h #=> {"a.b.c"=>"d"}
28
+ p h.fold! #=> {"a"=>{"b"=>{"c"=>"d"}}}
29
+ p h #=> {"a"=>{"b"=>{"c"=>"d"}}}
30
+
31
+ h = {'a/b/c' => 'd'}
32
+
33
+ p h.fold #=> {"a/b/c"=>"d"}
34
+ p h.fold('/') #=> {"a"=>{"b"=>{"c"=>"d"}}}
35
+ ```
36
+
37
+ ### Example (Ruby on Rails)
38
+
39
+ Controller
40
+
41
+ ```ruby
42
+ class ExampleController < ApplicationController
43
+
44
+ def create
45
+ render json: Example.create!(example_params)
46
+ end
47
+
48
+ protected
49
+
50
+ def example_params
51
+ params.fold.require(:example)
52
+ .permit(:nyan, :mew)
53
+ end
54
+
55
+ end
56
+ ```
57
+
58
+ Request
59
+
60
+ ```sh
61
+ curl http://localhost:3000/example.json -d example.nyan=neko -d example.mew=koneko
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.rspec_opts = ["-c", "-fs"]
6
+ end
7
+
8
+ task :default => :spec
data/folder.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'folder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "folder"
8
+ spec.version = Folder::VERSION
9
+ spec.authors = ["tatat"]
10
+ spec.email = ["ioiioioloo@gmail.com"]
11
+ spec.description = %q{string keys make directory structure}
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/tatat/folder"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,3 @@
1
+ module Folder
2
+ VERSION = "0.0.1"
3
+ end
data/lib/folder.rb ADDED
@@ -0,0 +1,37 @@
1
+ require "folder/version"
2
+
3
+ module Folder
4
+ DEFAULT_SEPARATOR = '.'.freeze
5
+
6
+ def fold!(separator = DEFAULT_SEPARATOR)
7
+ klass = self.class
8
+ temp = klass.new
9
+
10
+ each do |key, value|
11
+ next unless key.is_a?(String) and key.index(separator)
12
+
13
+ *keys, last = key.split(separator, -1)
14
+ current = temp
15
+
16
+ while keys.length != 0
17
+ k = keys.shift
18
+ current[k] = klass.new unless current[k].is_a?(klass)
19
+ current = current[k]
20
+ end
21
+
22
+ current[last] = value unless current[last].is_a?(klass)
23
+ delete key
24
+ end
25
+
26
+ merge! temp
27
+ end
28
+
29
+ def fold(separator = DEFAULT_SEPARATOR)
30
+ dup.fold! separator
31
+ end
32
+ end
33
+
34
+ class Hash
35
+ include Folder
36
+ end
37
+
@@ -0,0 +1,93 @@
1
+ require 'rubygems'
2
+ require 'folder'
3
+
4
+ class CustomHash < Hash; end
5
+
6
+ describe Hash, '#fold!' do
7
+
8
+ before do
9
+ @hash = {
10
+ 'nyan' => true,
11
+ 'n.yan' => true,
12
+ 'ny.an' => true,
13
+ 'nya.n' => true,
14
+ 'n.y.an' => true,
15
+ 'n.ya.n' => true,
16
+ 'ny.a.n' => true,
17
+ 'n.y.a.n' => true,
18
+ :nyan => false,
19
+ 1 => false,
20
+ 1.0 => false,
21
+ [] => false,
22
+ {} => false,
23
+ }
24
+ end
25
+
26
+ it 'should have correct keys and values' do
27
+ @hash.fold!
28
+
29
+ expect(@hash).to include(*%w(nyan nya ny n))
30
+ expect(@hash['n']).to include(*%w(yan ya y))
31
+ expect(@hash['n']['y']).to include(*%w(an a))
32
+ expect(@hash['n']['y']['a']).to include('n' => true)
33
+ expect(@hash['n']['ya']).to include('n' => true)
34
+ expect(@hash['n']['yan']).to be_true
35
+ expect(@hash['ny']).to include(*%w(an a))
36
+ expect(@hash['ny']['a']).to include('n' => true)
37
+ expect(@hash['ny']['an']).to be_true
38
+ expect(@hash['nya']).to include('n' => true)
39
+ expect(@hash['nyan']).to be_true
40
+
41
+ expect(@hash[:nyan]).to be_instance_of(FalseClass)
42
+ expect(@hash[1]).to be_instance_of(FalseClass)
43
+ expect(@hash[1.0]).to be_instance_of(FalseClass)
44
+ expect(@hash[[]]).to be_instance_of(FalseClass)
45
+ expect(@hash[{}]).to be_instance_of(FalseClass)
46
+ end
47
+
48
+ it 'should return self' do
49
+ expect(@hash.fold!).to be(@hash)
50
+ end
51
+
52
+ it 'should be fold specified separator' do
53
+ hash = {
54
+ 'nyan' => true,
55
+ 'ny/an' => true,
56
+ }.fold!('/')
57
+
58
+ expect(hash['nyan']).to be_true
59
+ expect(hash['ny']).to include('an' => true)
60
+ end
61
+
62
+ it 'should override duplicate key' do
63
+ @hash.merge!('nya' => true).fold!
64
+
65
+ expect(@hash['nya']).not_to be_instance_of(TrueClass)
66
+ expect(@hash['nya']).to include('n' => true)
67
+ end
68
+ end
69
+
70
+ describe Hash, '#fold' do
71
+ before :all do
72
+ @hash = {}
73
+ end
74
+
75
+ it 'should not return self' do
76
+ expect(@hash.fold).not_to be(@hash)
77
+ end
78
+ end
79
+
80
+ describe CustomHash, '#fold!' do
81
+ before do
82
+ @hash = CustomHash['n.y.a.n' => true]
83
+ end
84
+
85
+ it 'should fold with instance of CustomHash' do
86
+ @hash.fold!
87
+
88
+ expect(@hash).to be_instance_of(CustomHash)
89
+ expect(@hash['n']).to be_instance_of(CustomHash)
90
+ expect(@hash['n']['y']).to be_instance_of(CustomHash)
91
+ expect(@hash['n']['y']['a']).to be_instance_of(CustomHash)
92
+ end
93
+ end
File without changes
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: folder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - tatat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-22 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: string keys make directory structure
56
+ email:
57
+ - ioiioioloo@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - folder.gemspec
68
+ - lib/folder.rb
69
+ - lib/folder/version.rb
70
+ - spec/folder_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: https://github.com/tatat/folder
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.0
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: string keys make directory structure
96
+ test_files:
97
+ - spec/folder_spec.rb
98
+ - spec/spec_helper.rb