is-lazy 0.9.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.
- data/.yardopts +9 -0
- data/lib/is/lazy.rb +166 -0
- metadata +48 -0
data/.yardopts
ADDED
data/lib/is/lazy.rb
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Is
|
4
|
+
|
5
|
+
module Lazy
|
6
|
+
|
7
|
+
VERSION = '0.9.1'
|
8
|
+
|
9
|
+
# Object-placeholder for calculation.
|
10
|
+
class Value < BasicObject
|
11
|
+
|
12
|
+
def initialize mode, *args, &block
|
13
|
+
case mode
|
14
|
+
when :lambda
|
15
|
+
@args = args
|
16
|
+
@lambda = lambda &block
|
17
|
+
@mode = :lambda
|
18
|
+
when :thread
|
19
|
+
@thread = ::Thread.start *args, &block
|
20
|
+
@mode = :thread
|
21
|
+
else
|
22
|
+
@value = block.call *args
|
23
|
+
@mode = :value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Calculate result value (or wait for thread) and return it.
|
28
|
+
# @return [Object] calculated value.
|
29
|
+
def value
|
30
|
+
case @mode
|
31
|
+
when :lambda
|
32
|
+
@value = @lambda.call *@args
|
33
|
+
@lambda = nil
|
34
|
+
@args = nil
|
35
|
+
when :thread
|
36
|
+
@value = @thread.value
|
37
|
+
@thread = nil
|
38
|
+
end
|
39
|
+
@mode = :value
|
40
|
+
@value
|
41
|
+
end
|
42
|
+
|
43
|
+
# Stop the calculation and return value immediatelly.
|
44
|
+
# If calculation was finished before, returns real value, else nil.
|
45
|
+
# @return [Object, nil] calculated value if it's present.
|
46
|
+
def stop
|
47
|
+
case @mode
|
48
|
+
when :lambda
|
49
|
+
@args = nil
|
50
|
+
@lambda = nil
|
51
|
+
@value = nil
|
52
|
+
when :thread
|
53
|
+
if @thread.status == false
|
54
|
+
@value = @thread.value
|
55
|
+
else
|
56
|
+
@thread.terminate
|
57
|
+
@value = nil
|
58
|
+
end
|
59
|
+
@thread = nil
|
60
|
+
end
|
61
|
+
@mode = :value
|
62
|
+
@value
|
63
|
+
end
|
64
|
+
|
65
|
+
# Pass all unknown method calls to value.
|
66
|
+
def method_missing sym, *args, &block
|
67
|
+
self.value.send sym, *args, &block
|
68
|
+
end
|
69
|
+
|
70
|
+
# Calculate the value and return it's logical not.
|
71
|
+
# @return [Boolean] true if value is nil or false, false elsewhere.
|
72
|
+
def !
|
73
|
+
! self.value
|
74
|
+
end
|
75
|
+
|
76
|
+
# Calculate and return the value.
|
77
|
+
# This is a shortest expression with value.
|
78
|
+
# @return [Object] calculated value.
|
79
|
+
def +@
|
80
|
+
self.value
|
81
|
+
end
|
82
|
+
|
83
|
+
# Calculate value and compare it with other object.
|
84
|
+
# @param other [Object]
|
85
|
+
# @return [Boolean]
|
86
|
+
def == other
|
87
|
+
self.value == other
|
88
|
+
end
|
89
|
+
|
90
|
+
# Calculate value and compare it with other object.
|
91
|
+
# @param other [Object]
|
92
|
+
# @return [Boolean]
|
93
|
+
def != other
|
94
|
+
self.value != other
|
95
|
+
end
|
96
|
+
|
97
|
+
# Calculate value and compare it with other object.
|
98
|
+
# @param other [Object]
|
99
|
+
# @return [Boolean]
|
100
|
+
def equal? other
|
101
|
+
self.value.equal? other
|
102
|
+
end
|
103
|
+
|
104
|
+
# Calculate value and convert it to boolean.
|
105
|
+
# @return [Boolean] false if value is false or nil, true elsewhere.
|
106
|
+
def true?
|
107
|
+
! ! self.value
|
108
|
+
end
|
109
|
+
|
110
|
+
alias to_b true?
|
111
|
+
|
112
|
+
# Used by interpreter for implicit convertion to String.
|
113
|
+
# @return [String, nil]
|
114
|
+
def to_str
|
115
|
+
if self.value.respond_to? :to_str
|
116
|
+
self.value.to_str
|
117
|
+
else
|
118
|
+
self.value.to_s
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Used by interpreter for implicit convertion to Array.
|
123
|
+
# @return [Array, nil]
|
124
|
+
def to_ary
|
125
|
+
self.value.to_ary
|
126
|
+
end
|
127
|
+
|
128
|
+
# Used by interpreter for implicit convertion to Proc.
|
129
|
+
# @return [Proc, nil]
|
130
|
+
def to_proc
|
131
|
+
self.value.to_proc
|
132
|
+
end
|
133
|
+
|
134
|
+
# Used by interpreter for implicit convertion to Regexp.
|
135
|
+
# @return [Regexp, nil]
|
136
|
+
def to_regexp
|
137
|
+
self.value.to_regexp
|
138
|
+
end
|
139
|
+
|
140
|
+
# Used by interpreter for implicit convertion to Symbol.
|
141
|
+
# @return [Symbol, nil]
|
142
|
+
def to_sym
|
143
|
+
self.value.to_sym
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
# Create lazy calculated object.
|
149
|
+
# @yield [*args] Calculation of value.
|
150
|
+
# @return [Value]
|
151
|
+
def lazy *args, &block
|
152
|
+
Value.new :lambda, *args, &block
|
153
|
+
end
|
154
|
+
|
155
|
+
# Create thread calculated object.
|
156
|
+
# @yield [*args] Calculation of value.
|
157
|
+
# @return [Value]
|
158
|
+
def go *args, &block
|
159
|
+
Value.new :thread, *args, &block
|
160
|
+
end
|
161
|
+
|
162
|
+
module_function :lazy, :go
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: is-lazy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ivan Shikhalev
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Provides an object-placeholder for lazy and threaded calculations.
|
15
|
+
email: shikhalev@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/is/lazy.rb
|
21
|
+
- .yardopts
|
22
|
+
homepage: https://github.com/shikhalev/gems/
|
23
|
+
licenses:
|
24
|
+
- GNU LGPL
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.9.2
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.25
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Provides an object-placeholder for lazy and threaded calculations
|
47
|
+
test_files: []
|
48
|
+
has_rdoc:
|