Class: Mantra::Transform::TemplatizeIpAddress::QuadSplitter

Inherits:
Object
  • Object
show all
Includes:
Helpers::TemplateHelper
Defined in:
lib/mantra/transform/templatize_ip_address.rb

Overview

def templatize(parts)

raise "wrong!!!"
merged_parts = [parts.shift]
parts.each do |p|
  if !p.is_a?(Scope) && !merged_parts.last.is_a?(Scope)
    merged_parts.last.concat(p)
  else
    merged_parts << p
  end
end
merged_parts.select { |p| !p.empty? }.map do |p|
  p.is_a?(Scope) ? p : "\"#{p}\""
end

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::TemplateHelper

#is_scope?, #templatize

Constructor Details

#initialize(ip_address, template_quads) ⇒ QuadSplitter

Returns a new instance of QuadSplitter.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mantra/transform/templatize_ip_address.rb', line 92

def initialize(ip_address, template_quads)
  @values = {}
  extract_options = template_quads.map do |quad|
    quad["range_object"] = if quad["number"]
      index = quad["number"].to_i - 1
      (index..index)
    elsif quad["range"]
      index1, index2 = *quad["range"].split("-").map { |v| v.strip.to_i - 1 }
      (index1..index2)
    end
    quad
  end

  template = IpAddressTemplate.new(ip_address)

  extract_options.each do |option|
    range = option["range_object"]
    value_to_extract = template.quads[range].join(".")
    if option["with_value"].nil? || value_to_extract == option["with_value"]
      template.replace_with_scope(range, option["scope"])
      @values[option["scope"]] = value_to_extract
    end
  end
  @parts = template.parts
end

Instance Attribute Details

#parts(options = {templatize: false}) ⇒ Object

Returns the value of attribute parts.



91
92
93
# File 'lib/mantra/transform/templatize_ip_address.rb', line 91

def parts
  @parts
end

#valuesObject

Returns the value of attribute values.



91
92
93
# File 'lib/mantra/transform/templatize_ip_address.rb', line 91

def values
  @values
end