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 +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +14 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +22 -0
- data/README.md +76 -0
- data/Rakefile +12 -0
- data/behavioral.gemspec +20 -0
- data/lib/behavioral.rb +21 -0
- data/lib/behavioral/version.rb +3 -0
- data/test/behavioral_test.rb +64 -0
- data/test/test_helper.rb +12 -0
- metadata +57 -0
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
data/.travis.yml
ADDED
data/Gemfile
ADDED
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
|
+
[](https://travis-ci.org/saturnflyer/behavioral)
|
4
|
+
[](https://codeclimate.com/github/saturnflyer/behavioral)
|
5
|
+
[](https://coveralls.io/r/saturnflyer/behavioral)
|
6
|
+
[](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
data/behavioral.gemspec
ADDED
@@ -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,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
|
data/test/test_helper.rb
ADDED
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
|