law_of_demeter_violator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
+ tags
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in law_of_demeter_violator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Gabe Berke-Williams
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,52 @@
1
+ # Law Of Demeter Violator
2
+
3
+ `user.name` is for jerks who think patterns are cool. You and me, we know that
4
+ the Gang of Four were [communists](http://en.wikipedia.org/wiki/Gang_of_four).
5
+ Let's do `user.name.name` instead.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'law_of_demeter_violator'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install law_of_demeter_violator
20
+
21
+ Finally, ask yourself why you installed this. Just have a long talk.
22
+
23
+ ## Usage
24
+
25
+ You have two options.
26
+
27
+ 1) include the `LawOfDemeterViolator` module in your class, at the end:
28
+
29
+ class BadIdea
30
+ def meth1
31
+ end
32
+
33
+ include LawOfDemeterViolator
34
+ end
35
+
36
+ Unfortunately, it has to be at the end of the class definition.
37
+
38
+ 2) use the global `Violate` method:
39
+
40
+ Violate(BadIdea)
41
+
42
+ ## Is it tested?
43
+
44
+ Of course. I'm not a monster.
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new
7
+
8
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/law_of_demeter_violator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Gabe Berke-Williams"]
6
+ gem.email = ["gabe@thoughtbot.com"]
7
+ gem.description = %q{Violate the Law of Demeter hardcore with this too-easy-to-use library.}
8
+ gem.summary = %q{Violate the Law of Demeter hardcore with this too-easy-to-use library.}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "law_of_demeter_violator"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = LawOfDemeterViolator::VERSION
17
+
18
+ gem.add_development_dependency("rspec", "~> 2.0")
19
+ gem.add_development_dependency("bourne", "~> 1.0")
20
+ end
@@ -0,0 +1,10 @@
1
+ require "law_of_demeter_violator/violator"
2
+ require "law_of_demeter_violator/global_method"
3
+ require "law_of_demeter_violator/version"
4
+
5
+ module LawOfDemeterViolator
6
+ def self.included(base)
7
+ violator = Violator.new(base)
8
+ violator.violate
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ def Violate(object)
2
+ violator = LawOfDemeterViolator::Violator.new(object)
3
+ violator.violate
4
+ end
@@ -0,0 +1,3 @@
1
+ module LawOfDemeterViolator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,35 @@
1
+ require "ostruct"
2
+
3
+ module LawOfDemeterViolator
4
+ class Violator
5
+ def initialize(class_to_be_violated)
6
+ @class_to_be_violated = class_to_be_violated
7
+ end
8
+
9
+ def violate
10
+ methods_to_violate.each do |original_method_name|
11
+ unviolated_method_name = :"__unviolated_#{original_method_name}"
12
+ @class_to_be_violated.class_eval do
13
+ # First, save the unviolated method.
14
+ alias_method(unviolated_method_name, original_method_name)
15
+
16
+ # # Then, remove the unviolated method. It's still saved via the call to
17
+ # alias_method.
18
+ remove_method(original_method_name)
19
+
20
+ # Now, define a new method with the same name. It returns an OpenStruct with one key,
21
+ # the original method name. So obj.user is now obj.user.user.
22
+ # Pretty sure |*args| is 1.9-only
23
+ define_method(original_method_name) do |*args|
24
+ original_value = __send__(unviolated_method_name, *args)
25
+ OpenStruct.new(original_method_name => original_value)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def methods_to_violate
32
+ @class_to_be_violated.instance_methods(false)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe "the global Violate method" do
4
+ it "violates the given class" do
5
+ define_class("Greeter") do
6
+ def hello
7
+ "hello"
8
+ end
9
+ end
10
+
11
+ Violate(Greeter)
12
+ violated_instance = Greeter.new
13
+
14
+ violated_instance.hello.hello.should == "hello"
15
+ end
16
+ end
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ describe LawOfDemeterViolator::Violator do
4
+ it "violates methods that don't take arguments" do
5
+ define_class("Something") do
6
+ def hello
7
+ "hello"
8
+ end
9
+ end
10
+
11
+ violator = LawOfDemeterViolator::Violator.new(Something)
12
+ violator.violate
13
+
14
+ Something.new.hello.hello.should == "hello"
15
+ end
16
+
17
+ it "violates methods that take arguments" do
18
+ define_class("ClassWithMethodWithArguments") do
19
+ def name_plus(extra_argument)
20
+ "Bob #{extra_argument}"
21
+ end
22
+ end
23
+
24
+ violator = LawOfDemeterViolator::Violator.new(ClassWithMethodWithArguments)
25
+ violator.violate
26
+
27
+ violated_instance = ClassWithMethodWithArguments.new
28
+ violated_instance.name_plus("Viola").name_plus.should == "Bob Viola"
29
+ end
30
+
31
+ it "does not violate methods from parent classes" do
32
+ define_class("Parent") do
33
+ def parent_method
34
+ "parent"
35
+ end
36
+ end
37
+
38
+ define_class("Child", Parent) do
39
+ def child_method
40
+ end
41
+ end
42
+
43
+ violator = LawOfDemeterViolator::Violator.new(Child)
44
+ violator.violate
45
+
46
+ Child.new.parent_method.should == "parent"
47
+ end
48
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe LawOfDemeterViolator do
4
+ it "violates the Law of Demeter hardcore" do
5
+ define_class("ViolatedGreeter") do
6
+ def hello
7
+ "hello"
8
+ end
9
+
10
+ include LawOfDemeterViolator
11
+ end
12
+
13
+ # include_violator_in(Greeter)
14
+ violated_instance = ViolatedGreeter.new
15
+
16
+ violated_instance.hello.hello.should == "hello"
17
+ end
18
+
19
+ def include_violator_in(a_class)
20
+ a_class.send(:include, LawOfDemeterViolator)
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require "rspec"
4
+ require "law_of_demeter_violator"
5
+ require "bourne"
6
+
7
+ Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
8
+
9
+ RSpec.configure do |config|
10
+ config.mock_framework = :mocha
11
+ end
@@ -0,0 +1,43 @@
1
+ module DefineConstantMacros
2
+ def define_class(path, base = Object, &block)
3
+ namespace, class_name = *constant_path(path)
4
+ klass = Class.new(base)
5
+ namespace.const_set(class_name, klass)
6
+ klass.class_eval(&block) if block_given?
7
+ @defined_constants << path
8
+ klass
9
+ end
10
+
11
+ def constant_path(constant_name)
12
+ names = constant_name.split('::')
13
+ class_name = names.pop
14
+ namespace = names.inject(Object) { |result, name| result.const_get(name) }
15
+ [namespace, class_name]
16
+ end
17
+
18
+ def default_constants
19
+ @defined_constants ||= []
20
+ @created_tables ||= []
21
+ end
22
+
23
+ def clear_generated_constants
24
+ @defined_constants.reverse.each do |path|
25
+ namespace, class_name = *constant_path(path)
26
+ namespace.send(:remove_const, class_name)
27
+ end
28
+
29
+ @defined_constants.clear
30
+ end
31
+ end
32
+
33
+ RSpec.configure do |config|
34
+ config.include DefineConstantMacros
35
+
36
+ config.before do
37
+ default_constants
38
+ end
39
+
40
+ config.after do
41
+ clear_generated_constants
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: law_of_demeter_violator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gabe Berke-Williams
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-30 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2154589260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2154589260
25
+ - !ruby/object:Gem::Dependency
26
+ name: bourne
27
+ requirement: &2154581800 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '1.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2154581800
36
+ description: Violate the Law of Demeter hardcore with this too-easy-to-use library.
37
+ email:
38
+ - gabe@thoughtbot.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - law_of_demeter_violator.gemspec
49
+ - lib/law_of_demeter_violator.rb
50
+ - lib/law_of_demeter_violator/global_method.rb
51
+ - lib/law_of_demeter_violator/version.rb
52
+ - lib/law_of_demeter_violator/violator.rb
53
+ - spec/law_of_demeter_violator/global_method_spec.rb
54
+ - spec/law_of_demeter_violator/violator_spec.rb
55
+ - spec/law_of_demeter_violator_spec.rb
56
+ - spec/spec_helper.rb
57
+ - spec/support/macros/define_constant.rb
58
+ homepage: ''
59
+ licenses: []
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 1.8.11
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Violate the Law of Demeter hardcore with this too-easy-to-use library.
82
+ test_files:
83
+ - spec/law_of_demeter_violator/global_method_spec.rb
84
+ - spec/law_of_demeter_violator/violator_spec.rb
85
+ - spec/law_of_demeter_violator_spec.rb
86
+ - spec/spec_helper.rb
87
+ - spec/support/macros/define_constant.rb
88
+ has_rdoc: