behavioral 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile +1 -2
- data/LICENSE.txt +1 -1
- data/README.md +2 -3
- data/behavioral.gemspec +2 -3
- data/lib/behavioral/version.rb +1 -1
- data/lib/behavioral.rb +6 -4
- data/test/allowed.yml +8 -0
- data/test/behavioral_test.rb +36 -5
- data/test/test_helper.rb +3 -7
- metadata +8 -10
- data/.gitignore +0 -17
- data/.travis.yml +0 -14
- data/Rakefile +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e30262ab7bce075d0bc2600013c5d6921219bba3ca03a158de9916d09a6217d2
|
4
|
+
data.tar.gz: a08f931541338e3acc8f5ae3851df21aa59e3fb60a4dbeb2a5cb98652a8c04fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d2134626b2c672cdd30b8a0d075dc91a1fd18a6cbabb0c19f25ac52dd28639391114c363ac6e0fe6d817fb2150796328ca37ab7d681e1e365eb5a8e6eac38d5
|
7
|
+
data.tar.gz: f0409c019551652a75beb9b7dfecf2a3ebca144173fe90d9ce911089def9118f37c2892a07f1c245160f241c532f4144b6d27c8d83db1be3e89a33dade078e44
|
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# Behavioral
|
2
2
|
|
3
|
-
[![Build Status](https://
|
3
|
+
[![Build Status](https://github.com/saturnflyer/behavioral/actions/workflows/test.yml/badge.svg)](https://github.com/saturnflyer/behavioral/actions)
|
4
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
5
|
[![Gem Version](https://badge.fury.io/rb/behavioral.png)](http://badge.fury.io/rb/behavioral)
|
7
6
|
|
8
7
|
Add behavior to individual objects and remove it later _while preserving the existing behavior_.
|
@@ -40,7 +39,7 @@ person.without_behaviors(Greeter)
|
|
40
39
|
person.hello #=> NoMethodError
|
41
40
|
```
|
42
41
|
|
43
|
-
### This does not alter the
|
42
|
+
### This does not alter the ancestry
|
44
43
|
|
45
44
|
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
45
|
|
data/behavioral.gemspec
CHANGED
@@ -13,8 +13,7 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.homepage = "http://github.com/saturnflyer/behavioral"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
|
-
spec.files =
|
17
|
-
spec.
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
|
+
spec.files = Dir.glob("lib/**/*", File::FNM_DOTMATCH) + %w[ README.md LICENSE.txt behavioral.gemspec Gemfile ]
|
17
|
+
spec.test_files = Dir.glob("test/**/*", File::FNM_DOTMATCH)
|
19
18
|
spec.require_paths = ["lib"]
|
20
19
|
end
|
data/lib/behavioral/version.rb
CHANGED
data/lib/behavioral.rb
CHANGED
@@ -2,18 +2,20 @@ require "behavioral/version"
|
|
2
2
|
|
3
3
|
module Behavioral
|
4
4
|
def with_behaviors(*mods)
|
5
|
-
mods.each do |mod|
|
5
|
+
Array(mods).each do |mod|
|
6
6
|
mod.instance_methods.each do |meth|
|
7
|
-
|
7
|
+
instance_exec(meth, mod.instance_method(meth)){|m, unbound_method|
|
8
|
+
define_singleton_method m, unbound_method
|
9
|
+
}
|
8
10
|
end
|
9
11
|
end
|
10
12
|
self
|
11
13
|
end
|
12
14
|
|
13
15
|
def without_behaviors(*mods)
|
14
|
-
mods.each do |mod|
|
16
|
+
Array(mods).each do |mod|
|
15
17
|
mod.instance_methods.each do |meth|
|
16
|
-
|
18
|
+
singleton_class.send(:remove_method, meth) if singleton_methods.include?(meth)
|
17
19
|
end
|
18
20
|
end
|
19
21
|
self
|
data/test/allowed.yml
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
---
|
2
|
+
- Behavioral#test_0001_gains behaviors and overrides existing methods
|
3
|
+
- Behavioral#test_0002_removes behaviors leaving the previously-existing methods intact
|
4
|
+
- Behavioral#test_0003_reverts added behaviors
|
5
|
+
- Behavioral#test_0004_allows adding multiple behavior modules at once
|
6
|
+
- Behavioral#test_0005_overwrites singleton methods from subsequent behaviors
|
7
|
+
- Behavioral#test_0006_ignores unknown methods when removing behaviors
|
8
|
+
- Behavioral#test_0007_handles super with arguments
|
data/test/behavioral_test.rb
CHANGED
@@ -6,7 +6,7 @@ describe Behavioral do
|
|
6
6
|
person.with_behaviors(Greeter)
|
7
7
|
assert_equal "Hello, I am The Greeter Jim", person.hello
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
it 'removes behaviors leaving the previously-existing methods intact' do
|
11
11
|
person = Person.new('Jim')
|
12
12
|
person.with_behaviors(Greeter)
|
@@ -17,12 +17,20 @@ describe Behavioral do
|
|
17
17
|
end
|
18
18
|
assert_match "undefined method `hello'", error.message
|
19
19
|
end
|
20
|
+
|
21
|
+
it 'reverts added behaviors' do
|
22
|
+
person = Person.new('Jim')
|
23
|
+
person.with_behaviors(Greeter)
|
24
|
+
assert_equal "The Greeter Jim", person.name
|
25
|
+
person.without_behaviors(Greeter)
|
26
|
+
assert_equal "Jim", person.name
|
27
|
+
end
|
20
28
|
|
21
29
|
it 'allows adding multiple behavior modules at once' do
|
22
30
|
person = Person.new('Jim')
|
23
31
|
person.with_behaviors(Greeter, Admin)
|
24
32
|
assert_equal "Hello, I am The Greeter Jim", person.hello
|
25
|
-
|
33
|
+
assert_predicate person, :admin?
|
26
34
|
end
|
27
35
|
|
28
36
|
it 'overwrites singleton methods from subsequent behaviors' do
|
@@ -30,6 +38,21 @@ describe Behavioral do
|
|
30
38
|
person.with_behaviors(Greeter, OtherGreeter)
|
31
39
|
assert_equal "Hi. Call me The Greeter Jim", person.hello
|
32
40
|
end
|
41
|
+
|
42
|
+
it 'ignores unknown methods when removing behaviors' do
|
43
|
+
person = Person.new('Jim')
|
44
|
+
person.without_behaviors(Greeter)
|
45
|
+
err = assert_raises NoMethodError do
|
46
|
+
person.hello
|
47
|
+
end
|
48
|
+
assert_match "undefined method `hello'", err.message
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'handles super with arguments' do
|
52
|
+
person = Person.new('Jim')
|
53
|
+
person.with_behaviors(Greeter)
|
54
|
+
assert_equal "method_with_argument + it works", person.method_with_argument("it works")
|
55
|
+
end
|
33
56
|
end
|
34
57
|
|
35
58
|
|
@@ -39,16 +62,24 @@ class Person
|
|
39
62
|
end
|
40
63
|
attr_reader :name
|
41
64
|
include Behavioral
|
65
|
+
|
66
|
+
def method_with_argument(arg)
|
67
|
+
arg
|
68
|
+
end
|
42
69
|
end
|
43
70
|
|
44
71
|
module Greeter
|
45
72
|
def hello
|
46
|
-
"Hello, I am #{
|
73
|
+
"Hello, I am #{name}"
|
47
74
|
end
|
48
|
-
|
75
|
+
|
49
76
|
def name
|
50
77
|
"The Greeter #{super}"
|
51
78
|
end
|
79
|
+
|
80
|
+
def method_with_argument(arg)
|
81
|
+
"method_with_argument + #{super}"
|
82
|
+
end
|
52
83
|
end
|
53
84
|
|
54
85
|
module Admin
|
@@ -59,6 +90,6 @@ end
|
|
59
90
|
|
60
91
|
module OtherGreeter
|
61
92
|
def hello
|
62
|
-
"Hi. Call me #{
|
93
|
+
"Hi. Call me #{name}"
|
63
94
|
end
|
64
95
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: behavioral
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "'Jim Gay'"
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Add and remove behaviors to individual objects
|
14
14
|
email:
|
@@ -17,22 +17,20 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
-
- ".gitignore"
|
21
|
-
- ".travis.yml"
|
22
20
|
- Gemfile
|
23
21
|
- LICENSE.txt
|
24
22
|
- README.md
|
25
|
-
- Rakefile
|
26
23
|
- behavioral.gemspec
|
27
24
|
- lib/behavioral.rb
|
28
25
|
- lib/behavioral/version.rb
|
26
|
+
- test/allowed.yml
|
29
27
|
- test/behavioral_test.rb
|
30
28
|
- test/test_helper.rb
|
31
29
|
homepage: http://github.com/saturnflyer/behavioral
|
32
30
|
licenses:
|
33
31
|
- MIT
|
34
32
|
metadata: {}
|
35
|
-
post_install_message:
|
33
|
+
post_install_message:
|
36
34
|
rdoc_options: []
|
37
35
|
require_paths:
|
38
36
|
- lib
|
@@ -47,11 +45,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
45
|
- !ruby/object:Gem::Version
|
48
46
|
version: '0'
|
49
47
|
requirements: []
|
50
|
-
|
51
|
-
|
52
|
-
signing_key:
|
48
|
+
rubygems_version: 3.3.17
|
49
|
+
signing_key:
|
53
50
|
specification_version: 4
|
54
51
|
summary: Add and remove behaviors to individual objects
|
55
52
|
test_files:
|
53
|
+
- test/allowed.yml
|
56
54
|
- test/behavioral_test.rb
|
57
55
|
- test/test_helper.rb
|
data/.gitignore
DELETED
data/.travis.yml
DELETED
data/Rakefile
DELETED