Class: Cabi::DataFile

Inherits:
Object
  • Object
show all
Defined in:
lib/datafile.rb

Class Method Summary collapse

Class Method Details

.bulk_selection(id, opts = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/datafile.rb', line 61

def self.bulk_selection(id, opts={})
  contents  = []

  opts[:only_file]     ||= false

  Dir.glob( File.join( *self.id_array(id) ) ).each do |f|
    next if f == '.' or f == '..' or File.directory?(f)
    contents << (opts[:only_file] ? f : File.read(f)) if File.exists?(f)
  end

  contents
end

.bulk_selection?(id) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/datafile.rb', line 95

def self.bulk_selection?(id)
  self.id_array(id).join('').index /\*/
end

.contents(id) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/datafile.rb', line 5

def self.contents(id)
  return self.bulk_selection(id)                        if self.bulk_selection?(id)
  return YAML.load(File.read( self.yaml_file(id) ))     if self.yaml_exists?(id)
  return File.read( self.file(id) )                     if self.file_exists?(id)
  return File.read( self.non_extension_file(id) )       if self.non_extension_file(id)
  return self.sub_yaml(id)
  nil
end

.exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/datafile.rb', line 79

def self.exists?(id)
  self.file_exists?(id) or self.yaml_exists?(id)
end

.file(id) ⇒ Object



36
37
38
# File 'lib/datafile.rb', line 36

def self.file(id)
  File.join( *self.id_array(id) )
end

.file_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/datafile.rb', line 83

def self.file_exists?(id)
  File.exists?( self.file(id) )
end

.file_parent(id) ⇒ Object



91
92
93
# File 'lib/datafile.rb', line 91

def self.file_parent(id)
  File.dirname( self.file(id) ) 
end

.file_yaml_or_non_extension_file(id, opts = {}) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/datafile.rb', line 28

def self.file_yaml_or_non_extension_file(id, opts={})
  return self.bulk_selection(id, only_file: true)     if self.bulk_selection?(id)
  return self.yaml_file(id)                           if self.yaml_exists?(id)
  return self.file(id)                                if self.file_exists?(id)
  return self.non_extension_file(id)                  if self.non_extension_file(id)
  nil
end

.has_yaml_ext?(id) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/datafile.rb', line 45

def self.has_yaml_ext?(id)
  (id[-4..-1] == YAML_EXT)
end

.id_array(id) ⇒ Object



74
75
76
77
# File 'lib/datafile.rb', line 74

def self.id_array(id)
  id = id.split( DELIMITER )
  [Cabi.data_dir] + id
end

.non_extension_file(id) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/datafile.rb', line 49

def self.non_extension_file(id)
  base = id.split( DELIMITER ).last

  file = false
  Dir.glob( self.file_parent(id) + '/*' ).each do |f|
    file = File.join(f) if File.basename(f, File.extname(f)) == base
    break if file
  end

  file
end

.sub_yaml(id) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/datafile.rb', line 99

def self.sub_yaml(id)
  id = id.split( DELIMITER )
  val = false

  id.each_with_index do |key, index|
    break if val

    a                 = [Cabi.data_dir] + id[0..index] 
    last              = a[a.length - 1] 
    a[ a.length - 1 ] = last + YAML_EXT if !self.has_yaml_ext?(last)
    f = File.join( *a )

    if File.exists? f
      data = YAML.load( File.read(f) )
      val = data.access( id[index+1..-1].join(DELIMITER) )
    end
  end

  val
end

.write(id, content) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/datafile.rb', line 14

def self.write(id, content)
  file      = self.file_yaml_or_non_extension_file(id)
  new_file  = self.file(id)

  if not file
    parent    = File.expand_path( '..', new_file )
    FileUtils.mkdir_p(parent)     unless File.exists?(parent)
    FileUtils.touch(new_file)     unless File.exists?(new_file)
    file = new_file
  end

  File.open( file, 'w') {|f| f.write(content) } 
end

.yaml_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/datafile.rb', line 87

def self.yaml_exists?(id)
  File.exists?( self.yaml_file(id) )
end

.yaml_file(id) ⇒ Object



40
41
42
43
# File 'lib/datafile.rb', line 40

def self.yaml_file(id)
  path = self.has_yaml_ext?(id) ? self.file(id) : self.file(id) + YAML_EXT
  File.join( path )
end