Class: LanGrove::Persistable

Inherits:
Object
  • Object
show all
Defined in:
lib/langrove/ext/persistable.rb

Instance Method Summary collapse

Constructor Details

#initialize(notifies = []) ⇒ Persistable

Returns a new instance of Persistable.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/langrove/ext/persistable.rb', line 8

def initialize( notifies = [] )
  
  @notifies = notifies
  
  @notifies.each do |worker|
    
    Object.const_set( 
    
      worker.camelize, Class.new 
      
    ) ### ??unless defined already??
    
  end
  
end

Instance Method Details

#load_hash(hash_instance, from_file) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/langrove/ext/persistable.rb', line 24

def load_hash( hash_instance, from_file )
  #
  # <hash_instance> as String (name of) variable storing the Hash
  #
  
  @logger.debug "loading from: #{from_file}" if @logger
  
  #
  # get the instance variable (late bind)
  #
  hash = self.instance_variable_get( hash_instance.to_sym )
  hash ||= {}
  
  begin
    
    #
    # load file contents, merge into hash and 
    # store it at the instance variable
    #
    persisted = YAML.load_file( from_file )
    hash.merge!( persisted ) if persisted.is_a?( Hash )
    self.instance_variable_set( hash_instance.to_sym, hash )
    
    #
    # set flag to 'already in storage'
    #
    @stored = true
  
  rescue Exception => e
    @logger.error "FAILED loading from #{from_file} #{e}" if @logger
    
  end
end

#store_hash(hash_instance, to_file) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
101
102
# File 'lib/langrove/ext/persistable.rb', line 59

def store_hash( hash_instance, to_file )
  #
  # <hash_instance> as String (name of) variable storing the Hash
  #
  
  if @stored != nil && @stored
    
    @logger.debug "storing to: #{to_file} skipped - no change"
    return
    
  end
  
  @logger.debug "storing to: #{to_file}"
  
  #
  # get the instance variable (late bind)
  #
  hash = self.instance_variable_get( hash_instance.to_sym )
  
  begin
  
    File.open(to_file, 'w') do |f|

      YAML::dump( hash, f )
      @stored = true
      
    end
    
    #@notifies.each do |worker|
    #
    #  Resque.enqueue(
    #  
    #    Object.const_get( worker.camelize ), to_file
    #    
    #  )
    #  
    #end
  
  rescue Exception => e
    @logger.error "FAILED storing to: #{to_file} #{e}" if @logger
    @stored = false
    
  end
end