Class: Apia::FieldSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/apia/field_spec.rb

Defined Under Namespace

Classes: Parser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths, excludes: [], parsed_string: nil) ⇒ FieldSpec



12
13
14
15
16
# File 'lib/apia/field_spec.rb', line 12

def initialize(paths, excludes: [], parsed_string: nil)
  @paths = paths
  @excludes = excludes
  @parsed_string = parsed_string
end

Instance Attribute Details

#excludesObject (readonly)

Returns the value of attribute excludes.



9
10
11
# File 'lib/apia/field_spec.rb', line 9

def excludes
  @excludes
end

#parsed_stringObject (readonly)

Returns the value of attribute parsed_string.



10
11
12
# File 'lib/apia/field_spec.rb', line 10

def parsed_string
  @parsed_string
end

#pathsObject (readonly)

Returns the value of attribute paths.



8
9
10
# File 'lib/apia/field_spec.rb', line 8

def paths
  @paths
end

Class Method Details

.parse(string) ⇒ Object

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)



147
148
149
150
# File 'lib/apia/field_spec.rb', line 147

def parse(string)
  parser = Parser.new(string)
  parser.parse
end

Instance Method Details

#include_field?(field_path) ⇒ Boolean



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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