Class: Wpify::Parser

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

Overview

Parses the contents of a wp-config.php file into a Hash reasonably well.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Parser

Provide a comma separated list or files and a hash of options at the end (not yet used)



15
16
17
18
19
# File 'lib/wpify/parser.rb', line 15

def initialize(*args)
  @options = args.last.kind_of?(Hash) ? args.pop : Hash.new
  @files = args
  parse
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



11
12
13
# File 'lib/wpify/parser.rb', line 11

def files
  @files
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/wpify/parser.rb', line 11

def options
  @options
end

#wpconfigObject (readonly)

Returns the value of attribute wpconfig.



11
12
13
# File 'lib/wpify/parser.rb', line 11

def wpconfig
  @wpconfig
end

Class Method Details

.load(*args) ⇒ Object

Loader



6
7
8
9
# File 'lib/wpify/parser.rb', line 6

def self.load(*args)
  load = self.new(*args)
  load.wpconfig
end

Instance Method Details

#parseObject

Parse all the files in @files and add the configuration hash to @wpconfig



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wpify/parser.rb', line 23

def parse
  @wpconfig = Hash.new
  @files.each do |f|
    f = File.expand_path(f)
    next if not File.exist?(f)
    raw = File.open(f).read
    raw.scan(constant_pattern).each {|c| @wpconfig[c[0].downcase.to_sym] = c[1] }
    raw.scan(var_pattern).each {|v| @wpconfig[v[0].downcase.to_sym] = v[1] }
  end
  @wpconfig
end