Sha256: 6e080d1b770423432823defa1ba6a86308a685f05786c255b300ea77e0cea931

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

class Chef
  class Knife
    module PartialSearch
      def define_partial_search(keys)
        Chef::Search::Query.class_eval do

          @@keys = keys

          alias_method :old_search, :search unless method_defined? :old_search

          def search(type, query="*:*", sort='X_CHEF_id_CHEF_X asc', start=0, rows=1000, &block)
            q = Chef::PartialSearch.new
            args = Hash.new
            args[:keys] = @@keys
            args[:sort] = sort
            args[:start] = start
            args[:rows] = rows
            if block_given?
              q.search(type, query, args) do |node_hash|
                n = ::Knife::PartialSearch::FakeNode.new do |h,k|
                  h[k] = node_hash[k]
                end
                @@keys.each do |k,v| n[k] end

                block.call(n)
              end
            else
              res = q.search(type, query, args)
              [res.first]
            end
          end
        end
      end
    end
  end
end

class Chef
  class Knife
    module Core
      class GenericPresenter

        alias_method :old_extract, :extract_nested_value unless method_defined? :old_extract

        def extract_nested_value(data, nested_value_spec)
          if data.kind_of?(::Knife::PartialSearch::FakeNode)
            data[nested_value_spec]
          else
            old_extract(data, nested_value_spec)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
knife-partial-search-0.0.4 lib/chef/knife/partial_search/partial_search.rb
knife-partial-search-0.0.3 lib/chef/knife/partial_search/partial_search.rb