Class: Hobix::DataMarsh
- Inherits:
-
DBM
- Object
- DBM
- Hobix::DataMarsh
- Defined in:
- lib/hobix/datamarsh.rb
Constant Summary collapse
- VERSION =
"0.1"
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #delete(key) ⇒ Object
- #delete_if ⇒ Object
- #each_pair ⇒ Object (also: #each)
- #each_value ⇒ Object
- #fetch(keystr, ifnone = nil) ⇒ Object
- #has_value?(val) ⇒ Boolean
- #index(keystr) ⇒ Object
- #invert ⇒ Object
- #reject ⇒ Object
- #replace(hsh) ⇒ Object
- #select(*keys) ⇒ Object
- #shift ⇒ Object
- #store(key, val) ⇒ Object
- #to_a ⇒ Object
- #to_hash ⇒ Object
- #update(hsh) ⇒ Object
- #values ⇒ Object
- #values_at(*keys) ⇒ Object
Instance Method Details
#[](key) ⇒ Object
10 11 12 |
# File 'lib/hobix/datamarsh.rb', line 10 def []( key ) fetch( key ) end |
#[]=(key, val) ⇒ Object
13 14 15 |
# File 'lib/hobix/datamarsh.rb', line 13 def []=( key, val ) store( key, val ) end |
#delete(key) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/hobix/datamarsh.rb', line 34 def delete( key ) v = super( key ) if String === v v = Marshal::load( v ) end v end |
#delete_if ⇒ Object
41 42 43 44 45 46 |
# File 'lib/hobix/datamarsh.rb', line 41 def delete_if del_keys = keys.dup del_keys.delete_if { |k| yield( k, fetch( k ) ) == false } del_keys.each { |k| delete( k ) } self end |
#each_pair ⇒ Object Also known as: each
51 52 53 54 |
# File 'lib/hobix/datamarsh.rb', line 51 def each_pair keys.each { |k| yield k, fetch( k ) } self end |
#each_value ⇒ Object
55 56 57 58 |
# File 'lib/hobix/datamarsh.rb', line 55 def each_value super { |v| yield Marshal::load( v ) } self end |
#fetch(keystr, ifnone = nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/hobix/datamarsh.rb', line 16 def fetch( keystr, ifnone = nil ) begin val = super( keystr ) return Marshal::load( val ) if String === val rescue IndexError end if block_given? yield keystr else ifnone end end |
#has_value?(val) ⇒ Boolean
62 63 64 65 |
# File 'lib/hobix/datamarsh.rb', line 62 def has_value?( val ) each_value { |v| return true if v == val } return false end |
#index(keystr) ⇒ Object
28 29 30 |
# File 'lib/hobix/datamarsh.rb', line 28 def index( keystr ) super( keystr.to_yaml ) end |
#invert ⇒ Object
66 67 68 69 70 |
# File 'lib/hobix/datamarsh.rb', line 66 def invert h = {} keys.each { |k| h[ self.fetch( k ) ] = k } h end |
#reject ⇒ Object
47 48 49 50 |
# File 'lib/hobix/datamarsh.rb', line 47 def reject hsh = self.to_hash hsh.reject { |k,v| yield k, v } end |
#replace(hsh) ⇒ Object
71 72 73 74 |
# File 'lib/hobix/datamarsh.rb', line 71 def replace( hsh ) clear update( hsh ) end |
#select(*keys) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/hobix/datamarsh.rb', line 80 def select( *keys ) if block_given? self.keys.collect { |k| v = self[k]; [k, v] if yield k, v }.compact else values_at( *keys ) end end |
#shift ⇒ Object
75 76 77 78 79 |
# File 'lib/hobix/datamarsh.rb', line 75 def shift a = super a[1] = Marshal::load( a[1] ) if a a end |
#store(key, val) ⇒ Object
87 88 89 90 |
# File 'lib/hobix/datamarsh.rb', line 87 def store( key, val ) super( key, val.to_yaml ) val end |
#to_a ⇒ Object
97 98 99 100 101 |
# File 'lib/hobix/datamarsh.rb', line 97 def to_a a = [] keys.each { |k| a.push [ k, self.fetch( k ) ] } a end |
#to_hash ⇒ Object
102 103 104 105 106 |
# File 'lib/hobix/datamarsh.rb', line 102 def to_hash h = {} keys.each { |k| h[ k ] = self.fetch( k ) } h end |
#update(hsh) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/hobix/datamarsh.rb', line 91 def update( hsh ) hsh.keys.each do |k| self.store( k, hsh.fetch( k ) ) end self end |
#values ⇒ Object
59 60 61 |
# File 'lib/hobix/datamarsh.rb', line 59 def values super.collect { |v| Marshal::load( v ) } end |
#values_at(*keys) ⇒ Object
31 32 33 |
# File 'lib/hobix/datamarsh.rb', line 31 def values_at( *keys ) keys.collect { |k| fetch( k ) } end |