parameter_transformers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a5ecf03a52a2aeb572ec6a74ff00c12a0340e56d
4
+ data.tar.gz: fe2a47922bf49418da29b650865b03824c5a6193
5
+ SHA512:
6
+ metadata.gz: 7e63f973c1d71e3f1bd46c35c9355058462728496ce66e1a8c591f0abe01e723cc25aa6d058d434fce674a114b5abdc2866afdad9c017d512ae4d63aeeed77ab
7
+ data.tar.gz: 9760d29e92aff6a19502a8b992b465f1422f603eb5bcec1ee1582ae0e2faf7037e2072250e088e758e5e2f1bb935918eabac7d7ff5b2a19e17282b77a29346c4
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in parameter_transformers.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Brian Zeligson
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.
@@ -0,0 +1,59 @@
1
+ # ParameterTransformers
2
+
3
+ This gem provides a proxy which can encapsulate transformers to be
4
+ applied to given method/parameter combinations before the target
5
+ method is called.
6
+
7
+ It is useful for scenarios where you might have a stateful value that
8
+ needs to be consistently applied to other parameters being passed to
9
+ library code.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'parameter_transformers'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install parameter_transformers
26
+
27
+ ## Usage
28
+
29
+ Here's an example of using the proxy to consistently apply an authentication header to all requests made with the RestClient gem:
30
+
31
+ ```ruby
32
+ require 'rest_client'
33
+ require 'parameter_transformers'
34
+
35
+ add_auth_header = ->(headers) { (headers || {}).merge('AUTH-HEADER' => 'key') }
36
+ transformers = [:get, :post, :put, :delete].map do |method|
37
+ [[method, :headers], add_auth_header]
38
+ end
39
+ rc = ParameterTransformers::Proxy.new(RestClient, Hash[transformers])
40
+
41
+ # automatically adds {'AUTH-HEADER' => 'key'} to headers on all calls now
42
+ rc.get('http://somesite.com')
43
+ rc.post('http://somesite.com', {foo: 'bar'})
44
+ rc.put('http://somesite.com', {foo: 'bar'})
45
+ rc.delete('http://somesite.com')
46
+ ```
47
+
48
+ Note per example above the format for transformers is a hash where
49
+ keys are an array of [:method_name, :argument_name] and values
50
+ are procs which receive a copy of the passed argument, and return the
51
+ transformed value to be passed to the method on the target object
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it ( https://github.com/[my-github-username]/parameter_transformers/fork )
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,32 @@
1
+ require "parameter_transformers/version"
2
+
3
+ module ParameterTransformers
4
+
5
+ class Proxy
6
+
7
+ instance_methods.each do |m|
8
+ undef_method(m) unless m =~ /(^__|^nil\?$|^send$|^object_id$)/
9
+ end
10
+
11
+ def initialize(target, transformers={})
12
+ @target, @transformers = target, transformers
13
+ end
14
+
15
+ def respond_to?(symbol, include_priv=false)
16
+ @target.respond_to?(symbol, include_priv)
17
+ end
18
+
19
+ def method_missing(m, *args, &block)
20
+ params = @target.method(m).parameters.map(&:last)
21
+ t_args = args.dup
22
+ params.each_with_index do |param, ix|
23
+ if @transformers[[m, param]] &&
24
+ @transformers[[m, param]].kind_of?(Proc)
25
+ t_args[ix] =
26
+ @transformers[[m, param]].call(t_args[ix])
27
+ end
28
+ end
29
+ @target.send(m, *t_args, &block)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module ParameterTransformers
2
+ VERSION = "0.0.1"
3
+ end
@@ -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 'parameter_transformers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "parameter_transformers"
8
+ spec.version = ParameterTransformers::VERSION
9
+ spec.authors = ["Brian Zeligson"]
10
+ spec.email = ["bzeligson@localytics.com"]
11
+ spec.summary = %q{Provides a proxy object that allows consistent transformation of arguments passed to target object methods}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe ParameterTransformers::Proxy do
4
+
5
+ class Tester
6
+
7
+ def add_one_to_both(a, b)
8
+ [a+1, b+1]
9
+ end
10
+
11
+ def self.add_one_to_both(a, b)
12
+ [a+1, b+1]
13
+ end
14
+ end
15
+
16
+ it 'takes given parameter transformers and applies whenever matched' do
17
+ transformers = {
18
+ [:add_one_to_both, :a] => ->(x) { x + 3 },
19
+ [:add_one_to_both, :b] => ->(x) { x - 3 }
20
+ }
21
+ tc = ParameterTransformers::Proxy.new(Tester, transformers)
22
+ r = tc.add_one_to_both(4, 5)
23
+ expect(r.first).to eq(8)
24
+ expect(r.last).to eq(3)
25
+
26
+ t = ParameterTransformers::Proxy.new(Tester.new, transformers)
27
+ r = t.add_one_to_both(4, 5)
28
+ expect(r.first).to eq(8)
29
+ expect(r.last).to eq(3)
30
+ end
31
+ end
@@ -0,0 +1,92 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ require_relative '../lib/parameter_transformers'
21
+ # rspec-expectations config goes here. You can use an alternate
22
+ # assertion/expectation library such as wrong or the stdlib/minitest
23
+ # assertions if you prefer.
24
+ config.expect_with :rspec do |expectations|
25
+ # This option will default to `true` in RSpec 4. It makes the `description`
26
+ # and `failure_message` of custom matchers include text for helper methods
27
+ # defined using `chain`, e.g.:
28
+ # be_bigger_than(2).and_smaller_than(4).description
29
+ # # => "be bigger than 2 and smaller than 4"
30
+ # ...rather than:
31
+ # # => "be bigger than 2"
32
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
33
+ end
34
+
35
+ # rspec-mocks config goes here. You can use an alternate test double
36
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
37
+ config.mock_with :rspec do |mocks|
38
+ # Prevents you from mocking or stubbing a method that does not exist on
39
+ # a real object. This is generally recommended, and will default to
40
+ # `true` in RSpec 4.
41
+ mocks.verify_partial_doubles = true
42
+ end
43
+
44
+ # The settings below are suggested to provide a good initial experience
45
+ # with RSpec, but feel free to customize to your heart's content.
46
+ =begin
47
+ # These two settings work together to allow you to limit a spec run
48
+ # to individual examples or groups you care about by tagging them with
49
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
50
+ # get run.
51
+ config.filter_run :focus
52
+ config.run_all_when_everything_filtered = true
53
+
54
+ # Limits the available syntax to the non-monkey patched syntax that is
55
+ # recommended. For more details, see:
56
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
57
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
58
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
59
+ config.disable_monkey_patching!
60
+
61
+ # This setting enables warnings. It's recommended, but in some cases may
62
+ # be too noisy due to issues in dependencies.
63
+ config.warnings = true
64
+
65
+ # Many RSpec users commonly either run the entire suite or an individual
66
+ # file, and it's useful to allow more verbose output when running an
67
+ # individual spec file.
68
+ if config.files_to_run.one?
69
+ # Use the documentation formatter for detailed output,
70
+ # unless a formatter has already been configured
71
+ # (e.g. via a command-line flag).
72
+ config.default_formatter = 'doc'
73
+ end
74
+
75
+ # Print the 10 slowest examples and example groups at the
76
+ # end of the spec run, to help surface which specs are running
77
+ # particularly slow.
78
+ config.profile_examples = 10
79
+
80
+ # Run specs in random order to surface order dependencies. If you find an
81
+ # order dependency and want to debug it, you can fix the order by providing
82
+ # the seed, which is printed after each run.
83
+ # --seed 1234
84
+ config.order = :random
85
+
86
+ # Seed global randomization in this process using the `--seed` CLI option.
87
+ # Setting this allows you to use `--seed` to deterministically reproduce
88
+ # test failures related to randomization by passing the same `--seed` value
89
+ # as the one that triggered the failure.
90
+ Kernel.srand config.seed
91
+ =end
92
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parameter_transformers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Zeligson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-06 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: ''
56
+ email:
57
+ - bzeligson@localytics.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/parameter_transformers.rb
69
+ - lib/parameter_transformers/version.rb
70
+ - parameter_transformers.gemspec
71
+ - spec/parameter_transformers_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: ''
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.6
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Provides a proxy object that allows consistent transformation of arguments
97
+ passed to target object methods
98
+ test_files:
99
+ - spec/parameter_transformers_spec.rb
100
+ - spec/spec_helper.rb