Class: Is::Lazy::Value

Inherits:
BasicObject
Defined in:
lib/is/lazy.rb

Overview

Object-placeholder for calculation.

Instance Method Summary collapse

Constructor Details

#initialize(mode, *args, &block) ⇒ Value

Returns a new instance of Value.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/is/lazy.rb', line 12

def initialize mode, *args, &block
  case mode
  when :lambda
    @args = args
    @lambda = lambda &block
    @mode = :lambda
  when :thread
    @thread = ::Thread.start *args, &block
    @mode = :thread
  else
    @value = block.call *args
    @mode = :value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Pass all unknown method calls to value.



66
67
68
# File 'lib/is/lazy.rb', line 66

def method_missing sym, *args, &block
  self.value.send sym, *args, &block
end

Instance Method Details

#!Boolean

Calculate the value and return it’s logical not.

Returns:

  • (Boolean)

    true if value is nil or false, false elsewhere.



72
73
74
# File 'lib/is/lazy.rb', line 72

def !
  ! self.value
end

#!=(other) ⇒ Boolean

Calculate value and compare it with other object.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


93
94
95
# File 'lib/is/lazy.rb', line 93

def != other
  self.value != other
end

#+@Object

Calculate and return the value. This is a shortest expression with value.

Returns:

  • (Object)

    calculated value.



79
80
81
# File 'lib/is/lazy.rb', line 79

def +@
  self.value
end

#==(other) ⇒ Boolean

Calculate value and compare it with other object.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


86
87
88
# File 'lib/is/lazy.rb', line 86

def == other
  self.value == other
end

#equal?(other) ⇒ Boolean

Calculate value and compare it with other object.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


100
101
102
# File 'lib/is/lazy.rb', line 100

def equal? other
  self.value.equal? other
end

#stopObject?

Stop the calculation and return value immediatelly. If calculation was finished before, returns real value, else nil.

Returns:

  • (Object, nil)

    calculated value if it’s present.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/is/lazy.rb', line 46

def stop
  case @mode
  when :lambda
    @args = nil
    @lambda = nil
    @value = nil
  when :thread
    if @thread.status == false
      @value = @thread.value
    else
      @thread.terminate
      @value = nil
    end
    @thread = nil
  end
  @mode = :value
  @value
end

#to_aryArray?

Used by interpreter for implicit convertion to Array.

Returns:

  • (Array, nil)


124
125
126
# File 'lib/is/lazy.rb', line 124

def to_ary
  self.value.to_ary
end

#to_procProc?

Used by interpreter for implicit convertion to Proc.

Returns:

  • (Proc, nil)


130
131
132
# File 'lib/is/lazy.rb', line 130

def to_proc
  self.value.to_proc
end

#to_regexpRegexp?

Used by interpreter for implicit convertion to Regexp.

Returns:

  • (Regexp, nil)


136
137
138
# File 'lib/is/lazy.rb', line 136

def to_regexp
  self.value.to_regexp
end

#to_strString?

Used by interpreter for implicit convertion to String.

Returns:

  • (String, nil)


114
115
116
117
118
119
120
# File 'lib/is/lazy.rb', line 114

def to_str
  if self.value.respond_to? :to_str
    self.value.to_str
  else
    self.value.to_s
  end
end

#to_symSymbol?

Used by interpreter for implicit convertion to Symbol.

Returns:

  • (Symbol, nil)


142
143
144
# File 'lib/is/lazy.rb', line 142

def to_sym
  self.value.to_sym
end

#true?Boolean Also known as: to_b

Calculate value and convert it to boolean.

Returns:

  • (Boolean)

    false if value is false or nil, true elsewhere.



106
107
108
# File 'lib/is/lazy.rb', line 106

def true?
  ! ! self.value
end

#valueObject

Calculate result value (or wait for thread) and return it.

Returns:

  • (Object)

    calculated value.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/is/lazy.rb', line 29

def value
  case @mode
  when :lambda
    @value = @lambda.call *@args
    @lambda = nil
    @args = nil
  when :thread
    @value = @thread.value
    @thread = nil
  end
  @mode = :value
  @value
end