Module: Saddle::MethodTreeBuilder

Included in:
Client
Defined in:
lib/saddle/method_tree_builder.rb

Instance Method Summary collapse

Instance Method Details

#build_node_children(current_module, current_node, requester) ⇒ Object

Build out the traversal tree by module namespace



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/saddle/method_tree_builder.rb', line 54

def build_node_children(current_module, current_node, requester)
  current_module.constants.each do |const_symbol|
    const = current_module.const_get(const_symbol)

    if const.class == Module
      # A module means that it's a branch
      # Build the branch out with a base endpoint
      branch_node = current_node._build_and_attach_node(
        Saddle::TraversalEndpoint,
        ActiveSupport::Inflector.underscore(const_symbol)
      )
      # Build out the branch's endpoints on the new branch node
      self.build_node_children(const, branch_node, requester)
    end

    if const < Saddle::TraversalEndpoint
      # A class means that it's a node
      # Build out this endpoint on the current node
      current_node._build_and_attach_node(const)
    end
  end
end

#build_root_node(requester) ⇒ Object

Build our root node here. The root node is special in that it lives below the ‘endpoints’ directory, and so we need to manually check if it exists.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/saddle/method_tree_builder.rb', line 30

def build_root_node(requester)
  if knows_root?
    root_endpoint_file = File.join(
      self.implementation_root,
      'root_endpoint.rb'
    )
    if File.file?(root_endpoint_file)
      # Load it and create our base endpoint
      load(root_endpoint_file)
      # RootEndpoint is the special class name for a root endpoint
      root_node_class = self.implementation_module::RootEndpoint
    else
      # 'root_endpoint.rb' doesn't exist, so create a dummy endpoint
      root_node_class = Saddle::TraversalEndpoint
    end
  else
    # we don't even have an implementation root, so create a dummy endpoint
    root_node_class = Saddle::TraversalEndpoint
  end
  root_node_class.new(requester, nil, self)
end

#build_tree(requester) ⇒ Object

Build out the endpoint structure from the root of the implementation



17
18
19
20
21
22
23
24
25
# File 'lib/saddle/method_tree_builder.rb', line 17

def build_tree(requester)
  root_node = build_root_node(requester)
  # If we have an 'endpoints' directory, build it out
  if knows_root? && Dir.exists?(endpoints_directory)
    Dir["#{endpoints_directory}/**/*.rb"].each { |f| load(f) }
    build_node_children(self.endpoints_module, root_node, requester)
  end
  root_node
end

#endpoints_directoryObject

Get the path to the ‘endpoints’ directory, based upon the client class that inherited Saddle



94
95
96
# File 'lib/saddle/method_tree_builder.rb', line 94

def endpoints_directory
  File.join(self.implementation_root, 'endpoints')
end

#endpoints_moduleObject

Get the Endpoints module that lives within this implementation’s namespace



88
89
90
# File 'lib/saddle/method_tree_builder.rb', line 88

def endpoints_module
  implementation_module.const_get('Endpoints')
end

#implementation_moduleObject

Get the module that the client implementation belongs to. This will act as the root namespace for endpoint traversal and construction



80
81
82
83
84
# File 'lib/saddle/method_tree_builder.rb', line 80

def implementation_module
  ::ActiveSupport::Inflector.constantize(
    self.name.split('::')[0..-2].join('::')
  )
end

#knows_root?Boolean

If this client was not fully constructed, it may not even have an implementation root. Allow that behavior to avoid filesystem searching.

Returns:

  • (Boolean)


100
101
102
# File 'lib/saddle/method_tree_builder.rb', line 100

def knows_root?
  defined?(self.implementation_root)
end