Class: Hibiki::Derived

Inherits:
Object
  • Object
show all
Includes:
Observer, Trackable
Defined in:
lib/hibiki/derived.rb

Overview

---- derived / computed -----------------------------------------------------

Instance Method Summary collapse

Methods included from Observer

#add_source, #clear_sources, #sources

Methods included from Trackable

#notify, #register_dependency, #subscribers, #unsubscribe

Constructor Details

#initialize(&block) ⇒ Derived

Returns a new instance of Derived.



9
10
11
12
# File 'lib/hibiki/derived.rb', line 9

def initialize(&block)
  @block = block
  @dirty = true
end

Instance Method Details

#callObject

Solid signals are getter functions; sig.() reads (and registers).



28
# File 'lib/hibiki/derived.rb', line 28

def call = value

#inspectObject



38
# File 'lib/hibiki/derived.rb', line 38

def inspect = "#<Hibiki::Derived #{@dirty ? 'dirty' : @value.inspect}>"

#invalidateObject



30
31
32
33
34
35
# File 'lib/hibiki/derived.rb', line 30

def invalidate
  return if @dirty

  @dirty = true
  notify # propagate dirtiness downstream (derived-of-derived, effects)
end

#peekObject

Read without subscribing the reader. A dirty derived still recomputes (collecting its own deps as usual) — peek only skips the outward edge.



22
23
24
25
# File 'lib/hibiki/derived.rb', line 22

def peek
  recompute if @dirty
  @value
end

#to_sObject



37
# File 'lib/hibiki/derived.rb', line 37

def to_s = value.to_s

#valueObject



14
15
16
17
18
# File 'lib/hibiki/derived.rb', line 14

def value
  recompute if @dirty
  register_dependency
  @value
end