Class: APISpec::Namespace

Inherits:
Node
  • Object
show all
Defined in:
lib/apispec/namespace.rb

Defined Under Namespace

Classes: AlreadyDefinedError, ReferenceError

Instance Attribute Summary

Attributes inherited from Node

#name, #parent

Instance Method Summary collapse

Methods inherited from Node

#full_name, #node_path, #root?, #to_html, #to_path

Constructor Details

#initialize(name, &block) ⇒ Namespace

Returns a new instance of Namespace.



5
6
7
8
# File 'lib/apispec/namespace.rb', line 5

def initialize(name, &block)
  @nodes = {}
  super(name, &block)
end

Instance Method Details

#all(method, type) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/apispec/namespace.rb', line 38

def all(method, type)
  nodes = []
  @nodes.keys.sort.each do |name|
    node = @nodes[name]
    if node.is_a? APISpec::Namespace
      nodes << node.send(method)
    elsif node.is_a? type
      nodes << node
    end
  end
  nodes.flatten
end

#define_node(name, node) ⇒ Object



108
109
110
111
112
# File 'lib/apispec/namespace.rb', line 108

def define_node(name, node)
  raise AlreadyDefinedError.new("#{name} is already defined!") if @nodes[name]
  @nodes[name] = node
  node.parent = self if node.respond_to? :parent
end

#field(name, reference = nil, &block) ⇒ Object



124
125
126
127
# File 'lib/apispec/namespace.rb', line 124

def field(name, reference = nil, &block)
  reference = reference || name
  define_node reference, APISpec::Field.new(name, &block)
end

#field_set(reference = nil, *fields) ⇒ Object



129
130
131
# File 'lib/apispec/namespace.rb', line 129

def field_set(reference = nil, *fields)
  define_node reference, fields
end

#find(path) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/apispec/namespace.rb', line 28

def find(path)
  path_parts = path.split(".")
  node = self
  while path_parts.any?
    part = path_parts.shift
    node = node.find_node(part)
  end
  node
end

#find_field(path) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/apispec/namespace.rb', line 15

def find_field(path)
  result = find(path)
  if result.is_a? Array
    result.map { |path| find(path) }
  else
    [result]
  end
end

#find_node(name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/apispec/namespace.rb', line 59

def find_node(name)
  node = @nodes[name]
  if node
    node
  else
    similar_node = self.find_similar_node(name)
    if similar_node
      raise ReferenceError.new("#{name} not found in #{self.to_s}, maybe you meant #{similar_node} (case-sensitive!)")
    else
      raise ReferenceError.new("#{name} not found in #{self.to_s}")
    end
  end
end

#find_object(path) ⇒ Object



24
25
26
# File 'lib/apispec/namespace.rb', line 24

def find_object(path)
  find(path)
end

#find_similar_node(name) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/apispec/namespace.rb', line 73

def find_similar_node(name)
  @nodes.each do |node_name,node_value|
    if node_name.downcase == name.downcase
      return node_name
    end
  end
  nil
end

#interface(name, reference = nil, &block) ⇒ Object



114
115
116
117
# File 'lib/apispec/namespace.rb', line 114

def interface(name, reference = nil, &block)
  reference = reference || name
  define_node name, APISpec::Interface.new(name, &block)
end

#interfacesObject



51
52
53
# File 'lib/apispec/namespace.rb', line 51

def interfaces
  all(:interfaces, APISpec::Interface)
end

#namespace(name, &block) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/apispec/namespace.rb', line 100

def namespace(name, &block)
  if node = @nodes[name]
    node.instance_eval &block
  else
    define_node name, APISpec::Namespace.new(name, &block)
  end
end

#object(name, reference = nil, &block) ⇒ Object



119
120
121
122
# File 'lib/apispec/namespace.rb', line 119

def object(name, reference = nil, &block)
  reference = reference || name
  define_node reference, APISpec::Object.new(name, &block)
end

#objectsObject



55
56
57
# File 'lib/apispec/namespace.rb', line 55

def objects
  all(:objects, APISpec::Object)    
end

print the structure of all namespaces



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/apispec/namespace.rb', line 87

def print_tree(indent = 0, lines = [])
  lines << ("  " * indent) + self.to_s
  @nodes.keys.sort.each do |reference|
    node = @nodes[reference]
    if node.is_a? APISpec::Namespace
      node.print_tree(indent + 1, lines)
    else
      lines << ("  " * (indent + 1)) + "#{reference} => #{node.to_s}"
    end
  end
  lines.join("\n")
end

#read_file(path) ⇒ Object

reads the passed file



11
12
13
# File 'lib/apispec/namespace.rb', line 11

def read_file(path)
  eval(File.read(path), binding, path)
end

#to_sObject



82
83
84
# File 'lib/apispec/namespace.rb', line 82

def to_s
  "#{super} (Nodes: #{@nodes.size})"
end