Class: Scribbler::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/scribble-client/scribbler.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Context

Can optionally be initialized/reset with starting context data supplied via a block



93
94
95
96
97
98
# File 'lib/scribble-client/scribbler.rb', line 93

def initialize(&block)
  @contexts   = Hash.new()
  @init_proc  = block
  
  reset!
end

Instance Method Details

#add(hash) ⇒ Object

Merges the supplied key-value pairs into the context



102
103
104
105
106
# File 'lib/scribble-client/scribbler.rb', line 102

def add(hash)
  hash.each_pair { |k,v|
    @contexts[k] = v
  }
end

#apply_contextObject



113
114
115
116
117
118
119
# File 'lib/scribble-client/scribbler.rb', line 113

def apply_context()
  ret = {}
  @contexts.each_pair { |k,v| 
    ret[k] = (v.is_a? Proc) ? (v.call self) : v 
  }
  ret.empty? ? nil : ret
end

#empty?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/scribble-client/scribbler.rb', line 121

def empty?
  @contexts.empty?
end

#reset!Object



108
109
110
111
# File 'lib/scribble-client/scribbler.rb', line 108

def reset!
  @contexts.clear
  add(@init_proc.call) if(@init_proc)
end