Class: Yapfac::Apache::Directive

Inherits:
Object
  • Object
show all
Defined in:
lib/yapfac/apache/directive.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *params) ⇒ Directive

Initialize a new Directive, given an array and arbitrary number of params.

Parameters:

  • name (String)

    The directive name (DocumentRoot, etc.)

  • *params (Array)

    Any number of parameters for the directive.



12
13
14
15
# File 'lib/yapfac/apache/directive.rb', line 12

def initialize(name, *params)
  @name   = name
  @params = params
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/yapfac/apache/directive.rb', line 5

def name
  @name
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/yapfac/apache/directive.rb', line 5

def params
  @params
end

Class Method Details

.parse(line) ⇒ Yapfac::Apache::Directive

Parses a directive string into a directive object. This is most often used when reading in from a file.

Examples:

Known Name

d = Yapfac::Apache::Directive.parse("DocumentRoot", "/www/html")

Full Line Parse

d = Yapfac::Apache::Directive.parse("DocumentRoot /www/html")

Parse Multiple Params

# The real purpose of this method.
d1 = Yapfac::Apache::Directive.parse("Deny from all")
d2 = Yapfac::Apache::Directive.parse("Deny", "from all")

# d1.params == d2.params == ["from", "all"]

Parameters:

  • line (String)

    The full directive line to parse, with or without params.

Parameters:

  • name (String)

    The directive name (DocumentRoot, etc.)

  • params (String) (defaults to: nil)

    A string of directive parameters to parse.

Returns:



42
43
44
45
# File 'lib/yapfac/apache/directive.rb', line 42

def self.parse(name, params = nil)
  name, params = name.split /\s+/, 2 if params.nil?
  return Yapfac::Apache::Directive.new(name, *parse_params(params))
end

Instance Method Details

#to_hHash

Builds a hash representation of the Apache Directive, useful for serialization.

Examples:

Hashify an Apache Directive

d = Yapfac::Apache::Directive.parse("Deny from all")
d.to_h #=> { name: "Deny", params: [ "from", "all" ] }

Returns:

  • (Hash)

    A hash with :name (String) and :params (array) keys.



71
72
73
74
75
76
# File 'lib/yapfac/apache/directive.rb', line 71

def to_h
  return ({
    name: @name,
    params: @params
  })
end

#to_sString

Builds a string representation of the directive, which is valid for storing in an Apache configuration file.

Returns:

  • (String)

    String representation of Apache Directive



52
53
54
55
56
57
58
59
60
# File 'lib/yapfac/apache/directive.rb', line 52

def to_s
  "#{@name} #{@params.collect do |p|
    if p =~ /\s/
      "\"#{p}\""
    else
      p
    end
  end.join(' ')}"
end