clean_params 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YTJlOTkyYzZkYzhhMjU3NzY1N2I2NWJhYTg0MTUxOWM3ZjczNmY2Nw==
4
+ NzAwZjBlZjkwYzQ0NWE3NDIwZWU4NzUyODlkNjE0ZDZjZDM2NzJlNw==
5
5
  data.tar.gz: !binary |-
6
- ZDNjOWM5NTI5ZTE1NGMyMzIwMWFjZWI4Y2U1M2UxYzQyMDIyMGE0NQ==
6
+ MzlhNTkzNDYyZTcyOTBhMzI0ZmYxNzAwODA3ZDIzNjhkYTNlZjliOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MmVkMTllZWQ2ZjY4NTExZDhhODRlZDZhMzEzZDIxYmIwNDAxMTkyZDMxNzRk
10
- ZGNmOGRkZTRjNGE2MTFhZDk5YmJlNmEzNDU1MWNiZDIwNmI5ODczOGIyNDE5
11
- Yzc1YWExMDg2OWFjYWE5N2IyOGY1MzcxZGM3NWFkMDY3ZDBhZTc=
9
+ YmE0OGY4OWUzYmI2OTU0OTZhMjUzYjJlYmUxZTNhN2MzNjZiMjdhOTY0ZGEw
10
+ YTZlNDFlM2NjNTU5MjliODYxOTU5MmE5NDBkNThhYmEzMTE5NDE1NDU4M2Fl
11
+ ZTVkMWFjYzcxMWEyMGVjZDRhY2VjZGNjMDdjNTNjZDc0YTczZTA=
12
12
  data.tar.gz: !binary |-
13
- ZmM3YzMxY2QyYjE4ZjAzNDU1YTI5YzYzZDlmNDUxNjg2MjM4OTIzNDhmOTIz
14
- YmUyYzAzM2ZiMTJmNjkxZDI2NTBmMTU5M2QzZTNlZWEyYjhjNDgxMjFmMGM4
15
- NDlkZWViZTA3ZDVkMmFkY2IzMGQ1ODQ2MzE4NTdjY2M5MDc0MTg=
13
+ YWMzZGQxYjA1OTMyYzJmOTk5NWVhY2RkNDIwZDcyYmQ4MmQyYzZjODA2MTJh
14
+ NGI0MTA2MTk3ZjlhNTQ4NWUzNzhmYjRhM2M2ZTdjN2JhOTZkODQxOGVlZmUz
15
+ NTIyMGQ1NTgzZWEyYTEwZTYxMjIxNDMxY2VmZTEzNjY4ZGVmMDg=
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # CleanParams
2
2
 
3
3
  Clean Params is a Ruby gem for use with Rails to access the controller parameters cleanly.
4
- No more ugly hash accesses. No more case sensitive checking.
4
+ No more ugly hash accesses. No more flooding your codebase with multiple condition checking.
5
+ With clean_params you can specify various rules to parse your params hash inside the initializer file.
6
+ It also allows for nested hash parsing.
5
7
 
6
8
  ## Installation
7
9
 
@@ -18,6 +20,8 @@ Run the generator to create the initializer file. You can specify your parsing r
18
20
  params = CleanParams.clean(params) # params - rails params hash
19
21
  params.key # => params[:key]
20
22
  params.token # => params[:token]
23
+
24
+ Or you can include it inside the filter in ApplicationController and use the object inside any controller.
21
25
 
22
26
  ## Contributing
23
27
 
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
23
24
  end
@@ -1,31 +1,42 @@
1
1
  require "clean_params/version"
2
- require 'active_support/core_ext/hash'
2
+ #require 'active_support/core_ext/hash'
3
3
 
4
4
  module CleanParams
5
5
  class << self
6
6
  attr_accessor :configuration
7
7
  end
8
8
 
9
+ # Sets configuration object
9
10
  def self.configure
10
11
  self.configuration ||= Configuration.new
11
12
  yield(configuration)
12
13
  end
13
14
 
15
+ # main method call; returns the final object
14
16
  def self.clean(controllerParams)
15
- configuration.extract_params(controllerParams.with_indifferent_access)
17
+ configuration.controller_params = controllerParams
18
+ configuration.extract_params
16
19
  return configuration
17
20
  end
18
21
 
19
22
  class Configuration
