Class: Sadie
- Inherits:
-
Object
- Object
- Sadie
- Defined in:
- lib/sadie/defaults.rb,
lib/sadie.rb,
lib/sadie/version.rb
Overview
Notes
this file sets defaults which can and should be overridden using arguments the sadie constructor
Constant Summary collapse
- VERSION =
"0.0.17"
- DEFAULTS =
{ "sadie.primers_dirpath" => File.("primers","/var/sadie"), "sadie.sessions_dirpath" => File.("sessions","/var/sadie"), "sadie.primer_plugins_dirpath" => ppdp }
Class Method Summary collapse
-
.getCurrentSadieInstance ⇒ Object
method: Sadie::setCurrentSadieInstance.
-
.getSadieInstance(options) ⇒ Object
method: Sadie::getSadieInstance.
-
.iniFileToHash(filepath) ⇒ Object
method: Sadie::iniFileToHash.
-
.prime(primer_definition, &block) ⇒ Object
method: Sadie::Prime.
-
.registerPrimerPlugin(arghash, &block) ⇒ Object
method: Sadie::registerPrimerPlugin.
-
.setCurrentSadieInstance(instance) ⇒ Object
method: Sadie::setCurrentSadieInstance.
- .templatedFileToString(filepath) ⇒ Object
Instance Method Summary collapse
- #addPrimerPluginsDirPath(path) ⇒ Object
- #cheap?(k) ⇒ Boolean
-
#destroy!(key) ⇒ Object
method: destroy!.
-
#destroyOnGet?(key) ⇒ Boolean
method: destroyOnGet?.
-
#expensive?(k) ⇒ Boolean
method: primed?.
-
#get(k) ⇒ Object
method: get.
-
#initialize(options) ⇒ Sadie
constructor
method: constructor options can include any kay, value pairs but the following key values bear mention: REQUIRED.
-
#isset?(key) ⇒ Boolean
method: isset?.
-
#output(k) ⇒ Object
method: output.
- #prime(primer_definition, &block) ⇒ Object
- #registerPrimerPlugin(arghash, &block) ⇒ Object
-
#revert! ⇒ Object
method: revert!.
-
#save ⇒ Object
method: save.
-
#set(k, v) ⇒ Object
method: set alias for setCheap(k,v).
-
#setCheap(k, v) ⇒ Object
method: setCheap.
-
#setDestroyOnGet(key, turnon = true) ⇒ Object
method: setDestroyOnGet.
-
#setExpensive(k, v) ⇒ Object
method: setExpensive.
Constructor Details
#initialize(options) ⇒ Sadie
method: constructor
options can include any kay, value pairs but the following key values bear mention:
REQUIRED
sadie.sessions_dirpath
or
sadie.session_id
or
sadie.session_filepath <- this is probably a bad call, use with caution
and
sadie.primers_dirpath
and
sadie.primer_plugins_dirpath
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/sadie.rb', line 125 def initialize( ) # check instance sanity _checkInstanceSanity _checkClassSanity # internalize defaults to shortterm DEFAULTS.each do |key, value| if key.eql? "sadie.primer_plugins_dirpath" addPrimerPluginsDirPath value else _set( key, value ) end end # internalize supplied defaults, postponing a set of sadie.primers_dirpath # until the end if one is supplied. The reason for this is that the setter # attempts to read the plugins and if the primer plugin dirpath has not # yet been set, then it'll choke if it processes the wrong one first delay_set_primers_dirpath = nil # iterate over constructor args, but do primers_dirpath last since it # causes a call to initializePrimers .each do |key, value| if key.eql? "sadie.primers_dirpath" delay_set_primers_dirpath = value else set( key, value ) end end defined? delay_set_primers_dirpath \ and set( "sadie.primers_dirpath", delay_set_primers_dirpath ) # if a path to a session is given, init using session file if .has_key?( "sadie.session_filepath" ) set( "sadie.session_filepath", ["sadie.session_filepath"] ) _initializeWithSessionFilePath( get("sadie.session_filepath") ) elsif .has_key?( "sadie.session_id" ) set( "sadie.session_id", ["sadie.session_id"] ) _initializeWithSessionId( get( "sadie.session_id" ) ) else set( "sadie.session_id", _generateNewSessionId ) end end |
Class Method Details
.getCurrentSadieInstance ⇒ Object
method: Sadie::setCurrentSadieInstance
called by plugin handlers to get access to the current Sadie instance
44 45 46 |
# File 'lib/sadie.rb', line 44 def self.getCurrentSadieInstance @@current_sadie_instance end |
.getSadieInstance(options) ⇒ Object
method: Sadie::getSadieInstance
returns a new Sadie instance. Options match those of Sadie’s constructor method
29 30 31 |
# File 'lib/sadie.rb', line 29 def self.getSadieInstance( ) Sadie.new() end |
.iniFileToHash(filepath) ⇒ Object
method: Sadie::iniFileToHash
utility class method. accepts a filepath. digests ini file and returns hash of hashes.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/sadie.rb', line 74 def self.iniFileToHash ( filepath ) section = nil ret = Hash.new File.open( filepath, "r" ).each do |f| f.each_line do |line| next if line.match(/^;/) # skip comments if matches = line.match(/\[([^\]]+)\]/) section = matches[1] ret[section] = Hash.new elsif matches = line.match(/^\s*([^\s\=]+)\s*\=\s*([^\s]+)\s*$/) key = matches[1] value = matches[2] # strip quotes if qmatches = value.match(/[\'\"](.*)[\'\"]/) value = qmatches[1] end if defined? section ret[section][key] = value end end end end ret.empty? and return nil return ret end |
.prime(primer_definition, &block) ⇒ Object
method: Sadie::Prime
called my the .res files to register the keys the .res will prime for
accepts as an argument a hash and a block. The hash must include the key: ‘provides’ and it must define an array of keys that the calling resource (.res) file will have provided after the block is evaluated
56 57 58 59 |
# File 'lib/sadie.rb', line 56 def self.prime ( primer_definition, &block ) current_sadie_instance = Sadie::getCurrentSadieInstance current_sadie_instance.prime( primer_definition, &block ) end |
.registerPrimerPlugin(arghash, &block) ⇒ Object
method: Sadie::registerPrimerPlugin
this method is called in the .plugin.rb files to register new plugin types
65 66 67 68 |
# File 'lib/sadie.rb', line 65 def self.registerPrimerPlugin ( arghash, &block ) current_sadie_instance = Sadie::getCurrentSadieInstance current_sadie_instance.registerPrimerPlugin( arghash, &block ) end |
.setCurrentSadieInstance(instance) ⇒ Object
method: Sadie::setCurrentSadieInstance
this is called just prior to calling a primer plugin to handle a primer to provide a current sadie instance for Sadie::getCurrentSadieInstance to return
37 38 39 |
# File 'lib/sadie.rb', line 37 def self.setCurrentSadieInstance ( instance ) @@current_sadie_instance = instance end |
.templatedFileToString(filepath) ⇒ Object
102 103 104 105 106 |
# File 'lib/sadie.rb', line 102 def self.templatedFileToString( filepath ) f = open( filepath ) template = ERB.new( f.read ) template.result end |
Instance Method Details
#addPrimerPluginsDirPath(path) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/sadie.rb', line 173 def addPrimerPluginsDirPath( path ) exppath = File.(path) # add the path to the system load path $LOAD_PATH.include?(exppath) \ or $LOAD_PATH.unshift(exppath) # add the path to the pluginsdir array defined? @plugins_dir_paths \ or @plugins_dir_paths = Array.new @plugins_dir_paths.include?(exppath) \ or @plugins_dir_paths.unshift(exppath) end |
#cheap?(k) ⇒ Boolean
375 376 377 |
# File 'lib/sadie.rb', line 375 def cheap?( k ) ! expensive? ( k ) end |
#destroy!(key) ⇒ Object
method: destroy!
remove the key from sadie
313 314 315 316 |
# File 'lib/sadie.rb', line 313 def destroy! ( key ) unset( key ) primed?( key ) and unprime( key ) end |
#destroyOnGet?(key) ⇒ Boolean
method: destroyOnGet?
returns true if the destructOnGet flag is set for the key
321 322 323 324 325 326 327 328 |
# File 'lib/sadie.rb', line 321 def destroyOnGet?( key ) ( @flag_destroyonget.has_key?( key ) && @flag_destroyonget["#{key}"] ) # @flag_destroyonget.has_key?( key ) \ # or return _newline( false ) # @flag_destroyonget["#{key}"] \ # and return _newline( true ) # return _newline(false) end |
#expensive?(k) ⇒ Boolean
method: primed?
INTERNAL: this method should only be called the the class method, Prime
398 399 400 401 402 403 |
# File 'lib/sadie.rb', line 398 def expensive?( k ) @flag_expensive.has_key?( k ) or return false; @flag_expensive["#{k}"] \ and return true return false end |
#get(k) ⇒ Object
method: get
a standard getter which primes the unprimed and recalls “expensive” facts from files completely behind-the-scenes as directed by the resource (.res) files
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/sadie.rb', line 256 def get( k ) if ! isset?( k ) # prime if not yet primed primed?( k ) or _prime( k ) end return _recallExpensive( k ) if expensive?( k ) # if it's already set, return known answer if isset?( k ) # _get the return value return_value = _get( k ) # unset and unprime if destructOnGet? destroyOnGet?( k ) \ and destroy! k return return_value end end |
#isset?(key) ⇒ Boolean
method: isset?
returns true if sadie has a value for the key
291 292 293 |
# File 'lib/sadie.rb', line 291 def isset?( key ) return @shortterm.has_key?( key ) end |
#output(k) ⇒ Object
method: output
an alias for get. intended for use with primers that produce an output beyond their return value
284 285 286 |
# File 'lib/sadie.rb', line 284 def output( k ) return get( k ) end |
#prime(primer_definition, &block) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/sadie.rb', line 190 def prime( primer_definition, &block ) # validate params defined? primer_definition \ or raise "Prime called without parameters" primer_definition.is_a? Hash \ or raise "Prime called without hash parameters" defined? primer_definition["provides"] \ or raise "Prime called without provides parameter" # if initializing primers, just remember how to get back to the primer later, # otherwise, prime if midPrimerInit? # mid primer init, just memorize primer location memorizePrimerLocation( @@mid_primer_filepath, getCurrentPrimerPluginFilepath, primer_definition["provides"] ) else # run code block with the current sadie instance block.call( self ) # loop thru all primer provides, ensuring each primed current_primer_filepath = getCurrentPrimerFilepath primer_definition["provides"].each do | key | # skip blank lines next if key.match /^\s*$/ #puts "Prime> providing: #{key}" # key primed or raise error primed? key \ or raise "primer definition file: #{current_primer_filepath} was supposed to define #{key}, but did not" end end end |
#registerPrimerPlugin(arghash, &block) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/sadie.rb', line 227 def registerPrimerPlugin ( arghash, &block ) # if mid plugin init is set, we're registering the plugin # init mode, just store arghash info accepts_block = arghash.has_key?( "accepts-block" ) && arghash["accepts-block"] ? true : false prime_on_init = arghash.has_key?( "prime-on-init" ) && arghash["prime-on-init"] ? true : false # if mid plugin init, register the plugin params with the match if midPluginInit? regPluginMatch( arghash["match"], @@mid_plugin_filepath, accepts_block, prime_on_init ) # midplugininit returned false, we're actually in the process of either initializing # a primer or actually priming else yield( self, getCurrentPrimerKeyPrefix, @@current_primer_filepath ) \ end end |
#revert! ⇒ Object
method: revert!
return to last saved state
423 424 425 426 427 428 429 430 431 |
# File 'lib/sadie.rb', line 423 def revert! @shortterm = { "sadie.session_id" => get( "sadie.session_id" ), "sadie.sessions_dirpath" => get( "sadie.sessions_dirpath" ) } _initializeWithSessionId( get( "sadie.session_id" ) ) end |
#save ⇒ Object
method: save
serialize to session file
409 410 411 412 413 414 415 416 417 |
# File 'lib/sadie.rb', line 409 def save session_id = get("sadie.session_id") session_filepath = File.( "session."+session_id, get( "sadie.sessions_dirpath" ) ) serialized_value = Marshal::dump( [ @shortterm, @flag_primed, @flag_expensive ] ) File.open(session_filepath, 'w') { |f| f.write( serialized_value ) } return session_id end |
#set(k, v) ⇒ Object
method: set
alias for setCheap(k,v)
332 333 334 |
# File 'lib/sadie.rb', line 332 def set( k, v ) setCheap( k, v ) end |
#setCheap(k, v) ⇒ Object
method: setCheap
the cheap setter. key, value pairs stored via this method are kept in memory
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/sadie.rb', line 339 def setCheap( k, v ) puts "setCheap( #{k}, #{v} )" if k.eql? "sadie.primer_plugins_dirpath" puts "adding primer plugins dirpath via setCheap" if v.respond_to? "each" v.each do |plugindir| addPrimerPluginsDirPath plugindir end else addPrimerPluginsDirPath v end v = @plugins_dir_paths end # set it, mark not expensive and primed _set( k, v ) _expensive( k, false ) # if we've reset the primers dirpath, init the primers if k.eql?( "sadie.primers_dirpath" ) initializePrimers end _primed( k, true ) # if we've reset the primers dirpath, init the primers if k.eql?( "sadie.primers_dirpath" ) Sadie::setCurrentSadieInstance( self ) end end |
#setDestroyOnGet(key, turnon = true) ⇒ Object
method: setDestroyOnGet
key value will go away and key will be unprimed and unset after next get
NOTE: this doesn't make sense with keys that were set via setExpensive
so it can be set, but nothing's going to happen differently
301 302 303 304 305 306 307 308 |
# File 'lib/sadie.rb', line 301 def setDestroyOnGet( key, turnon=true ) if ( turnon ) @flag_destroyonget["#{key}"] = true return true end @flag_destroyonget.has_key?( key ) \ and @flag_destroyonget.delete( key ) end |
#setExpensive(k, v) ⇒ Object
method: setExpensive
the expensive setter. key, value pairs stored via this method are not kept in memory but are stored to file and recalled as needed
383 384 385 386 387 388 389 390 391 392 |
# File 'lib/sadie.rb', line 383 def setExpensive(k,v) # puts "setting expensive, key: #{k}" expensive_filepath = _computeExpensiveFilepath( k ) serialized_value = Marshal::dump( v ) File.open(expensive_filepath, 'w') { |f| f.write( serialized_value ) } _expensive( k, true ) _primed( k, true ) end |