Class: Primer
- Inherits:
-
Object
- Object
- Primer
- Defined in:
- lib/primer.rb
Instance Attribute Summary collapse
-
#assign_keys ⇒ Object
Returns the value of attribute assign_keys.
-
#filepath ⇒ Object
Returns the value of attribute filepath.
-
#keys ⇒ Object
Returns the value of attribute keys.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#refresh_rate ⇒ Object
Returns the value of attribute refresh_rate.
-
#session ⇒ Object
Returns the value of attribute session.
-
#storage_mechanism ⇒ Object
Returns the value of attribute storage_mechanism.
Instance Method Summary collapse
- #after(arg, &block) ⇒ Object
- #assign(keys = nil) ⇒ Object
- #before(arg, &block) ⇒ Object
- #decorate(primer_filepath) ⇒ Object
- #expire(val = nil) ⇒ Object
-
#initialize(params = nil) ⇒ Primer
constructor
A new instance of Primer.
- #prime(k = nil) ⇒ Object
- #refresh(rsecs) ⇒ Object
- #refreshes? ⇒ Boolean
- #set(keys = nil, value = nil) ⇒ Object
- #store_in(mech) ⇒ Object
Constructor Details
#initialize(params = nil) ⇒ Primer
Returns a new instance of Primer.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/primer.rb', line 5 def initialize( params=nil ) self.storage_mechanism = :memory self.refresh_rate = nil @before_block = {} @after_block = {} expire(:never) unless params.nil? if params.is_a? Hash if params.has_key?( :session ) self.session = params[:session] end end end end |
Instance Attribute Details
#assign_keys ⇒ Object
Returns the value of attribute assign_keys.
3 4 5 |
# File 'lib/primer.rb', line 3 def assign_keys @assign_keys end |
#filepath ⇒ Object
Returns the value of attribute filepath.
3 4 5 |
# File 'lib/primer.rb', line 3 def filepath @filepath end |
#keys ⇒ Object
Returns the value of attribute keys.
3 4 5 |
# File 'lib/primer.rb', line 3 def keys @keys end |
#mode ⇒ Object
Returns the value of attribute mode.
3 4 5 |
# File 'lib/primer.rb', line 3 def mode @mode end |
#refresh_rate ⇒ Object
Returns the value of attribute refresh_rate.
3 4 5 |
# File 'lib/primer.rb', line 3 def refresh_rate @refresh_rate end |
#session ⇒ Object
Returns the value of attribute session.
3 4 5 |
# File 'lib/primer.rb', line 3 def session @session end |
#storage_mechanism ⇒ Object
Returns the value of attribute storage_mechanism.
3 4 5 |
# File 'lib/primer.rb', line 3 def storage_mechanism @storage_mechanism end |
Instance Method Details
#after(arg, &block) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/primer.rb', line 58 def after( arg, &block ) if arg == :each if block_given? @after_block[:each] = block end elsif arg.is_a? String if self.keys.index( arg ).nil? raise 'key passed as argument to after must be declared in the prime directive' else @after_block[arg] = block end end end |
#assign(keys = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/primer.rb', line 77 def assign( keys = nil ) if keys.nil? self.assign_keys = self.keys elsif _validate_key_arg(keys) unless (Array( keys ) - self.keys).empty? raise 'assigning keys that are not given as arguments to prime is not permitted' end self.assign_keys = Array(keys) end unless mode == :registration yield if block_given? end end |
#before(arg, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/primer.rb', line 44 def before( arg, &block ) if arg == :each if block_given? @before_block[:each] = block end elsif arg.is_a? String if self.keys.index( arg ).nil? raise 'key passed as argument to before must be declared in the prime directive' else @before_block[arg] = block end end end |
#decorate(primer_filepath) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/primer.rb', line 35 def decorate( primer_filepath ) if File.exists?( primer_filepath ) self.instance_eval File.open(primer_filepath, 'rb') { |f| f.read } self.filepath = primer_filepath else raise ArgumentError, "#{primer_filepath} not found" end end |
#expire(val = nil) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/primer.rb', line 28 def expire( val=nil ) unless val.nil? @expire = val end @expire end |
#prime(k = nil) ⇒ Object
72 73 74 75 |
# File 'lib/primer.rb', line 72 def prime( k=nil ) self.keys = Array(k) if _validate_key_arg(k) yield if block_given? end |
#refresh(rsecs) ⇒ Object
20 21 22 |
# File 'lib/primer.rb', line 20 def refresh( rsecs ) self.refresh_rate = rsecs end |
#refreshes? ⇒ Boolean
24 25 26 |
# File 'lib/primer.rb', line 24 def refreshes? ( ! refresh_rate.nil? ) end |
#set(keys = nil, value = nil) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/primer.rb', line 91 def set( keys=nil, value=nil) if value.nil? _set1(keys) else _set2(keys,value) end end |
#store_in(mech) ⇒ Object
99 100 101 |
# File 'lib/primer.rb', line 99 def store_in( mech ) self.storage_mechanism = mech end |