Class: Config

Inherits:
Object
  • Object
show all
Defined in:
lib/config.rb,
lib/config/stack.rb

Defined Under Namespace

Classes: Line, Section, Stack, Variable

Constant Summary collapse

SECTION_LINE =
/\A\s*\[([a-z0-9-]+)( "(.+)")?\]\s*(\Z|#|;)/i
VARIABLE_LINE =
/\A\s*([a-z][a-z0-9-]*)\s*=\s*(.*?)\s*(\Z|#|;)/im
BLANK_LINE =
/\A\s*(\Z|#|;)/
INTEGER =
/\A-?[1-9][0-9]*\Z/
VALID_SECTION =
/^[a-z0-9-]+$/i
VALID_VARIABLE =
/^[a-z][a-z0-9-]*$/i
Conflict =
Class.new(StandardError)
ParseError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Config

Returns a new instance of Config.



48
49
50
51
52
# File 'lib/config.rb', line 48

def initialize(path)
  @path     = path
  @lockfile = Lockfile.new(path)
  @lines    = nil
end

Class Method Details

.valid_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/config.rb', line 44

def self.valid_key?(key)
  VALID_SECTION =~ key.first and VALID_VARIABLE =~ key.last
end

Instance Method Details

#add(key, value) ⇒ Object



81
82
83
84
85
86
# File 'lib/config.rb', line 81

def add(key, value)
  key, var   = split_key(key)
  section, _ = find_lines(key, var)

  add_variable(section, key, var, value)
end

#get(key) ⇒ Object



70
71
72
# File 'lib/config.rb', line 70

def get(key)
  get_all(key).last
end

#get_all(key) ⇒ Object



74
75
76
77
78
79
# File 'lib/config.rb', line 74

def get_all(key)
  key, var = split_key(key)
  _, lines = find_lines(key, var)

  lines.map { |line| line.variable.value }
end

#openObject



54
55
56
# File 'lib/config.rb', line 54

def open
  read_config_file unless @lines
end

#open_for_updateObject



58
59
60
61
# File 'lib/config.rb', line 58

def open_for_update
  @lockfile.hold_for_update
  read_config_file
end

#remove_section(key) ⇒ Object



127
128
129
130
# File 'lib/config.rb', line 127

def remove_section(key)
  key = Section.normalize(key)
  @lines.delete(key) ? true : false
end

#replace_all(key, value) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/config.rb', line 101

def replace_all(key, value)
  key, var       = split_key(key)
  section, lines = find_lines(key, var)

  remove_all(section, lines)
  add_variable(section, key, var, value)
end

#saveObject



63
64
65
66
67
68
# File 'lib/config.rb', line 63

def save
  @lines.each do |section, lines|
    lines.each { |line| @lockfile.write(line.text) }
  end
  @lockfile.commit
end

#section?(key) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
# File 'lib/config.rb', line 143

def section?(key)
  key = Section.normalize(key)
  @lines.has_key?(key)
end

#set(key, value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/config.rb', line 88

def set(key, value)
  key, var       = split_key(key)
  section, lines = find_lines(key, var)

  case lines.size
  when 0 then add_variable(section, key, var, value)
  when 1 then update_variable(lines.first, var, value)
  else
    message = "cannot overwrite multiple values with a single value"
    raise Conflict, message
  end
end

#subsections(name) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/config.rb', line 132

def subsections(name)
  name, _  = Section.normalize([name])
  sections = []

  @lines.each_key do |main, sub|
    sections.push(sub) if main == name and sub != ""
  end

  sections
end

#unset(key) ⇒ Object



109
110
111
112
113
# File 'lib/config.rb', line 109

def unset(key)
  unset_all(key) do |lines|
    raise Conflict, "#{ key } has multiple values" if lines.size > 1
  end
end

#unset_all(key) {|lines| ... } ⇒ Object

Yields:

  • (lines)


115
116
117
118
119
120
121
122
123
124
125
# File 'lib/config.rb', line 115

def unset_all(key)
  key, var       = split_key(key)
  section, lines = find_lines(key, var)

  return unless section
  yield lines if block_given?

  remove_all(section, lines)
  lines = lines_for(section)
  remove_section(key) if lines.size == 1
end