pre_and_post_initialize 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +0 -0
- data/README.md +58 -0
- data/lib/pre_and_post_initialize/class_instance.rb +36 -0
- data/lib/pre_and_post_initialize/object_instance.rb +46 -0
- data/lib/pre_and_post_initialize.rb +25 -0
- data/spec/pre_and_post_initialize_spec.rb +31 -0
- metadata +51 -0
data/CHANGELOG.md
ADDED
File without changes
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Pre-And-Post-Initialize #
|
2
|
+
|
3
|
+
http://rubygems.org/gems/pre_and_post_initialize
|
4
|
+
|
5
|
+
# Summary #
|
6
|
+
|
7
|
+
Creates event chain prior to object initialization.
|
8
|
+
|
9
|
+
# Description #
|
10
|
+
|
11
|
+
Adds Object#pre\_initialize and Object#post\_initialize so that events can occur before and after Object#initialize chain.
|
12
|
+
|
13
|
+
# Install #
|
14
|
+
|
15
|
+
* sudo gem install pre\_and\_post\_initialize
|
16
|
+
|
17
|
+
# Usage #
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class AnyClass
|
21
|
+
|
22
|
+
extend PreAndPostInitialize
|
23
|
+
|
24
|
+
def pre_initialize( *initialize_args, & initialize_block )
|
25
|
+
# anything here will happen before #initialize
|
26
|
+
end
|
27
|
+
|
28
|
+
def post_initialize( *initialize_args, & initialize_block )
|
29
|
+
# anything here will happen after #initialize
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
# License #
|
36
|
+
|
37
|
+
(The MIT License)
|
38
|
+
|
39
|
+
Copyright (c) 2013 Ridiculous Power, Asher
|
40
|
+
|
41
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
42
|
+
a copy of this software and associated documentation files (the
|
43
|
+
'Software'), to deal in the Software without restriction, including
|
44
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
45
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
46
|
+
permit persons to whom the Software is furnished to do so, subject to
|
47
|
+
the following conditions:
|
48
|
+
|
49
|
+
The above copyright notice and this permission notice shall be
|
50
|
+
included in all copies or substantial portions of the Software.
|
51
|
+
|
52
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
53
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
54
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
55
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
56
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
57
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
58
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
###
|
4
|
+
# Adds calls to #pre_initialize and #post_initialize before and after call to #initialize.
|
5
|
+
#
|
6
|
+
module ::PreAndPostInitialize::ClassInstance
|
7
|
+
|
8
|
+
#########
|
9
|
+
# new #
|
10
|
+
#########
|
11
|
+
|
12
|
+
###
|
13
|
+
# Adds calls to #pre_initialize and #post_initialize before and after call to #initialize.
|
14
|
+
#
|
15
|
+
# @overload initialize( any_arg, ... )
|
16
|
+
#
|
17
|
+
# @param [Object] any_arg
|
18
|
+
#
|
19
|
+
# Any arguments can be used for initialize.
|
20
|
+
# No arguments are expected here, but any will be passed to super.
|
21
|
+
#
|
22
|
+
def new( *args, & block )
|
23
|
+
|
24
|
+
instance = allocate
|
25
|
+
|
26
|
+
instance.instance_eval do
|
27
|
+
pre_initialize( *args, & block )
|
28
|
+
initialize( *args, & block )
|
29
|
+
post_initialize( *args, & block )
|
30
|
+
end
|
31
|
+
|
32
|
+
return instance
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
###
|
4
|
+
# Adds #pre_initialize and #post_initialize before and after call to #initialize.
|
5
|
+
#
|
6
|
+
module ::PreAndPostInitialize::ObjectInstance
|
7
|
+
|
8
|
+
####################
|
9
|
+
# pre_initialize #
|
10
|
+
####################
|
11
|
+
|
12
|
+
###
|
13
|
+
# Permits activity to be defined prior to any initialization activity. This can be useful
|
14
|
+
# if initialization activity may depend on a particular feature, such as instance registration,
|
15
|
+
# prior to activity occuring that queries or otherwise implicates its identity/definition.
|
16
|
+
#
|
17
|
+
# @overload pre_initialize( any_arg, ... )
|
18
|
+
#
|
19
|
+
# @param [Object] any_arg
|
20
|
+
#
|
21
|
+
# Any arguments can be used for pre_initialize.
|
22
|
+
# No arguments are expected here, but any will be passed to super.
|
23
|
+
#
|
24
|
+
def pre_initialize( *args, & block )
|
25
|
+
# nothing here - subclasses define
|
26
|
+
end
|
27
|
+
|
28
|
+
#####################
|
29
|
+
# post_initialize #
|
30
|
+
#####################
|
31
|
+
|
32
|
+
###
|
33
|
+
# Permits activity to be defined after all initialization activity.
|
34
|
+
#
|
35
|
+
# @overload post_initialize( any_arg, ... )
|
36
|
+
#
|
37
|
+
# @param [Object] any_arg
|
38
|
+
#
|
39
|
+
# Any arguments can be used for post_initialize.
|
40
|
+
# No arguments are expected here, but any will be passed to super.
|
41
|
+
#
|
42
|
+
def post_initialize( *args, & block )
|
43
|
+
# nothing here - subclasses define
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
###
|
4
|
+
# Adds calls to #pre_initialize and #post_initialize before and after call to #initialize.
|
5
|
+
#
|
6
|
+
module ::PreAndPostInitialize
|
7
|
+
|
8
|
+
require_relative 'pre_and_post_initialize/class_instance.rb'
|
9
|
+
require_relative 'pre_and_post_initialize/object_instance.rb'
|
10
|
+
|
11
|
+
include ::PreAndPostInitialize::ClassInstance
|
12
|
+
|
13
|
+
###################
|
14
|
+
# self.extended #
|
15
|
+
###################
|
16
|
+
|
17
|
+
def self.extended( instance )
|
18
|
+
|
19
|
+
super if defined?( super )
|
20
|
+
|
21
|
+
instance.class_eval { include( ::PreAndPostInitialize::ObjectInstance ) }
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
require_relative '../lib/pre_and_post_initialize.rb'
|
3
|
+
|
4
|
+
describe ::PreAndPostInitialize do
|
5
|
+
|
6
|
+
class PreAndPostInitializeTestClass
|
7
|
+
extend PreAndPostInitialize
|
8
|
+
attr_accessor :pre_init_arg, :init_arg, :post_init_arg
|
9
|
+
def pre_initialize
|
10
|
+
@pre_init_arg = :pre
|
11
|
+
@init_arg = :pre
|
12
|
+
@post_init_arg = :pre
|
13
|
+
end
|
14
|
+
def initialize
|
15
|
+
@init_arg = :init
|
16
|
+
@post_init_arg = :init
|
17
|
+
end
|
18
|
+
def post_initialize
|
19
|
+
@post_init_arg = :post
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
let( :instance ) { ::PreAndPostInitializeTestClass.new }
|
24
|
+
|
25
|
+
it 'should run #pre_initialize, #initialize, #post_initialize' do
|
26
|
+
instance.pre_init_arg.should == :pre
|
27
|
+
instance.init_arg.should == :init
|
28
|
+
instance.post_init_arg.should == :post
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pre_and_post_initialize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Asher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Adds Object#pre_initialize so that events can occur before and after
|
15
|
+
Object#initialize chain.
|
16
|
+
email: asher@ridiculouspower.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/pre_and_post_initialize/class_instance.rb
|
22
|
+
- lib/pre_and_post_initialize/object_instance.rb
|
23
|
+
- lib/pre_and_post_initialize.rb
|
24
|
+
- spec/pre_and_post_initialize_spec.rb
|
25
|
+
- README.md
|
26
|
+
- CHANGELOG.md
|
27
|
+
homepage: http://rubygems.org/gems/pre_initialize
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 1.9.1
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project: pre_and_post_initialize
|
47
|
+
rubygems_version: 1.8.23
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: Creates event chain before and after object initialization.
|
51
|
+
test_files: []
|