class Apia::FieldSpec

Attributes

excludes[R]
parsed_string[R]
paths[R]

Public Class Methods

new(paths, excludes: [], parsed_string: nil) click to toggle source
# File lib/apia/field_spec.rb, line 12
def initialize(paths, excludes: [], parsed_string: nil)
  @paths = paths
  @excludes = excludes
  @parsed_string = parsed_string
end
parse(string) click to toggle source

data_center # => Return all default attributes for data center data_center # => Only return name for data center data_center[+country] # => Add the country to the default parameters with it only containing id and name data_center # => Remove country from the default parameters (assuming it is part of them) data_center # => Pointless but should return name plus the default country params (same as name,country)

# File lib/apia/field_spec.rb, line 147
def parse(string)
  parser = Parser.new(string)
  parser.parse
end

Public Instance Methods

include_field?(field_path) click to toggle source
# File lib/apia/field_spec.rb, line 18
def include_field?(field_path)
  if field_path.is_a?(String)
    path = field_path.split('.')
  else
    path = field_path.map { |r| r.name.to_s }
  end

  # If the field path matches exactly any item in the list of paths
  # allowed, then allow this path.
  return true if @paths.include?(path.join('.'))

  # If the field is purposely excluded, we'll check that and ensure that it
  # isn't included.
  return false if @excludes.include?(path.join('.'))

  # If there's a wildcard at the root we can allow it at this point
  # return true if @paths.include?('*')

  # Check to see whether we're allowing a wildcard to be permitted at any
  # point in the chain
  path.size.times do |i|
    parts = path[0, path.size - i - 1]

    next unless @paths.include?((parts + ['*']).join('.'))

    next_parts = path[0, path.size - i]
    unless @paths.include?(next_parts.join('.'))
      return true
    end
  end

  false
end