url_plumber 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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 url_plumber.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Raphael Randschau
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,55 @@
1
+ # UrlPlumber
2
+
3
+ Ever had a table with multiple filter-options and the need to change one filter while keeping another active?
4
+
5
+ Using `UrlPlumber` you can change individual parts of your url parameters while not touching anything else.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'url_plumber'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install url_plumber
20
+
21
+ ## Usage
22
+
23
+ It's best to add a helper to your application to use `UrlPlumber` like this:
24
+
25
+ ```
26
+ # url_plumber.rb
27
+ module PlumberHelper
28
+ def plumb(key_path, value = nil)
29
+ return url_for (@plumber ||= ::UrlPlumber::Plumber.new(params)).plumb(key_path, value)
30
+ end
31
+ end
32
+ ```
33
+
34
+ Then, in your view, you can use it like this:
35
+
36
+ ```
37
+ # some_view.haml
38
+ = link_to 'Clear Option A', plumb('filter.option_a')
39
+ = link_to 'Option A 1', plumb('filter.option_a', 1)
40
+ = link_to 'Option A 2', plumb('filter.option_a', 2)
41
+
42
+ = link_to 'Clear Option B', plumb('filter.option_b')
43
+ = link_to 'Option B 1', plumb('filter.option_b', 1)
44
+ = link_to 'Option B 2', plumb('filter.option_b', 2)
45
+ ```
46
+
47
+ Mix and match your plumber got you covert :)
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler.require
5
+
6
+ Rake::TestTask.new do |test|
7
+ if ::RUBY_VERSION < '1.9'
8
+ test.test_files = %w(test/url_plumber_test.rb)
9
+ else
10
+ test.test_files = FileList['test/*_test.rb']
11
+ end
12
+ # test.verbose = true
13
+ end
14
+
15
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ require "url_plumber/version"
2
+
3
+ module UrlPlumber
4
+ autoload :Plumber, 'url_plumber/plumber'
5
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails'
2
+
3
+ module Flexirails
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,51 @@
1
+ require 'active_support/hash_with_indifferent_access'
2
+
3
+ module UrlPlumber
4
+ class Plumber
5
+ attr_accessor :params
6
+
7
+ def initialize params
8
+ @params = dup(params)
9
+ end
10
+
11
+ def plumb key_path, value = nil
12
+ keys = key_path.to_s.split('.').map(&:to_sym)
13
+ key = keys[-1].to_sym
14
+
15
+ scopes = []
16
+ scope = dup(params)
17
+
18
+ keys[0..-2].each do |part|
19
+ scopes << scope
20
+ scope = scope.fetch(part) { HashWithIndifferentAccess.new }
21
+ end
22
+
23
+ if value.nil?
24
+ scope.delete key
25
+ else
26
+ scope[key] = value
27
+ end
28
+
29
+ keys[0..-2].reverse.each_with_index do |part|
30
+ parent = scopes.pop
31
+ parent[part] = scope
32
+ scope = parent
33
+ end
34
+
35
+ scope
36
+ end
37
+
38
+ private
39
+ def dup hash
40
+ new_hash = ::HashWithIndifferentAccess.new
41
+ hash.each do |key, value|
42
+ if value.is_a?(Hash)
43
+ new_hash[key] = dup(value)
44
+ else
45
+ new_hash[key] = value
46
+ end
47
+ end
48
+ return new_hash
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module UrlPlumber
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,64 @@
1
+ require 'test/unit'
2
+ require 'active_support/test_case'
3
+ require 'active_support/inflector'
4
+
5
+ require 'url_plumber'
6
+
7
+ class UrlPlumberTest < ActiveSupport::TestCase
8
+ test 'removes key from hash' do
9
+ plumber = ::UrlPlumber::Plumber.new({ foo: 42 })
10
+ assert_empty plumber.plumb(:foo)
11
+ end
12
+
13
+ test 'removes key from nested hash' do
14
+ plumber = ::UrlPlumber::Plumber.new({
15
+ a: {
16
+ b: {
17
+ c: 4,
18
+ x: 6
19
+ }
20
+ }
21
+ })
22
+ expected_hash = {
23
+ "a" => {
24
+ "b" => {
25
+ "c" => 4
26
+ }
27
+ }
28
+ }
29
+ assert_equal expected_hash, plumber.plumb("a.b.x")
30
+ end
31
+
32
+ test 'does not modify original hash' do
33
+ hash = { foo: 42 }
34
+ plumber = ::UrlPlumber::Plumber.new(hash)
35
+ plumber.plumb :foo, 12
36
+ assert_equal({ foo: 42 }, hash)
37
+ end
38
+
39
+ test 'changes value of hash' do
40
+ hash = { foo: 42 }
41
+ plumber = ::UrlPlumber::Plumber.new(hash)
42
+ assert_equal({ "foo" => 12 }, plumber.plumb(:foo, 12))
43
+ end
44
+
45
+ test 'changes value of nested hash' do
46
+ plumber = ::UrlPlumber::Plumber.new({
47
+ a: {
48
+ b: {
49
+ x: 4,
50
+ c: "a"
51
+ }
52
+ }
53
+ })
54
+ expected_hash = {
55
+ "a" => {
56
+ "b" => {
57
+ "x" => 4,
58
+ "c" => 12
59
+ }
60
+ }
61
+ }
62
+ assert_equal expected_hash, plumber.plumb("a.b.c", 12)
63
+ end
64
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'url_plumber/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "url_plumber"
8
+ gem.version = UrlPlumber::VERSION
9
+ gem.authors = ["Raphael Randschau"]
10
+ gem.email = ["nicolai86@me.com"]
11
+ gem.description = %q{modify parts of a hash using a key_path}
12
+ gem.summary = %q{modify parts of a hash using a key_path}
13
+ gem.homepage = "https://github.com/nicolai86/url_plumber"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency 'activesupport', '>= 3.0.0'
22
+ gem.add_development_dependency 'rake', '~> 10.0.3'
23
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: url_plumber
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Raphael Randschau
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ none: false
21
+ name: activesupport
22
+ type: :runtime
23
+ prerelease: false
24
+ requirement: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: 3.0.0
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 10.0.3
36
+ none: false
37
+ name: rake
38
+ type: :development
39
+ prerelease: false
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 10.0.3
45
+ none: false
46
+ description: modify parts of a hash using a key_path
47
+ email:
48
+ - nicolai86@me.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/url_plumber.rb
59
+ - lib/url_plumber/engine.rb
60
+ - lib/url_plumber/plumber.rb
61
+ - lib/url_plumber/version.rb
62
+ - test/url_plumber_test.rb
63
+ - url_plumber.gemspec
64
+ homepage: https://github.com/nicolai86/url_plumber
65
+ licenses:
66
+ - MIT
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ none: false
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ none: false
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.23
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: modify parts of a hash using a key_path
89
+ test_files:
90
+ - test/url_plumber_test.rb
91
+ has_rdoc: