Class: Hobix::DataMarsh

Inherits:
DBM
  • Object
show all
Defined in:
lib/hobix/datamarsh.rb

Constant Summary collapse

VERSION =
"0.1"

Instance Method Summary collapse

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_ifObject



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_pairObject 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_valueObject



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

Returns:

  • (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

#invertObject



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

#rejectObject



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

#shiftObject



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_aObject



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_hashObject



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

#valuesObject



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