class BrocadeVRouter::PathBuilder

Constants

SEPARATOR
SNAKE_TRANSFORM
WHTSP_TRANSFORM

Attributes

base_path[R]

Public Class Methods

new(context = nil, base_path = nil, &blk) click to toggle source
# File lib/brocade_vrouter/path_builder.rb, line 9
def initialize(context = nil, base_path = nil, &blk)
  @base_path, @paths, @context = base_path, [], context

  if block_given?
    @context ||= eval 'self', blk.binding
    instance_eval(&blk)
  end
end

Public Instance Methods

to_a() click to toggle source
# File lib/brocade_vrouter/path_builder.rb, line 18
def to_a
  return @paths.flatten if !base_path || base_path.empty?
  @paths.flatten.map { |p| [base_path, p].join(SEPARATOR) }
end

Private Instance Methods

address(value, &blk) click to toggle source
# File lib/brocade_vrouter/path_builder.rb, line 34
def address(value, &blk)
  addresses = Array(value).map { |v| URI.escape v.to_s, SEPARATOR }
  @paths << generate_paths!(:address, addresses, nil, &blk)
  @paths.last
end
escape(value) click to toggle source
# File lib/brocade_vrouter/path_builder.rb, line 62
def escape(value)
  Array(value).map { |v| URI.escape(v.to_s.gsub(*WHTSP_TRANSFORM)) }
end
generate_paths!(name, value = nil, exec = nil, &blk) click to toggle source
# File lib/brocade_vrouter/path_builder.rb, line 46
def generate_paths!(name, value = nil, exec = nil, &blk)
  path = URI.escape name.to_s.gsub(*SNAKE_TRANSFORM)

  value_is_path = @paths.pop if @paths.last == value
  @paths << "#{path}/#{value_is_path ? value : to_path(value)}" if exec

  return value.map { |v| [path, v].join(SEPARATOR) } if value.is_a?(Array)

  path = [path, value_is_path ? value : to_path(value)].join(SEPARATOR) if value
  block_given? ? PathBuilder.new(@context, path, &blk).to_a : path
end
hash_to_ary(hash) click to toggle source
# File lib/brocade_vrouter/path_builder.rb, line 66
def hash_to_ary(hash)
  hash.map do |k, v|
    respond_to?(k, true) ? send(k, v) && @paths.pop : generate_paths!(k, to_path(v))
  end
end
method_missing(*args, &blk) click to toggle source
# File lib/brocade_vrouter/path_builder.rb, line 40
def method_missing(*args, &blk)
  return @context.send(*args) if @context.respond_to?(args.first, true)
  @paths << generate_paths!(*args, &blk)
  @paths.last
end
to_path(value) click to toggle source
# File lib/brocade_vrouter/path_builder.rb, line 58
def to_path(value)
  (value.is_a?(Hash) ? hash_to_ary(value) : escape(value)).join(SEPARATOR)
end