url_plumber 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/url_plumber.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "url_plumber/version"
2
2
 
3
3
  module UrlPlumber
4
- autoload :Plumber, 'url_plumber/plumber'
4
+ autoload :Plumber, "url_plumber/plumber"
5
5
  end
@@ -1,4 +1,4 @@
1
- require 'active_support/hash_with_indifferent_access'
1
+ require "active_support/hash_with_indifferent_access"
2
2
 
3
3
  module UrlPlumber
4
4
  class Plumber
@@ -8,12 +8,19 @@ module UrlPlumber
8
8
  @params = dup(params)
9
9
  end
10
10
 
11
- def plumb key_path, value = nil
11
+ def plumb attributes = {}
12
+ scope = dup(params)
13
+ attributes.each do |key, value|
14
+ scope = update_scope scope, key, value
15
+ end
16
+ scope
17
+ end
18
+
19
+ def update_scope scope, key_path, value = nil
12
20
  keys = key_path.to_s.split('.').map(&:to_sym)
13
- key = keys[-1].to_sym
21
+ key = keys[-1]
14
22
 
15
23
  scopes = []
16
- scope = dup(params)
17
24
 
18
25
  keys[0..-2].each do |part|
19
26
  scopes << scope
@@ -26,7 +33,7 @@ module UrlPlumber
26
33
  scope[key] = value
27
34
  end
28
35
 
29
- keys[0..-2].reverse.each_with_index do |part|
36
+ keys[0..-2].reverse.each do |part|
30
37
  parent = scopes.pop
31
38
  parent[part] = scope
32
39
  scope = parent
@@ -36,6 +43,8 @@ module UrlPlumber
36
43
  end
37
44
 
38
45
  private
46
+ # params.dup returns the original request parameters in Ruby on Rails
47
+ # so we need to get our hands dirty to create an actual copy of the params
39
48
  def dup hash
40
49
  new_hash = ::HashWithIndifferentAccess.new
41
50
  hash.each do |key, value|
@@ -1,3 +1,3 @@
1
1
  module UrlPlumber
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,21 +1,21 @@
1
- require 'test/unit'
2
- require 'active_support/test_case'
3
- require 'active_support/inflector'
1
+ require "test/unit"
2
+ require "active_support/test_case"
3
+ require "active_support/inflector"
4
4
 
5
- require 'url_plumber'
5
+ require "url_plumber"
6
6
 
7
7
  class UrlPlumberTest < ActiveSupport::TestCase
8
8
  test 'removes key from hash' do
9
- plumber = ::UrlPlumber::Plumber.new({ foo: 42 })
10
- assert_empty plumber.plumb(:foo)
9
+ plumber = ::UrlPlumber::Plumber.new({ "foo" => 42 })
10
+ assert_empty plumber.plumb("foo" => nil)
11
11
  end
12
12
 
13
13
  test 'removes key from nested hash' do
14
14
  plumber = ::UrlPlumber::Plumber.new({
15
- a: {
16
- b: {
17
- c: 4,
18
- x: 6
15
+ "a" => {
16
+ "b" => {
17
+ "c" => 4,
18
+ "x" => 6
19
19
  }
20
20
  }
21
21
  })
@@ -26,28 +26,28 @@ class UrlPlumberTest < ActiveSupport::TestCase
26
26
  }
27
27
  }
28
28
  }
29
- assert_equal expected_hash, plumber.plumb("a.b.x")
29
+ assert_equal expected_hash, plumber.plumb("a.b.x" => nil)
30
30
  end
31
31
 
32
32
  test 'does not modify original hash' do
33
- hash = { foo: 42 }
33
+ hash = { "foo" => 42 }
34
34
  plumber = ::UrlPlumber::Plumber.new(hash)
35
- plumber.plumb :foo, 12
36
- assert_equal({ foo: 42 }, hash)
35
+ plumber.plumb "foo" => 12
36
+ assert_equal({ "foo" => 42 }, hash)
37
37
  end
38
38
 
39
39
  test 'changes value of hash' do
40
- hash = { foo: 42 }
40
+ hash = { "foo" => 42 }
41
41
  plumber = ::UrlPlumber::Plumber.new(hash)
42
- assert_equal({ "foo" => 12 }, plumber.plumb(:foo, 12))
42
+ assert_equal({ "foo" => 12 }, plumber.plumb("foo" => 12))
43
43
  end
44
44
 
45
45
  test 'changes value of nested hash' do
46
46
  plumber = ::UrlPlumber::Plumber.new({
47
- a: {
48
- b: {
49
- x: 4,
50
- c: "a"
47
+ "a" => {
48
+ "b" => {
49
+ "x" => 4,
50
+ "c" => "a"
51
51
  }
52
52
  }
53
53
  })
@@ -59,6 +59,18 @@ class UrlPlumberTest < ActiveSupport::TestCase
59
59
  }
60
60
  }
61
61
  }
62
- assert_equal expected_hash, plumber.plumb("a.b.c", 12)
62
+ assert_equal expected_hash, plumber.plumb("a.b.c" => 12)
63
+ end
64
+
65
+ test 'changes value of hash with multiple key value pairs' do
66
+ hash = { "foo" => 42, "bar" => 10 }
67
+ plumber = ::UrlPlumber::Plumber.new(hash)
68
+ assert_equal({ "foo" => 12, "bar" => 32 }, plumber.plumb("foo" => 12, "bar" => 32))
69
+ end
70
+
71
+ test 'removes key of hash with multiple key value pairs' do
72
+ hash = { "foo" => 42, "bar" => 10, "baz" => 12 }
73
+ plumber = ::UrlPlumber::Plumber.new(hash)
74
+ assert_equal({ "foo" => 42 }, plumber.plumb("bar" => nil, "baz" => nil))
63
75
  end
64
76
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: url_plumber
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Raphael Randschau
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-03 00:00:00.000000000 Z
12
+ date: 2013-02-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  version_requirements: !ruby/object:Gem::Requirement
@@ -56,7 +56,6 @@ files:
56
56
  - README.md
57
57
  - Rakefile
58
58
  - lib/url_plumber.rb
59
- - lib/url_plumber/engine.rb
60
59
  - lib/url_plumber/plumber.rb
61
60
  - lib/url_plumber/version.rb
62
61
  - test/url_plumber_test.rb
@@ -1,6 +0,0 @@
1
- require 'rails'
2
-
3
- module Flexirails
4
- class Engine < ::Rails::Engine
5
- end
6
- end