Module: Saddle::MethodTreeBuilder
- Included in:
- Client
- Defined in:
- lib/saddle/method_tree_builder.rb
Instance Method Summary collapse
-
#build_node_children(current_module, current_node, requester) ⇒ Object
Build out the traversal tree by module namespace.
-
#build_root_node(requester) ⇒ Object
Build our root node here.
-
#build_tree(requester) ⇒ Object
Build out the endpoint structure from the root of the implementation.
-
#endpoints_directories ⇒ Object
Get all directories under the implementation root named ‘endpoints’ This search allows for flexible structuring of gem.
-
#endpoints_module ⇒ Object
Get the Endpoints module that lives within this implementation’s namespace.
-
#implementation_module ⇒ Object
Get the module that the client implementation belongs to.
Instance Method Details
#build_node_children(current_module, current_node, requester) ⇒ Object
Build out the traversal tree by module namespace
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/saddle/method_tree_builder.rb', line 58 def build_node_children(current_module, current_node, requester) return unless current_module 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, const_symbol.to_s.underscore ) # 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.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/saddle/method_tree_builder.rb', line 33 def build_root_node(requester) if defined?(self.implementation_root) root_endpoint_file = File.join( self.implementation_root, 'root_endpoint.rb' ) if File.file?(root_endpoint_file) warn "[DEPRECATION] `root_endpoint.rb` is deprecated. Please use `ABSOLUTE_PATH` in your endpoints." # Load it and create our base endpoint require(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::RootEndpoint end else # we don't even have an implementation root, so create a dummy endpoint root_node_class = Saddle::RootEndpoint 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 26 27 28 |
# File 'lib/saddle/method_tree_builder.rb', line 17 def build_tree(requester) root_node = build_root_node(requester) # Search out the implementations directory structure for endpoints if defined?(self.implementation_root) # For each endpoints directory, recurse down it to load the modules endpoints_directories.each do |endpoints_directories| Dir["#{endpoints_directories}/**/*.rb"].each { |f| require(f) } end build_node_children(self.endpoints_module, root_node, requester) end root_node end |
#endpoints_directories ⇒ Object
Get all directories under the implementation root named ‘endpoints’ This search allows for flexible structuring of gem
91 92 93 |
# File 'lib/saddle/method_tree_builder.rb', line 91 def endpoints_directories Dir["#{implementation_root}/**/"].select { |d| d.ends_with?('endpoints/') } end |
#endpoints_module ⇒ Object
Get the Endpoints module that lives within this implementation’s namespace
97 98 99 100 101 102 103 |
# File 'lib/saddle/method_tree_builder.rb', line 97 def endpoints_module begin implementation_module.const_get('Endpoints') rescue NameError nil # If there is no endpoints module, we just won't load any end end |
#implementation_module ⇒ Object
Get the module that the client implementation belongs to. This will act as the root namespace for endpoint traversal and construction
85 86 87 |
# File 'lib/saddle/method_tree_builder.rb', line 85 def implementation_module self.name.split('::')[0..-2].join('::').constantize end |