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 19 20 21 |
# 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 if params.has_key?( :default_storage_mechanism ) self.storage_mechanism = params[:default_storage_mechanism] 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
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/primer.rb', line 61 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
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/primer.rb', line 80 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
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/primer.rb', line 47 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
38 39 40 41 42 43 44 45 |
# File 'lib/primer.rb', line 38 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
31 32 33 34 35 36 |
# File 'lib/primer.rb', line 31 def expire( val=nil ) unless val.nil? @expire = val end @expire end |
#prime(k = nil) ⇒ Object
75 76 77 78 |
# File 'lib/primer.rb', line 75 def prime( k=nil ) self.keys = Array(k) if _validate_key_arg(k) yield if block_given? end |
#refresh(rsecs) ⇒ Object
23 24 25 |
# File 'lib/primer.rb', line 23 def refresh( rsecs ) self.refresh_rate = rsecs end |
#refreshes? ⇒ Boolean
27 28 29 |
# File 'lib/primer.rb', line 27 def refreshes? ( ! refresh_rate.nil? ) end |
#set(keys = nil, value = nil) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/primer.rb', line 94 def set( keys=nil, value=nil) if value.nil? _set1(keys) else _set2(keys,value) end end |
#store_in(mech) ⇒ Object
102 103 104 |
# File 'lib/primer.rb', line 102 def store_in( mech ) self.storage_mechanism = mech end |