Class: Ruty::Datastructure::Deferred

Inherits:
Object
  • Object
show all
Defined in:
lib/ruty/datastructure.rb

Overview

special class that is used by some ruty tags to provide data for the context that requires calculation or rendering and is optional (for example block.super)

Instance Method Summary collapse

Constructor Details

#initialize(callables = nil) ⇒ Deferred

Returns a new instance of Deferred.


150
151
152
# File 'lib/ruty/datastructure.rb', line 150

def initialize callables=nil
  @callables = callables || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object


162
163
164
# File 'lib/ruty/datastructure.rb', line 162

def method_missing name
  @callables[name].call if @callables.include?(name)
end

Instance Method Details

#add_deferred(name, &block) ⇒ Object


154
155
156
# File 'lib/ruty/datastructure.rb', line 154

def add_deferred name, &block
  @callables[name] = block
end

#pretty_print(q) ⇒ Object

override the pretty print callback function so that we get values instead of just a lot of proc inspect outputs.


168
169
170
171
172
173
174
175
176
177
# File 'lib/ruty/datastructure.rb', line 168

def pretty_print q
  unknown = (Class.new{
    define_method(:inspect) { '?' }
  }).new
  t = {}
  @callables.each do |name, callable|
    t[name] = callable.call rescue unknown
  end
  q.pp_hash(t)
end

#ruty_safe?(name) ⇒ Boolean

Returns:

  • (Boolean)

158
159
160
# File 'lib/ruty/datastructure.rb', line 158

def ruty_safe? name
  @callables.include?(name)
end