Sha256: 1d0d685040dd89575f9f8f57b5f80b4ef1d4d598d26d03cb8dbf529ec964881c

Contents?: true

Size: 1.31 KB

Versions: 38

Compression:

Stored size: 1.31 KB

Contents

module Appfuel
  module Config
    module Search
      # Allow you to access child definitions as if it were a hash.
      # If you add a space separated list of names this will traverse
      # the child hierarchy and return the last name in the list
      #
      # @param name String name or names to search
      # @return Definition | nil
      def [](name)
        find @children, name.to_s.split(" ")
      end

      # Allows you to search child definitions using an array of names
      # instead of a space separated string
      #
      # @param names Array of strings
      # @return Definition | nil
      def search(*names)
        return nil if names.empty?
        find children, names
      end

      protected

      # Recursively locate a child definition in the hierarchy
      #
      # @param child_list Hash
      # @param terms Array of definition keys
      def find(child_list, terms)
        while term = terms.shift
          term = term.to_sym
          child_list.each do |(definition_key, definition)|
            next unless definition_key == term
            result = if terms.empty?
                       definition
                     else
                       find(definition.children, terms)
                     end
            return result
          end
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
appfuel-0.7.0 lib/appfuel/config/search.rb
appfuel-0.6.16 lib/appfuel/config/search.rb
appfuel-0.6.15 lib/appfuel/config/search.rb
appfuel-0.6.14 lib/appfuel/config/search.rb
appfuel-0.6.13 lib/appfuel/config/search.rb
appfuel-0.6.12 lib/appfuel/config/search.rb
appfuel-0.6.11 lib/appfuel/config/search.rb
appfuel-0.6.10 lib/appfuel/config/search.rb
appfuel-0.6.9 lib/appfuel/config/search.rb
appfuel-0.6.8 lib/appfuel/config/search.rb
appfuel-0.6.7 lib/appfuel/config/search.rb
appfuel-0.6.6 lib/appfuel/config/search.rb
appfuel-0.6.5 lib/appfuel/config/search.rb
appfuel-0.6.4 lib/appfuel/config/search.rb
appfuel-0.6.3 lib/appfuel/config/search.rb
appfuel-0.6.1 lib/appfuel/config/search.rb
appfuel-0.5.16 lib/appfuel/config/search.rb
appfuel-0.5.15 lib/appfuel/config/search.rb
appfuel-0.5.14 lib/appfuel/config/search.rb
appfuel-0.5.13 lib/appfuel/config/search.rb