delegate_to_instance 1.0.0

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: ca5a1577bae3ab3b06810c177705ee6f00fc53aa
4
+ data.tar.gz: bc5325b4b8c3f1324c3ccea3675c7bbc149859f6
5
+ SHA512:
6
+ metadata.gz: 0ca3644eef96e3e6c0135a56abac93dd003d03be750051e794715cf3a25cde8f6a5e588802b6eb83a26e4ef7b3f0d5bbe5a3c0f04fed00f4c377ccfe57d64ba5
7
+ data.tar.gz: e9585e17ad60d0b4e872a0edb18bd7f8ac2039d8c33b32cc630dee0af78b9f636ddaa36d6e92438663fd5c2cbe0c28e4f64ded42eb58f644c4d2265dd2472ef4
@@ -0,0 +1,15 @@
1
+ .rspec
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require pry
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in delegate_to_instance.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Nisanth Chunduru
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,95 @@
1
+ # delegate_to_instance
2
+
3
+ Helper for single purpose objects
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'delegate_to_instance'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install delegate_to_instance
20
+
21
+ ## Usage
22
+
23
+ Write
24
+ ```ruby
25
+ class SendInvoiceJob
26
+ extend DelegateToInstance
27
+ delegate_to_instance :perform
28
+
29
+ def initialize(invoice_id, recipients)
30
+ @invoice_id, @recipients = invoice_id, recipients
31
+ end
32
+
33
+ def perform
34
+ send_invoice(invoice_id, recipients)
35
+ end
36
+ end
37
+ ```
38
+
39
+ instead of
40
+ ```ruby
41
+ class SendInvoiceJob
42
+ def self.perform(invoice_id, recipients)
43
+ new(invoice_id, recipients).perform
44
+ end
45
+
46
+ def initialize(invoice_id, recipients)
47
+ @invoice_id, @recipients = invoice_id, recipients
48
+ end
49
+
50
+ def perform
51
+ send_invoice(invoice_id, recipients)
52
+ end
53
+ end
54
+ ```
55
+
56
+ ## Why
57
+
58
+ When I write single purpose objects, like
59
+ ```ruby
60
+ class HTMLSanitizer
61
+ def self.sanitize(html)
62
+ new(html).sanitize
63
+ end
64
+
65
+ def initialize(html)
66
+ @html = html
67
+ end
68
+
69
+ def sanitize
70
+ sanitize_html(html)
71
+ end
72
+ end
73
+ ```
74
+ I often find myself writing a class level delegator (and corresponding tests for it)
75
+ ```ruby
76
+ class HTMLSanitizer
77
+ def self.sanitize(html)
78
+ new(html).sanitize
79
+ end
80
+
81
+ # ...
82
+ ```
83
+
84
+ Instead, just write
85
+ ```ruby
86
+ class HTMLSanitizer
87
+ include DelegateToInstance
88
+ delegate_to_instance :sanitize
89
+
90
+ # ...
91
+ ```
92
+
93
+ ## TODO
94
+
95
+ 1. Add a RSpec matcher
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'delegate_to_instance/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "delegate_to_instance"
8
+ spec.version = DelegateToInstance::VERSION
9
+ spec.authors = ["Nisanth Chunduru"]
10
+ spec.email = ["nisanth074@gmail.com"]
11
+ spec.summary = 'Helper for single purpose objects'
12
+ spec.homepage = 'https://github.com/nisanth074/delegate_to_instance'
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency 'rspec', '~> 3.1'
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,9 @@
1
+ require "delegate_to_instance/version"
2
+
3
+ module DelegateToInstance
4
+ def delegate_to_instance(method)
5
+ define_singleton_method(method) do |*args|
6
+ new(*args).public_send(method)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module DelegateToInstance
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'delegate_to_instance'
3
+
4
+ class DummyHTMLSanitizer
5
+ extend DelegateToInstance
6
+ delegate_to_instance :sanitize
7
+
8
+ def initialize(html)
9
+ @html = html
10
+ end
11
+
12
+ def sanitize
13
+ @html
14
+ end
15
+ end
16
+
17
+ describe DelegateToInstance do
18
+ let(:html) { '<p>Overpriced coffee, awesome!</p>' }
19
+ let(:sanitizer) { DummyHTMLSanitizer.new(html) }
20
+
21
+ it 'creates an instance' do
22
+ expect(DummyHTMLSanitizer).to receive(:new).with(html).once.and_return(sanitizer)
23
+ DummyHTMLSanitizer.sanitize(html)
24
+ end
25
+
26
+ it 'delegates method to that instance' do
27
+ expect(DummyHTMLSanitizer).to receive(:new).with(html).once.and_return(sanitizer)
28
+ expect(sanitizer).to receive(:sanitize).once
29
+ DummyHTMLSanitizer.sanitize(html)
30
+ end
31
+ end
@@ -0,0 +1,89 @@
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 this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # rspec-expectations config goes here. You can use an alternate
19
+ # assertion/expectation library such as wrong or the stdlib/minitest
20
+ # assertions if you prefer.
21
+ config.expect_with :rspec do |expectations|
22
+ # This option will default to `true` in RSpec 4. It makes the `description`
23
+ # and `failure_message` of custom matchers include text for helper methods
24
+ # defined using `chain`, e.g.:
25
+ # be_bigger_than(2).and_smaller_than(4).description
26
+ # # => "be bigger than 2 and smaller than 4"
27
+ # ...rather than:
28
+ # # => "be bigger than 2"
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ # rspec-mocks config goes here. You can use an alternate test double
33
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
34
+ config.mock_with :rspec do |mocks|
35
+ # Prevents you from mocking or stubbing a method that does not exist on
36
+ # a real object. This is generally recommended, and will default to
37
+ # `true` in RSpec 4.
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+
41
+ # The settings below are suggested to provide a good initial experience
42
+ # with RSpec, but feel free to customize to your heart's content.
43
+ =begin
44
+ # These two settings work together to allow you to limit a spec run
45
+ # to individual examples or groups you care about by tagging them with
46
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
47
+ # get run.
48
+ config.filter_run :focus
49
+ config.run_all_when_everything_filtered = true
50
+
51
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
52
+ # For more details, see:
53
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
54
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
55
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
56
+ config.disable_monkey_patching!
57
+
58
+ # This setting enables warnings. It's recommended, but in some cases may
59
+ # be too noisy due to issues in dependencies.
60
+ config.warnings = true
61
+
62
+ # Many RSpec users commonly either run the entire suite or an individual
63
+ # file, and it's useful to allow more verbose output when running an
64
+ # individual spec file.
65
+ if config.files_to_run.one?
66
+ # Use the documentation formatter for detailed output,
67
+ # unless a formatter has already been configured
68
+ # (e.g. via a command-line flag).
69
+ config.default_formatter = 'doc'
70
+ end
71
+
72
+ # Print the 10 slowest examples and example groups at the
73
+ # end of the spec run, to help surface which specs are running
74
+ # particularly slow.
75
+ config.profile_examples = 10
76
+
77
+ # Run specs in random order to surface order dependencies. If you find an
78
+ # order dependency and want to debug it, you can fix the order by providing
79
+ # the seed, which is printed after each run.
80
+ # --seed 1234
81
+ config.order = :random
82
+
83
+ # Seed global randomization in this process using the `--seed` CLI option.
84
+ # Setting this allows you to use `--seed` to deterministically reproduce
85
+ # test failures related to randomization by passing the same `--seed` value
86
+ # as the one that triggered the failure.
87
+ Kernel.srand config.seed
88
+ =end
89
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delegate_to_instance
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nisanth Chunduru
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - nisanth074@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec.example"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - delegate_to_instance.gemspec
69
+ - lib/delegate_to_instance.rb
70
+ - lib/delegate_to_instance/version.rb
71
+ - spec/delegate_to_instance_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: https://github.com/nisanth074/delegate_to_instance
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.5
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Helper for single purpose objects
97
+ test_files:
98
+ - spec/delegate_to_instance_spec.rb
99
+ - spec/spec_helper.rb