meta_instance 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d4bfd557799d7a5ae48a0dd7ceb48791cc491342
4
+ data.tar.gz: ee4f4bd538775a2c3e29682e3842d079ab118bfa
5
+ SHA512:
6
+ metadata.gz: 194a99f1087ef77810c9f8e860ee8191313d5814a126338ae51e91aea036258380f4ecbc10c1570569a544b921e8e4c89f80f74e6678dd1f9ddbbf024d0ae406
7
+ data.tar.gz: 631cc3e95e6155b7a94181b9e05c7aa963feee3874596e9558d910d9251b9153c6ba8e29df6bd29abdc246a48f0b58d797be06cd4a58f91987892221d67d2834
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ README.html
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ bundler_args: --without guard
3
+ rvm:
4
+ - "2.0"
5
+ - "2.1"
6
+ - ruby-head
7
+ script: "bundle exec rspec"
8
+ addons:
9
+ code_climate:
10
+ repo_token: 1d1b1a31bb3137d986297ad8a1ad5a3a1adbd70f0e8583d7eaf1dd4c0ab0bbe1
11
+ branches:
12
+ only: master
13
+ notifications:
14
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in MetaInstance.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 L. Preston Sego III
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ MetaInstance
2
+ ============
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/meta-instance.svg)](http://badge.fury.io/rb/meta_instance)
5
+ [![Build Status](https://travis-ci.org/NullVoxPopuli/MetaInstance.svg)](https://travis-ci.org/NullVoxPopuli/MetaInstance)
6
+ [![Code Climate](https://codeclimate.com/github/NullVoxPopuli/MetaInstance/badges/gpa.svg)](https://codeclimate.com/github/NullVoxPopuli/MetaInstance)
7
+ [![Test Coverage](https://codeclimate.com/github/NullVoxPopuli/MetaInstance/badges/coverage.svg)](https://codeclimate.com/github/NullVoxPopuli/MetaInstance)
8
+ [![Dependency Status](https://gemnasium.com/NullVoxPopuli/MetaInstance.svg)](https://gemnasium.com/NullVoxPopuli/MetaInstance)
9
+
10
+
11
+ A few helpers for manipulating methods on an instance of an object.
12
+
13
+
14
+ #### In your Gemfile
15
+
16
+ gem 'meta_instance'
17
+
18
+ #### Usage
19
+
20
+ With a class:
21
+
22
+ class Foo
23
+ include MetaInstance
24
+
25
+ def bar; "bar"; end
26
+ end
27
+
28
+ You can do this:
29
+
30
+ f = Foo.new
31
+ f.bar
32
+ # => "bar"
33
+
34
+ f.instance_define(:bar){ "foo" }
35
+ f.bar
36
+ # => "foo"
37
+
38
+ Foo.new.bar
39
+ # => "bar"
40
+
41
+
42
+ See Specs for examples :-)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module MetaInstance
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,57 @@
1
+ # This is based on a few things from
2
+ # http://reference.jumpingmonkey.org/programming_languages/ruby/ruby-metaprogramming.html
3
+ #
4
+ # allows the adding of methods to instances,
5
+ # but not the entire set of instances for a
6
+ # particular class
7
+ module MetaInstance
8
+
9
+ # when a method is stubbed with snapshot data, we stare the
10
+ # original method prixefixed with this:
11
+ METHOD_BACKUP_KEY = "_mata_instance_backup_current_"
12
+
13
+ # backs up and overrides a method.
14
+ # but don't override if we already have overridden this method
15
+ def instance_override(name, &block)
16
+ unless self.respond_to?("#{METHOD_BACKUP_KEY}#{name}")
17
+ backup_instance_method(name)
18
+ end
19
+ instance_define(name, &block)
20
+ end
21
+
22
+ # Adds methods to a metaclass
23
+ def instance_define(name, &block)
24
+ meta_eval {
25
+ define_method(name, &block)
26
+ }
27
+ end
28
+
29
+ # backs up a method in case we want to restore it later
30
+ def backup_instance_method(name)
31
+ meta_eval {
32
+ alias_method "#{METHOD_BACKUP_KEY}#{name}", name
33
+ }
34
+ end
35
+
36
+ # the original method becomes reaccessible
37
+ def restore_instance_method(name)
38
+ meta_eval {
39
+ alias_method name, "#{METHOD_BACKUP_KEY}#{name}"
40
+ remove_method "#{METHOD_BACKUP_KEY}#{name}"
41
+ }
42
+ end
43
+
44
+ private
45
+
46
+ # The hidden singleton lurks behind everyone
47
+ def metaclass
48
+ class << self
49
+ self
50
+ end
51
+ end
52
+
53
+ def meta_eval(&block)
54
+ metaclass.instance_eval(&block)
55
+ end
56
+
57
+ end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe MetaInstance do
4
+ let(:original){"bar"}
5
+ let(:f){ Foo.new }
6
+
7
+ it "returns the original value" do
8
+ expect(f.bar).to eq original
9
+ end
10
+
11
+ context "instance_define" do
12
+
13
+ context "without args" do
14
+
15
+ before(:each) do
16
+ f.instance_define(:bar) do
17
+ "foo"
18
+ end
19
+ end
20
+
21
+ it "redefines the method" do
22
+ expect(f.bar).to eq "foo"
23
+ end
24
+
25
+ it "returns the redefined value on another instance" do
26
+ expect(Foo.new.bar).to_not eq "foo"
27
+ end
28
+
29
+ end
30
+
31
+ context "with args" do
32
+
33
+ before(:each) do
34
+ f.instance_define(:bar) do |number|
35
+ number * 2
36
+ end
37
+
38
+ it "takes an argument" do
39
+ expect(f.bar(3)).to eq 6
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ context "backup_instance_method" do
46
+ before(:each) do
47
+ f.backup_instance_method(:bar)
48
+ end
49
+
50
+ it "has the original method accessible" do
51
+ name = "#{MetaInstance::METHOD_BACKUP_KEY}bar"
52
+ expect(f.send(name)).to eq original
53
+ end
54
+ end
55
+
56
+ context "restore_instance_method" do
57
+ before(:each) do
58
+ f.backup_instance_method(:bar)
59
+ f.instance_define(:bar) { "different" }
60
+ f.restore_instance_method(:bar)
61
+ end
62
+
63
+ it "returns the original" do
64
+ expect(f.bar).to eq original
65
+ end
66
+
67
+ it "doesn't respond_to the backup method" do
68
+ expect(f).to_not respond_to "#{MetaInstance::METHOD_BACKUP_KEY}bar"
69
+ end
70
+ end
71
+
72
+ context "instance_override" do
73
+ before(:each) do
74
+ f.instance_override(:bar) do
75
+ 4
76
+ end
77
+ end
78
+
79
+ it "returns the new value" do
80
+ expect(f.bar).to eq 4
81
+ end
82
+
83
+ it "has the original method accessible" do
84
+ name = "#{MetaInstance::METHOD_BACKUP_KEY}bar"
85
+ expect(f.send(name)).to eq original
86
+ end
87
+
88
+ it "returns the redefined value on another instance" do
89
+ expect(Foo.new.bar).to_not eq "foo"
90
+ end
91
+
92
+ context "second call on instance_override" do
93
+ before(:each) do
94
+ f.instance_override(:bar) do
95
+ 0
96
+ end
97
+ end
98
+
99
+ it "does not re-backup the method" do
100
+ name = "#{MetaInstance::METHOD_BACKUP_KEY}bar"
101
+ expect(f.send(name)).to eq original
102
+ end
103
+
104
+ it "returns the new value" do
105
+ expect(f.bar).to eq 0
106
+ end
107
+
108
+ end
109
+
110
+ end
111
+
112
+ end
@@ -0,0 +1,33 @@
1
+
2
+ require "rubygems"
3
+ require "bundler/setup"
4
+
5
+ require "pry-byebug" # binding.pry to debug!
6
+
7
+ # Coverage
8
+ require "codeclimate-test-reporter"
9
+ ENV['CODECLIMATE_REPO_TOKEN'] = "53c2221e147d16cbe8a445915377ff528b291a9534f5cb72f6abf8df13bd06f4"
10
+ CodeClimate::TestReporter.start
11
+
12
+ # This Gem
13
+ require "meta_instance"
14
+
15
+ Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each {|file| require file }
16
+
17
+ # This file was generated by the `rspec --init` command. Conventionally, all
18
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
19
+ # Require this file using `require "spec_helper"` to ensure that it is only
20
+ # loaded once.
21
+ #
22
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
23
+ RSpec.configure do |config|
24
+ config.treat_symbols_as_metadata_keys_with_true_values = true
25
+ config.run_all_when_everything_filtered = true
26
+ config.filter_run :focus
27
+
28
+ # Run specs in random order to surface order dependencies. If you find an
29
+ # order dependency and want to debug it, you can fix the order by providing
30
+ # the seed, which is printed after each run.
31
+ # --seed 1234
32
+ config.order = 'random'
33
+ end
@@ -0,0 +1,5 @@
1
+ class Foo
2
+ include MetaInstance
3
+
4
+ def bar; "bar"; end
5
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meta_instance
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - L. Preston Sego III
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-18 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
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
+ - !ruby/object:Gem::Dependency
56
+ name: codeclimate-test-reporter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A few helpers for manipulating methods on an instance of an object.
70
+ email: LPSego3+dev@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - lib/meta_instance.rb
83
+ - lib/meta_instance/version.rb
84
+ - spec/meta_instance_spec.rb
85
+ - spec/spec_helper.rb
86
+ - spec/support/foo.rb
87
+ homepage: https://github.com/NullVoxPopuli/MetaInstance
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.1
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: MetaInstance-1.0.0
111
+ test_files:
112
+ - spec/meta_instance_spec.rb
113
+ - spec/spec_helper.rb
114
+ - spec/support/foo.rb