Module: Treebis::PersistentDotfile::ClassMethods

Defined in:
lib/treebis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dotfile_pathsObject

Returns the value of attribute dotfile_paths.



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

def dotfile_paths
  @dotfile_paths
end

#file_utilsObject

Returns the value of attribute file_utils.



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

def file_utils
  @file_utils
end

Instance Method Details

#dotfile_path_first_that_existsObject



864
865
866
# File 'lib/treebis.rb', line 864

def dotfile_path_first_that_exists
  @dotfile_paths.detect{ |p| File.exist?(p) }
end

#dotfile_path_to_write_toObject



868
869
870
# File 'lib/treebis.rb', line 868

def dotfile_path_to_write_to
  dotfile_path_first_that_exists || @dotfile_paths.first
end

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

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

Returns:

  • path to new empty directory



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

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



901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
# File 'lib/treebis.rb', line 901

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



850
851
852
853
854
855
856
857
858
859
860
861
862
# File 'lib/treebis.rb', line 850

def persistent_dotfile_init opts
  dfp =
    case opts[:dotfile_paths]
    when Array; opts[:dotfile_paths]
    when String; [opts[:dotfile_paths]]
    else fail("dotfile_path(s) must be string or array, not "<<
      " #{opts[:dofile_paths].inspect}")
  end
  @dotfile_paths = dfp
  @file_utils   ||= opts[:file_utils]
  @persistent_struct ||= nil
  @tmpdir       ||= nil
end

#persistent_get(path) ⇒ Object



921
922
923
924
# File 'lib/treebis.rb', line 921

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

#persistent_set(path, value) ⇒ Object



940
941
942
943
944
945
946
947
# File 'lib/treebis.rb', line 940

def persistent_set path, value
  struct = persistent_struct || (@persistent_struct = {})
  struct[path] = value
  File.open(dotfile_path_to_write_to, '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



928
929
930
931
932
933
934
935
936
937
938
# File 'lib/treebis.rb', line 928

def persistent_struct
  if @persistent_struct
    @persistent_struct
  elsif @persistent_struct == false
    nil
  elsif path = dotfile_path_first_that_exists
    @persistent_struct = JSON.parse(File.read(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.)



892
893
894
895
896
897
898
899
# File 'lib/treebis.rb', line 892

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