Module: Treebis::PersistentDotfile::ClassMethods

Defined in:
lib/treebis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dotfile_pathObject

Returns the value of attribute dotfile_path.



849
850
851
# File 'lib/treebis.rb', line 849

def dotfile_path
  @dotfile_path
end

#file_utilsObject

Returns the value of attribute file_utils.



849
850
851
# File 'lib/treebis.rb', line 849

def file_utils
  @file_utils
end

Instance Method Details

#empty_tmpdir(path, futils_opts = {}) ⇒ Object

if it exists delete it. create it. file_utils must be defined

Returns:

  • path to new empty directory



862
863
864
865
866
867
868
869
870
# File 'lib/treebis.rb', line 862

def empty_tmpdir path, futils_opts={}
  futils_opts = {:verbose=>true}.merge(futils_opts)
  full_path = File.join(tmpdir, path)
  if File.exist? full_path
    file_utils.remove_entry_secure full_path, futils_opts
  end
  file_utils.mkdir_p full_path, futils_opts
  full_path
end

#persistent_delegate_to(mod) ⇒ Object



887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
# File 'lib/treebis.rb', line 887

def persistent_delegate_to(mod)
  if instance_variable_defined?('@persistent_delegate') # rcov
    @persistent_delegate
  else
    @persistent_delegate = begin
      mm = Module.new
      str = "#{self}::PersistentDotfileDelegate"
      const_set('PersistentDotfileDelegate', mm)
      class << mm; self end.send(:define_method,:inspect){str}
      buck = self
      DelegatedMethods.each do |meth|
        mm.send(:define_method, meth){|*a| buck.send(meth,*a) }
      end
      mm
    end
  end
  mod.extend(@persistent_delegate)
  mod.send(:include, @persistent_delegate)
end

#persistent_dotfile_init(opts) ⇒ Object



851
852
853
854
855
856
# File 'lib/treebis.rb', line 851

def persistent_dotfile_init opts
  @dotfile_path ||= opts[:dotfile_path]
  @file_utils   ||= opts[:file_utils]
  @persistent_struct ||= nil
  @tmpdir       ||= nil
end

#persistent_get(path) ⇒ Object



907
908
909
910
# File 'lib/treebis.rb', line 907

def persistent_get path
  struct = persistent_struct or return nil
  struct[path]
end

#persistent_set(path, value) ⇒ Object



926
927
928
929
930
931
932
933
# File 'lib/treebis.rb', line 926

def persistent_set path, value
  struct = persistent_struct || (@persistent_struct = {})
  struct[path] = value
  File.open(dotfile_path, 'w+') do |fh|
    fh.write JSON.pretty_generate(struct)
  end
  nil
end

#persistent_structObject

this might cause bugs if different classes use the same dotfile name



914
915
916
917
918
919
920
921
922
923
924
# File 'lib/treebis.rb', line 914

def persistent_struct
  if @persistent_struct
    @persistent_struct
  elsif @persistent_struct == false
    nil
  elsif File.exists? dotfile_path
    @persistent_struct = JSON.parse(File.read(dotfile_path))
  else
    @persistent_struct = false
  end
end

#tmpdirObject

Get a path to a temporary directory, suitable to be used in tests. The contents of this directory are undefined, but it is writable and as the name implies temporary so a given test should feel free to erase it and create a new empty one at this same path if desired. (see callee for details.)



878
879
880
881
882
883
884
885
# File 'lib/treebis.rb', line 878

def tmpdir
  if @tmpdir.nil?
    @tmpdir = get_tmpdir
  elsif ! File.exist?(@tmpdir) # check every time!
    @tmpdir = get_tmpdir
  end
  @tmpdir
end