defer 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.
Files changed (4) hide show
  1. checksums.yaml +15 -0
  2. data/lib/defer.rb +1 -0
  3. data/lib/defer/defer.rb +44 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWY4ZjQxOGZkMjhmMDA0ZTI3OTU1YjU3MTA4ZjMwYmMwY2Q0YzBlYg==
5
+ data.tar.gz: !binary |-
6
+ Nzc4YWQxODk2MTFlYmI2N2QyODc2ODhjMTNkZjhhZmM4YTliYjRlMQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZGU1YTZjZGViMGExOTI1NTczNzIxNTc0Y2M1NDE3ZWM5OTJjODJlNGQwNGZm
10
+ Mzc3ZGY1NjdmZjc4ZjI3OWUzYTMwNGY3OTdkZjM4NmViYzY5YzViMjc1YjZm
11
+ YWE1MzUyMDU5OGI0NGE1MDVlOGJjOTgzODM3Y2MzZWU2YjAzY2I=
12
+ data.tar.gz: !binary |-
13
+ MmJjYTQxNDY2YTFkYTAwNGI1ZDhlNDU4Mjg3ZWExZjQ0NDMwNDk4YWI2ZTli
14
+ NjY0MmU4YTdhOWUyNGNhODZhM2FkNzI2MGYwYzI1NDI2ZWEzNmFiYjVlMzU5
15
+ ZGVjZDdkMmRkYThiNDNmYmViOGQyM2E4OGE2MWE0Njk1YWFiYmI=
data/lib/defer.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'defer/defer.rb'
@@ -0,0 +1,44 @@
1
+ # Defers the initialization of an instance until a method is called.
2
+ #
3
+ # Standard Usage:
4
+ #
5
+ # Initialization:
6
+ #
7
+ # instance = Defer.defer {
8
+ # instance.new
9
+ # instance.do_something
10
+ # }
11
+ #
12
+ # Runtime:
13
+ #
14
+ # instance.do_another_thing # starts the initialization here, not above
15
+ class Defer
16
+
17
+ # Initializes defer on a block of code
18
+ def self.defer(&block)
19
+ Defer.new(&block)
20
+ end
21
+
22
+ def initialize(&block)
23
+ @deferred_init = block
24
+ @semaphore = Mutex.new
25
+ end
26
+
27
+ # Gets an instance of the object, calling the deferred method if needed
28
+ def get_instance
29
+ @semaphore.synchronize do
30
+ if @object.nil?
31
+ @object = @deferred_init.call
32
+ else
33
+ @object
34
+ end
35
+ end
36
+ end
37
+
38
+ # After initialization is done, defer passes the arguments through
39
+ # this block to ensure that everything is completed as required
40
+ def method_missing(method, *args, &block)
41
+ object = get_instance
42
+ object.send(method, *args, &block);
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: defer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - manan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ! 'Defers initialization of blocks until they are formally called. '
14
+ email: manan.shah.777@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/defer.rb
20
+ - lib/defer/defer.rb
21
+ homepage:
22
+ licenses:
23
+ - Apache
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.3.0
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Safe object initialization at calling time
45
+ test_files: []