Module: MetaHashMethods

Included in:
KVJ
Defined in:
lib/meta_hash_methods.rb

Instance Method Summary collapse

Instance Method Details

#generate_hash_functionsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/meta_hash_methods.rb', line 20

def generate_hash_functions
  methods = Hash.instance_methods - KVJ.instance_methods
  write_methods = methods.select { |method| method =~ /=|!|delete/ }
  read_only_methods = methods - write_methods

  read_only_methods.each do |method|
    inheritage_hash_method(method, :read)
  end

  write_methods.each do |method|
    inheritage_hash_method(method, :write)
  end
end

#inheritage_hash_method(method, level) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/meta_hash_methods.rb', line 2

def inheritage_hash_method(method, level)
  define_method(method) do |*arg, &block|
    begin
      if level == :write
        @file_connector.grab_ex_lock
      else
        @file_connector.grab_sh_lock
      end
      hash = @file_connector.read
      return_value = hash.send(method, *arg, &block)
      @file_connector.write(hash) if level == :write
      return return_value
    ensure
      @file_connector.release_lock
    end
  end
end