behavioral 0.0.1

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: f9fa5f5cd6d1cd0b84ea41241e3b949d55b8f85d
4
+ data.tar.gz: a97a23313e96e54677d26ef0ccfdbc067b7e3982
5
+ SHA512:
6
+ metadata.gz: 3d0b8c1a4b7d9adc452505711a90e8481bdea37e2b76b62b3e8d91937766945f5e71f4cda1ebf5d03ca2403f8bdd6e04e1f4bab7c69b11758c480b2616237513
7
+ data.tar.gz: c7ef6230da3303de717c2bff2a7127dad0c23ecebd522ffba8fb9107f0b98c6f500609908f844edbf66e2b01cd81fc2511a91a5a7c1e455e94653a16bce42b48
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
5
+ - ruby-head
6
+ - jruby-head
7
+ - rbx-2
8
+ env:
9
+ - COVERALLS=true
10
+ matrix:
11
+ allow_failures:
12
+ - rvm: ruby-head
13
+ - rvm: jruby-head
14
+ - rvm: rbx-2
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rubinius-coverage', platform: :rbx
7
+ gem 'coveralls', require: false
8
+ gem 'minitest'
9
+ gem 'rake'
10
+ gem 'simplecov'
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 'Jim Gay'
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.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Behavioral
2
+
3
+ [![Build Status](https://travis-ci.org/saturnflyer/behavioral.png?branch=master)](https://travis-ci.org/saturnflyer/behavioral)
4
+ [![Code Climate](https://codeclimate.com/github/saturnflyer/behavioral.png)](https://codeclimate.com/github/saturnflyer/behavioral)
5
+ [![Coverage Status](https://coveralls.io/repos/saturnflyer/behavioral/badge.png)](https://coveralls.io/r/saturnflyer/behavioral)
6
+ [![Gem Version](https://badge.fury.io/rb/behavioral.png)](http://badge.fury.io/rb/behavioral)
7
+
8
+ Add behavior to individual objects and remove it later _while preserving the existing behavior_.
9
+
10
+ This is _similar_ to [Casting](http://rubygems.org/gems/casting) in that it adds and removes behaviors and preserves `self` but it's different in that you can still use `super` inside your methods.
11
+
12
+ ## Usage
13
+
14
+ Add Behavioral to your classes to add new features or override existing ones. Later you may remove your behaviors:
15
+
16
+ ```ruby
17
+ class Person
18
+ def initialize(name)
19
+ @name = name
20
+ end
21
+ attr_reader :name
22
+
23
+ include Behavioral
24
+ end
25
+
26
+ module Greeter
27
+ def hello
28
+ "Hello, I am #{self.name}"
29
+ end
30
+
31
+ def name
32
+ "The Greeter #{super}"
33
+ end
34
+ end
35
+
36
+ person = Person.new('Jim').with_behaviors(Greeter)
37
+ person.hello #=> "Hello, I am Jim"
38
+
39
+ person.without_behaviors(Greeter)
40
+ person.hello #=> NoMethodError
41
+ ```
42
+
43
+ ### This does not alter the anncestry
44
+
45
+ When you add behaviors, the methods are copied to the `singleton_class` of your object. Later, if you ask the object if it is of that type, the answer will be false.
46
+
47
+ ```ruby
48
+ person = Person.new('Jim').with_behaviors(Greeter)
49
+ person.is_a?(Greeter) #=> false
50
+
51
+ #alternative
52
+ person = Person.new('Jim').extend(Greeter)
53
+ person.is_a?(Greeter) #=> true
54
+ ```
55
+
56
+ ## Installation
57
+
58
+ Add this line to your application's Gemfile:
59
+
60
+ gem 'behavioral'
61
+
62
+ And then execute:
63
+
64
+ $ bundle
65
+
66
+ Or install it yourself as:
67
+
68
+ $ gem install behavioral
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.test_files = FileList['test/*_test.rb']
8
+ t.ruby_opts = ["-w"]
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'behavioral/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "behavioral"
8
+ spec.version = Behavioral::VERSION
9
+ spec.authors = ["'Jim Gay'"]
10
+ spec.email = ["jim@saturnflyer.com"]
11
+ spec.summary = %q{Add and remove behaviors to individual objects}
12
+ spec.description = %q{Add and remove behaviors to individual objects}
13
+ spec.homepage = "http://github.com/saturnflyer/behavioral"
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
+ end
data/lib/behavioral.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "behavioral/version"
2
+
3
+ module Behavioral
4
+ def with_behaviors(*mods)
5
+ mods.each do |mod|
6
+ mod.instance_methods.each do |meth|
7
+ self.define_singleton_method(meth, mod.instance_method(meth))
8
+ end
9
+ end
10
+ self
11
+ end
12
+
13
+ def without_behaviors(*mods)
14
+ mods.each do |mod|
15
+ mod.instance_methods.each do |meth|
16
+ self.singleton_class.send(:remove_method, meth)
17
+ end
18
+ end
19
+ self
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Behavioral
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ describe Behavioral do
4
+ it 'gains behaviors and overrides existing methods' do
5
+ person = Person.new('Jim')
6
+ person.with_behaviors(Greeter)
7
+ assert_equal "Hello, I am The Greeter Jim", person.hello
8
+ end
9
+
10
+ it 'removes behaviors leaving the previously-existing methods intact' do
11
+ person = Person.new('Jim')
12
+ person.with_behaviors(Greeter)
13
+ assert_equal "The Greeter Jim", person.name
14
+ person.without_behaviors(Greeter)
15
+ error = assert_raises NoMethodError do
16
+ person.hello
17
+ end
18
+ assert_match "undefined method `hello'", error.message
19
+ end
20
+
21
+ it 'allows adding multiple behavior modules at once' do
22
+ person = Person.new('Jim')
23
+ person.with_behaviors(Greeter, Admin)
24
+ assert_equal "Hello, I am The Greeter Jim", person.hello
25
+ assert person.admin?
26
+ end
27
+
28
+ it 'overwrites singleton methods from subsequent behaviors' do
29
+ person = Person.new('Jim')
30
+ person.with_behaviors(Greeter, OtherGreeter)
31
+ assert_equal "Hi. Call me The Greeter Jim", person.hello
32
+ end
33
+ end
34
+
35
+
36
+ class Person
37
+ def initialize(name)
38
+ @name = name
39
+ end
40
+ attr_reader :name
41
+ include Behavioral
42
+ end
43
+
44
+ module Greeter
45
+ def hello
46
+ "Hello, I am #{self.name}"
47
+ end
48
+
49
+ def name
50
+ "The Greeter #{super}"
51
+ end
52
+ end
53
+
54
+ module Admin
55
+ def admin?
56
+ true
57
+ end
58
+ end
59
+
60
+ module OtherGreeter
61
+ def hello
62
+ "Hi. Call me #{self.name}"
63
+ end
64
+ end
@@ -0,0 +1,12 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter 'test'
4
+ end
5
+
6
+ require 'coveralls'
7
+ if ENV['COVERALLS']
8
+ Coveralls.wear!
9
+ end
10
+
11
+ require 'minitest/autorun'
12
+ require 'behavioral'
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: behavioral
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "'Jim Gay'"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Add and remove behaviors to individual objects
14
+ email:
15
+ - jim@saturnflyer.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".travis.yml"
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - behavioral.gemspec
27
+ - lib/behavioral.rb
28
+ - lib/behavioral/version.rb
29
+ - test/behavioral_test.rb
30
+ - test/test_helper.rb
31
+ homepage: http://github.com/saturnflyer/behavioral
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.2.0
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Add and remove behaviors to individual objects
55
+ test_files:
56
+ - test/behavioral_test.rb
57
+ - test/test_helper.rb