Module: Config

Included in:
Crane::Commands::Test, Crane::Engine
Defined in:
lib/crane/config.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.CONFIGObject

Returns the value of attribute CONFIG.



20
21
22
# File 'lib/crane/config.rb', line 20

def CONFIG
  @CONFIG
end

.FILENAMEObject

Returns the value of attribute FILENAME.



20
21
22
# File 'lib/crane/config.rb', line 20

def FILENAME
  @FILENAME
end

.IGNORE_FILESObject

Returns the value of attribute IGNORE_FILES.



20
21
22
# File 'lib/crane/config.rb', line 20

def IGNORE_FILES
  @IGNORE_FILES
end

.PATHObject

Returns the value of attribute PATH.



20
21
22
# File 'lib/crane/config.rb', line 20

def PATH
  @PATH
end

Class Method Details

.get_ignored_filesObject



23
24
25
26
27
28
29
30
# File 'lib/crane/config.rb', line 23

def self.get_ignored_files
  if File.exists? ".gitignore"
    IO.readlines(".gitignore").each { |e|
      @IGNORE_FILES.push e.gsub(/\n/, "")
    }
  end
  @IGNORE_FILES
end

.has_config_file?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/crane/config.rb', line 32

def self.has_config_file?
  File.exists? @PATH
end

.load_configObject



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
103
104
105
106
107
108
109
# File 'lib/crane/config.rb', line 70

def self.load_config
  config = {}
  
  if File.exists? Config.PATH
    cfile = File.open Config.PATH, 'r'
    
    section = ""
    
    all_properties = {}
    cfile.each { |l|
      maybe_section = l.scan(/\[(.*)\]\n/)
      maybe_property = l.scan(/(.*)=(.*)\n/)
      
      maybe_section = maybe_section[0][0] if maybe_section[0]
      
      unless maybe_section.empty? then
        unless (section.empty? and all_properties.empty? )
          config[section.to_sym] = all_properties
          all_properties = ""
        end
        section = maybe_section
      else maybe_property.empty?
        l.scan(/(.*) = (.*)\n/) {
          |property, value|
          all_properties[property.to_sym] = value.to_s
        }
      end
      
    }
    
    unless (section.empty? and all_properties.empty? )
      config[section.to_sym] = all_properties
      all_properties = ""
    end
    
    cfile.close
  end
  @CONFIG = config
  config
end

.make_config(config) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/crane/config.rb', line 36

def self.make_config config
  data = ""
  
  config.each { |key, value|
    # a new section
    if value.class.to_s == "Hash" then
      data << "[" + key.to_s + "]" + "\n"
      data << self.make_config(value).to_s
    elsif ["String", "Symbol"].include? value.class.to_s
      data << key.to_s + " = " +value.to_s + "\n"
    end
  }
  
  data
end

.save_config(config) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/crane/config.rb', line 52

def self.save_config config
  @CONFIG = config
  data = self.make_config config
  unless data.empty?
    # deletes configuration file to recreate
    File.delete self.PATH if File.exists? self.PATH
    
    File.open(self.PATH, "w+") { |cfile|
      File.chmod(0777, self.PATH)
      data.each_line { |l|
        cfile.puts l
      }
    }
    return true
  end
  false
end