20
- attr_accessor :params
23
+ attr_accessor :params, :controller_params
21
24
 
22
- def extract_params(controllerParams)
25
+ # for each rule in configuration file, check the params hash and sets instance variables
26
+ def extract_params
23
27
  params.each do |key, val|
24
- self.class.send(:attr_accessor, key)
25
- instance_variable_set("@#{key}", get_value_from_params(val, controllerParams))
28
+ set_instances(key, val)
26
29
  end
27
30
  end
28
31
 
32
+ private
33
+
34
+ def set_instances(key, val)
35
+ self.class.send(:attr_accessor, key)
36
+ instance_variable_set("@#{key}", get_value_from_params(val, controller_params))
37
+ end
38
+
39
+ # search params hash for values in rules
29
40
  def get_value_from_params(val, controllerParams)
30
41
  if val.is_a? String
31
42
  val = val.split(",")
@@ -37,15 +48,19 @@ module CleanParams
37
48
  nil # return nil if no match
38
49
  end
39
50
 
51
+ # Utility method: search hash recursively
40
52
  def search_hash(h, search)
41
53
  return h[search] if h.fetch(search, nil)
42
-
43
54
  h.keys.each do |k|
44
55
  answer = search_hash(h[k], search) if h[k].is_a? Hash
45
56
  return answer if answer
46
57
  end
47
-
48
58
  nil
49
59
  end
60
+
61
+ # return nil if no rule for this attribute
62
+ def method_missing(method, *args, &block)
63
+ set_instances(method, method.to_s)
64
+ end
50
65
  end
51
66
  end
@@ -1,3 +1,3 @@
1
1
  module CleanParams
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ describe CleanParams do
4
+ before(:each) do
5
+ CleanParams.configure do |config|
6
+ config.params = {
7
+ 'key' => ['key', 'Key'],
8
+ 'token' => ['token', "Token"],
9
+ 'source' => ['source', 'Source'],
10
+ 'name' => ['name', 'Name', 'customer_name']
11
+ }
12
+ end
13
+ end
14
+
15
+ let(:hash) { {"key"=>"123", "Key" => "345", "keys" => "1,2,3", "token"=>"asd34dd", "nested"=>{"source"=>"clean_params", "name"=>"XYZ"}} }
16
+ let(:res) { CleanParams.clean(hash) }
17
+
18
+ it "should return the match correctly" do
19
+ expect(res.token).to be == 'asd34dd'
20
+ end
21
+
22
+ it "should return the first match" do
23
+ expect(res.key).to be == '123'
24
+ end
25
+
26
+ it "should return the nested param correctly" do
27
+ expect(res.source).to be == 'clean_params'
28
+ end
29
+
30
+ it "should return nil for the no-match" do
31
+ expect(res.sources).to be_nil
32
+ end
33
+
34
+ it "should return value if present for no rule" do
35
+ expect(res.keys).to be == "1,2,3"
36
+ end
37
+
38
+ it "should return nil if not present for no rule" do
39
+ expect(res.no_keys).to be_nil
40
+ end
41
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.require(:default)
4
+
5
+ RSpec.configure do |config|
6
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clean_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yogesh Pendharkar
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ! '>='
39
39
  - !ruby/object:Gem::Version
40
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'
41
55
  description: Easy and clean way to maintain your params.
42
56
  email:
43
57
  - yogesh.pendharkar@gmail.com
@@ -46,6 +60,7 @@ extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
62
  - .gitignore
63
+ - .rspec
49
64
  - Gemfile
50
65
  - LICENSE.txt
51
66
  - README.md
@@ -55,6 +70,8 @@ files:
55
70
  - lib/clean_params/version.rb
56
71
  - lib/generators/clean_params/install_generator.rb
57
72
  - lib/generators/clean_params/templates/clean_params.rb
73
+ - spec/clean_params_spec.rb
74
+ - spec/spec_helper.rb
58
75
  homepage: https://github.com/ypendharkar/clean_params
59
76
  licenses:
60
77
  - MIT
@@ -79,4 +96,6 @@ rubygems_version: 2.1.3
79
96
  signing_key:
80
97
  specification_version: 4
81
98
  summary: Clean your params hash with clean_params
82
- test_files: []
99
+ test_files:
100
+ - spec/clean_params_spec.rb
101
+ - spec/spec_helper.rb