object_extender 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +2 -0
- data/.travis.yml +13 -0
- data/README.md +40 -0
- data/Rakefile +8 -0
- data/lib/object_extender.rb +54 -0
- data/lib/object_extender/version.rb +3 -0
- data/object_extender.gemspec +24 -0
- data/test/test_object_extender.rb +113 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
M2M1MWRmMGIwNGI1ODQ5NGE4NGE4NTllOTMxNGNkYjU2YmJkMjRhZg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YTE2ZTJhOTRhZWNmNDY0YzBmNTFiZDUxNWQ0N2IwMjQxZjIzYzE2OA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZTc0M2I1NmYzNzMxMzk5OTRjYmNkNzE1ODJkMWYyYjY4ZmU3YTMyZGI1ZmY1
|
10
|
+
YTIyN2IxNjc0ZjNjNjIxMjQ2M2FjNTJjMjAxYzllZTE3MzdkNzk0ODYwYjM3
|
11
|
+
NWE4MTdkOGUwODA4MDdiZmU1M2MzZWZhNjgxNzFlNWU2NjM4MDE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MTZjNjFmYjZlY2Y5ZjU5N2RiODI4MjY3MDk4N2U1NjBiNzU1ZTc1YmViOTU3
|
14
|
+
YTg4NTZjOGVlNThmNmU4MmFhYWZhMWExYWQ5YmFjNDQ3MmJjOTk1MDE0OWE2
|
15
|
+
MjhkNjNmMmYyMDUyNGMzNWNiMGFjNzdhODg3YTRmMTJjZjljMDM=
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.9.2
|
4
|
+
- 2.2
|
5
|
+
script: rake test
|
6
|
+
|
7
|
+
deploy:
|
8
|
+
provider: rubygems
|
9
|
+
api_key:
|
10
|
+
secure: iPLvtMc8QsgXQ/M9fUcRDuuURC+GWLIAoJvYM5qIPNbMrQi5Q66Vd2j63msycg0cITS6D0YqcepJ8iszy0uQsHaZvz4JFwK+LhA7L9gqFl3T9J6H3BgHsnCcbpI163VsB6rAnIAxX+ifCeWvb9mBwgZ1rLr6x08RsnyqWCxsJXOe9AlmrB3rmRelTA4rJ5GBcNmVxPAl564RZyh+fUDaDwo6oHer4ZVwVvgQZssYMCzljTyWa03wjbl+jPfoWETkdmt66h4i6/hkP2AGXsDeWW8Q2cN2xM0u2nzFkY3KtMtkm8nJKt5RUsUV9djYqWoQLlKVq8Olb8Oc0od8NgVl2IodsSkNAslBO70a38IH9Ch0CQ7kuvdP+aX/TLxbhkZeY79jqe3saQxHZx8uPv6W4OgvlapBd3Q5eqcrRVPx+z7CGy5mLeLLphCj7reEZxNVMCfmVz4f0GpHn3XwlDU/C610avJIHoOHhlLB2qbHh5JXF/SnBP+x0B89UPVdq1aoIoGCkf4Xf16fZKpdbvYz4JqQI62q5iEVZ5TvWwaCs1FdAzgZR0uFQUlaIvhkXOWLITYeWiN2JU2vAv4ukclWfNLxj4R5ue5V3fI+j5Szcib3eUqVHk97bthVZd2fyR86l9wy7YMoclkhZogMDu0Sexd2Y/RkXSNtpBFYBUnfaJM=
|
11
|
+
gemspec: object_extender.gemspec
|
12
|
+
on:
|
13
|
+
branch: master
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Object Extender Gem
|
2
|
+
|
3
|
+
Call class statements with extended object.
|
4
|
+
|
5
|
+
- http://github.com/masuidrive/object_extender_rb
|
6
|
+
- License: MIT
|
7
|
+
- Author: Yuichiro MASUI <masui@masuidrive.jp>
|
8
|
+
|
9
|
+
|
10
|
+
## Getting Started
|
11
|
+
|
12
|
+
```
|
13
|
+
require 'object_extender'
|
14
|
+
|
15
|
+
module AddFooAssoc
|
16
|
+
extend ObjectExtender
|
17
|
+
|
18
|
+
extended_class do
|
19
|
+
attr_accessor :foo
|
20
|
+
end
|
21
|
+
|
22
|
+
extended do
|
23
|
+
self.foo = :Foo
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
obj = Object.new
|
28
|
+
# obj.foo = :Foo => NoMethodError
|
29
|
+
|
30
|
+
obj.extend(AddFooAssoc)
|
31
|
+
obj.foo
|
32
|
+
obj.foo = :Bar
|
33
|
+
|
34
|
+
# Same as
|
35
|
+
obj = Object.new
|
36
|
+
obj.singleton_class.class_eval do
|
37
|
+
attr_accessor :foo
|
38
|
+
end
|
39
|
+
obj.foo = :Bar
|
40
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# This module is `ActiveSupport::Concern` for instance.
|
2
|
+
#
|
3
|
+
# module Foo
|
4
|
+
# extend ObjectExtender
|
5
|
+
# extended_class do
|
6
|
+
# attr_accessor :bar
|
7
|
+
# end
|
8
|
+
# extended do
|
9
|
+
# puts :extended
|
10
|
+
# end
|
11
|
+
# def foo; p @bar; end
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# obj = Object.new.extend(Foo)
|
15
|
+
# obj.bar = :Foo
|
16
|
+
# obj.foo
|
17
|
+
#
|
18
|
+
# References:
|
19
|
+
# - https://github.com/rails/rails/blob/master/activesupport/lib/active_support/concern.rb
|
20
|
+
#
|
21
|
+
require "object_extender/version"
|
22
|
+
|
23
|
+
module ObjectExtender
|
24
|
+
class MultipleExtendedClassBlocks < StandardError #:nodoc:
|
25
|
+
def initialize
|
26
|
+
super "Cannot define multiple 'extended_class' blocks"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class MultipleExtendedBlocks < StandardError #:nodoc:
|
31
|
+
def initialize
|
32
|
+
super "Cannot define multiple 'extended' blocks"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def extended(obj = nil, &block)
|
37
|
+
if obj.nil?
|
38
|
+
fail MultipleExtendedBlocks if instance_variable_defined?(:@_extended_block)
|
39
|
+
@_extended_block = block
|
40
|
+
else
|
41
|
+
if instance_variable_defined?(:@_extended_class_block)
|
42
|
+
obj.singleton_class.class_eval(&@_extended_class_block)
|
43
|
+
end
|
44
|
+
if instance_variable_defined?(:@_extended_block)
|
45
|
+
obj.instance_eval(&@_extended_block)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def extended_class(&block)
|
51
|
+
fail MultipleExtendedClassBlocks if instance_variable_defined?(:@_extended_class_block)
|
52
|
+
@_extended_class_block = block
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'object_extender/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "object_extender"
|
7
|
+
spec.version = ObjectExtender::VERSION
|
8
|
+
spec.authors = ['Yuichiro MASUI']
|
9
|
+
spec.email = 'masui@masuidrive.jp'
|
10
|
+
spec.summary = 'Call class statements with extended object. It\'s without class pollution.'
|
11
|
+
spec.description = 'Call class statements with extended object. It\'s without class pollution.'
|
12
|
+
|
13
|
+
spec.homepage = "https://github.com/masuidrive/object_extender_rb"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.required_ruby_version = '>= 1.9.2'
|
17
|
+
|
18
|
+
spec.files = `git ls-files`.split($/)
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'object_extender'
|
3
|
+
|
4
|
+
class ObjectExtenderTest < Test::Unit::TestCase
|
5
|
+
def test_without_ext
|
6
|
+
obj = Object.new
|
7
|
+
assert_raise NoMethodError do
|
8
|
+
obj.foo
|
9
|
+
end
|
10
|
+
assert_raise NoMethodError do
|
11
|
+
obj.bar
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module WithCallingClassMethod
|
16
|
+
extend ObjectExtender
|
17
|
+
extended_class do
|
18
|
+
attr_accessor :foo
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_with_calling_class_method
|
23
|
+
obj = Object.new
|
24
|
+
assert_equal obj, obj.extend(WithCallingClassMethod)
|
25
|
+
assert_nil obj.foo
|
26
|
+
obj.foo = :Foo
|
27
|
+
assert_equal :Foo, obj.foo
|
28
|
+
|
29
|
+
test_without_ext
|
30
|
+
end
|
31
|
+
|
32
|
+
module WithDefineMethod
|
33
|
+
extend ObjectExtender
|
34
|
+
def bar
|
35
|
+
:Bar
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_with_define_method
|
40
|
+
obj = Object.new
|
41
|
+
assert_equal obj, obj.extend(WithDefineMethod)
|
42
|
+
assert_equal :Bar, obj.bar
|
43
|
+
|
44
|
+
test_without_ext
|
45
|
+
end
|
46
|
+
|
47
|
+
module WithCallingExtended
|
48
|
+
extend ObjectExtender
|
49
|
+
extended do
|
50
|
+
self.foo = :FooFoo
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
module WithCallingExtendedAndExtendedClass
|
55
|
+
extend ObjectExtender
|
56
|
+
extended_class do
|
57
|
+
attr_accessor :foo
|
58
|
+
end
|
59
|
+
extended do
|
60
|
+
self.foo = :FooFooFoo
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_with_extended
|
65
|
+
obj = Object.new
|
66
|
+
assert_equal obj, obj.extend(WithCallingExtendedAndExtendedClass)
|
67
|
+
assert_equal :FooFooFoo, obj.foo
|
68
|
+
|
69
|
+
test_without_ext
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_with_extended_and_extended_class
|
73
|
+
obj = Object.new
|
74
|
+
assert_equal obj, obj.extend(WithCallingClassMethod)
|
75
|
+
assert_equal obj, obj.extend(WithCallingExtended)
|
76
|
+
assert_equal :FooFoo, obj.foo
|
77
|
+
|
78
|
+
test_without_ext
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_with_multiple_extended
|
82
|
+
obj = Object.new
|
83
|
+
assert_equal obj, obj.extend(WithCallingExtendedAndExtendedClass)
|
84
|
+
assert_equal obj, obj.extend(WithCallingExtended)
|
85
|
+
assert_equal :FooFoo, obj.foo
|
86
|
+
|
87
|
+
test_without_ext
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_multiple_extended
|
91
|
+
assert_raises(ObjectExtender::MultipleExtendedBlocks) do
|
92
|
+
Module.new do
|
93
|
+
extend ObjectExtender
|
94
|
+
extended do
|
95
|
+
end
|
96
|
+
extended do
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_multiple_extended_class
|
103
|
+
assert_raises(ObjectExtender::MultipleExtendedClassBlocks) do
|
104
|
+
Module.new do
|
105
|
+
extend ObjectExtender
|
106
|
+
extended_class do
|
107
|
+
end
|
108
|
+
extended_class do
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: object_extender
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuichiro MASUI
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Call class statements with extended object. It's without class pollution.
|
28
|
+
email: masui@masuidrive.jp
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- .travis.yml
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/object_extender.rb
|
38
|
+
- lib/object_extender/version.rb
|
39
|
+
- object_extender.gemspec
|
40
|
+
- test/test_object_extender.rb
|
41
|
+
homepage: https://github.com/masuidrive/object_extender_rb
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.9.2
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.4.5
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Call class statements with extended object. It's without class pollution.
|
65
|
+
test_files:
|
66
|
+
- test/test_object_extender.rb
|