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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f9fa5f5cd6d1cd0b84ea41241e3b949d55b8f85d
4
- data.tar.gz: a97a23313e96e54677d26ef0ccfdbc067b7e3982
2
+ SHA256:
3
+ metadata.gz: e30262ab7bce075d0bc2600013c5d6921219bba3ca03a158de9916d09a6217d2
4
+ data.tar.gz: a08f931541338e3acc8f5ae3851df21aa59e3fb60a4dbeb2a5cb98652a8c04fa
5
5
  SHA512:
6
- metadata.gz: 3d0b8c1a4b7d9adc452505711a90e8481bdea37e2b76b62b3e8d91937766945f5e71f4cda1ebf5d03ca2403f8bdd6e04e1f4bab7c69b11758c480b2616237513
7
- data.tar.gz: c7ef6230da3303de717c2bff2a7127dad0c23ecebd522ffba8fb9107f0b98c6f500609908f844edbf66e2b01cd81fc2511a91a5a7c1e455e94653a16bce42b48
6
+ metadata.gz: 4d2134626b2c672cdd30b8a0d075dc91a1fd18a6cbabb0c19f25ac52dd28639391114c363ac6e0fe6d817fb2150796328ca37ab7d681e1e365eb5a8e6eac38d5
7
+ data.tar.gz: f0409c019551652a75beb9b7dfecf2a3ebca144173fe90d9ce911089def9118f37c2892a07f1c245160f241c532f4144b6d27c8d83db1be3e89a33dade078e44
data/Gemfile CHANGED
@@ -3,9 +3,8 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  group :test do
6
- gem 'rubinius-coverage', platform: :rbx
7
- gem 'coveralls', require: false
8
6
  gem 'minitest'
7
+ gem 'minitest-allow'
9
8
  gem 'rake'
10
9
  gem 'simplecov'
11
10
  end
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 'Jim Gay'
1
+ Copyright (c) 2014-2022 'Jim Gay'
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # Behavioral
2
2
 
3
- [![Build Status](https://travis-ci.org/saturnflyer/behavioral.png?branch=master)](https://travis-ci.org/saturnflyer/behavioral)
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 anncestry
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 = `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)/})
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
@@ -1,3 +1,3 @@
1
1
  module Behavioral
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
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
- self.define_singleton_method(meth, mod.instance_method(meth))
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
- self.singleton_class.send(:remove_method, meth)
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
@@ -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
- assert person.admin?
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 #{self.name}"
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 #{self.name}"
93
+ "Hi. Call me #{name}"
63
94
  end
64
95
  end
data/test/test_helper.rb CHANGED
@@ -1,12 +1,8 @@
1
1
  require 'simplecov'
2
+ require 'minitest/autorun'
3
+
2
4
  SimpleCov.start do
3
5
  add_filter 'test'
4
6
  end
5
7
 
6
- require 'coveralls'
7
- if ENV['COVERALLS']
8
- Coveralls.wear!
9
- end
10
-
11
- require 'minitest/autorun'
12
- require 'behavioral'
8
+ require 'behavioral'
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.1
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: 2014-03-20 00:00:00.000000000 Z
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
- rubyforge_project:
51
- rubygems_version: 2.2.0
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
@@ -1,17 +0,0 @@
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 DELETED
@@ -1,14 +0,0 @@
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/Rakefile DELETED
@@ -1,12 +0,0 @@
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