Kouhei Sutou
null+****@clear*****
Fri Dec 9 18:56:33 JST 2016
Kouhei Sutou 2016-12-09 18:56:33 +0900 (Fri, 09 Dec 2016) New Revision: d268afac6fe9c09368dced525cb3b7a0ab6063e9 https://github.com/ranguba/groonga-client-rails/commit/d268afac6fe9c09368dced525cb3b7a0ab6063e9 Message: Use Groonga::Client::Request It's included in groonga-client 0.3.3 but it's not released yet. Added files: lib/groonga/client/searcher/select_request.rb Removed files: lib/groonga/client/searcher/error.rb lib/groonga/client/searcher/raw_request.rb lib/groonga/client/searcher/select.rb lib/groonga/client/searcher/select/request.rb lib/groonga/client/searcher/select/result_set.rb Modified files: groonga-client-rails.gemspec lib/groonga/client/searcher.rb Modified: groonga-client-rails.gemspec (+1 -1) =================================================================== --- groonga-client-rails.gemspec 2016-06-09 17:44:14 +0900 (789008f) +++ groonga-client-rails.gemspec 2016-12-09 18:56:33 +0900 (e7b202e) @@ -40,7 +40,7 @@ Gem::Specification.new do |spec| spec.files += Dir.glob("doc/text/*") spec.test_files += Dir.glob("test/**/*") - spec.add_runtime_dependency("groonga-client", ">= 0.2.4") + spec.add_runtime_dependency("groonga-client", ">= 0.3.3") spec.add_runtime_dependency("rails") spec.add_development_dependency("bundler") Modified: lib/groonga/client/searcher.rb (+3 -3) =================================================================== --- lib/groonga/client/searcher.rb 2016-06-09 17:44:14 +0900 (23c81e8) +++ lib/groonga/client/searcher.rb 2016-12-09 18:56:33 +0900 (92ea678) @@ -16,8 +16,7 @@ require "groonga/client" -require "groonga/client/searcher/error" -require "groonga/client/searcher/select" +require "groonga/client/searcher/select_request" require "groonga/client/searcher/schema" require "groonga/client/searcher/schema_synchronizer" require "groonga/client/searcher/source" @@ -152,7 +151,8 @@ module Groonga full_text_searchable_column_names << name end end - Select::Request.new(schema.table). + Request::Select.new(schema.table). + extend(SelectRequest). match_columns(full_text_searchable_column_names) end Deleted: lib/groonga/client/searcher/error.rb (+0 -37) 100644 =================================================================== --- lib/groonga/client/searcher/error.rb 2016-06-09 17:44:14 +0900 (0649142) +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -module Groonga - class Client - class Searcher - class Error < Client::Error - end - - class ErrorResponse < Error - attr_reader :response - def initialize(response) - @response = response - command =****@respo***** - status_code =****@respo*****_code - error_message =****@respo*****_message - message = "failed to execute: #{command.name}: #{status_code}: " - message << "<#{error_message}>" - super(message) - end - end - end - end -end Deleted: lib/groonga/client/searcher/raw_request.rb (+0 -85) 100644 =================================================================== --- lib/groonga/client/searcher/raw_request.rb 2016-06-09 17:44:14 +0900 (8b1c56d) +++ /dev/null @@ -1,85 +0,0 @@ -# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -module Groonga - class Client - class Searcher - class RawRequest - def initialize(command_name, parameters=nil) - @command_name = command_name - @parameters = parameters - end - - def response - @reponse ||= Client.open do |client| - response = client.execute(@command_name, to_parameters) - raise ErrorResponse.new(response) unless response.success? - response - end - end - - def parameter(name, value) - add_parameter(OverwriteMerger, - RequestParameter.new(name, value)) - end - - def to_parameters - if****@param*****? - {} - else - @parameters.to_parameters - end - end - - private - def add_parameter(merger_class, parameter) - merger = merger_class.new(@parameters, parameter) - create_request(merger) - end - - def create_request(parameters) - self.class.new(@command_name, parameters) - end - end - - class RequestParameter - def initialize(name, value) - @name = name - @value = value - end - - def to_parameters - { - @name => @value, - } - end - end - - class ParameterMerger - def initialize(parameters1, parameters2) - @parameters1 = parameters1 - @parameters2 = parameters2 - end - end - - class OverwriteMerger < ParameterMerger - def to_parameters - @parameters1.to_parameters.merge(@parameters2.to_parameters) - end - end - end - end -end Deleted: lib/groonga/client/searcher/select.rb (+0 -18) 100644 =================================================================== --- lib/groonga/client/searcher/select.rb 2016-06-09 17:44:14 +0900 (c6e2de7) +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -require "groonga/client/searcher/select/request" -require "groonga/client/searcher/select/result_set" Deleted: lib/groonga/client/searcher/select/request.rb (+0 -283) 100644 =================================================================== --- lib/groonga/client/searcher/select/request.rb 2016-06-09 17:44:14 +0900 (c78022c) +++ /dev/null @@ -1,283 +0,0 @@ -# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -require "active_support/core_ext/object/blank" - -require "groonga/client/searcher/raw_request" - -module Groonga - class Client - class Searcher - module Select - class Request < RawRequest - def initialize(table_or_parameters) - if table_or_parameters.respond_to?(:to_parameters) - parameters = table_or_parameters - else - table_name = table_or_parameters - parameters = RequestParameter.new(:table, table_name) - end - super("select", parameters) - end - - def result_set - @result_set ||= create_result_set - end - - def match_columns(value) - add_parameter(OverwriteMerger, - MatchColumnsParameter.new(value)) - end - - def query(value) - add_parameter(QueryMerger, - RequestParameter.new(:query, value)) - end - - def filter(expression, values=nil) - add_parameter(FilterMerger, - FilterParameter.new(expression, values)) - end - - def output_columns(value) - add_parameter(OverwriteMerger, - OutputColumnsParameter.new(value)) - end - - def sortby(value) - add_parameter(OverwriteMerger, - SortbyParameter.new(value)) - end - alias_method :sort, :sortby - - def offset(value) - parameter(:offset, value) - end - - def limit(value) - parameter(:limit, value) - end - - def paginate(page, per_page: 10) - page ||= 1 - page = page.to_i - if page <= 0 - offset = 0 - else - offset = per_page * (page - 1) - end - offset(offset).limit(per_page) - end - - private - def create_request(parameters) - self.class.new(parameters) - end - - def create_result_set - result_set = ResultSet.new(response) - if paginated? and defined?(Kaminari) - result_set.extend(Kaminari::ConfigurationMethods::ClassMethods) - result_set.extend(Kaminari::PageScopeMethods) - end - result_set - end - - def paginated? - parameters = to_parameters - parameters.key?(:offset) and parameters.key?(:limit) - end - end - - # @private - class QueryMerger < ParameterMerger - def to_parameters - params1 =****@param*****_parameters - params2 =****@param*****_parameters - params = params1.merge(params2) - query1 = params1[:query] - query2 = params2[:query] - if query1.present? and query2.present? - params[:query] = "(#{query1}) (#{query2})" - else - params[:query] = (query1 || query2) - end - params - end - end - - # @private - class FilterMerger < ParameterMerger - def to_parameters - params1 =****@param*****_parameters - params2 =****@param*****_parameters - params = params1.merge(params2) - filter1 = params1[:filter] - filter2 = params2[:filter] - if filter1.present? and filter2.present? - params[:filter] = "(#{filter1}) && (#{filter2})" - else - params[:filter] = (filter1 || filter2) - end - params - end - end - - # @private - class MatchColumnsParameter - def initialize(match_columns) - @match_columns = match_columns - end - - def to_parameters - if @match_columns.blank? - {} - else - case @match_columns - when ::Array - match_columns = @match_columns.join(", ") - when Symbol - match_columns = @match_columns.to_s - else - match_columns = @match_columns - end - { - match_columns: match_columns, - } - end - end - end - - # @private - class FilterParameter - def initialize(expression, values) - @expression = expression - @values = values - end - - def to_parameters - if****@expre*****? - {} - else - if****@value*****? - expression = @expression - else - escaped_values = {} - @values.each do |key, value| - escaped_values[key] = escape_filter_value(value) - end - expression = @expression % escaped_values - end - { - filter: expression, - } - end - end - - private - def escape_filter_value(value) - case value - when Numeric - value - when TrueClass, FalseClass - value - when NilClass - "null" - when String - ScriptSyntax.format_string(value) - when Symbol - ScriptSyntax.format_string(value.to_s) - when ::Array - escaped_value = "[" - value.each_with_index do |element, i| - escaped_value << ", " if i > 0 - escaped_value << escape_filter_value(element) - end - escaped_value << "]" - escaped_value - when ::Hash - escaped_value = "{" - value.each_with_index do |(k, v), i| - escaped_value << ", " if i > 0 - escaped_value << escape_filter_value(k.to_s) - escaped_value << ": " - escaped_value << escape_filter_value(v) - end - escaped_value << "}" - escaped_value - else - value - end - end - end - - # @private - class OutputColumnsParameter - def initialize(output_columns) - @output_columns = output_columns - end - - def to_parameters - if @output_columns.blank? - {} - else - case @output_columns - when ::Array - output_columns = @output_columns.join(", ") - when Symbol - output_columns = @output_columns.to_s - else - output_columns = @output_columns - end - parameters = { - output_columns: output_columns, - } - if output_columns.include?("(") - parameters[:command_version] = "2" - end - parameters - end - end - end - - # @private - class SortbyParameter - def initialize(sortby) - @sortby = sortby - end - - def to_parameters - if****@sortb*****? - {} - else - case @sortby - when ::Array - sortby =****@sortb*****(&:to_s).join(", ") - when Symbol - sortby =****@sortb*****_s - else - sortby = @sortby - end - { - sortby: sortby, - } - end - end - end - end - end - end -end Deleted: lib/groonga/client/searcher/select/result_set.rb (+0 -117) 100644 =================================================================== --- lib/groonga/client/searcher/select/result_set.rb 2016-06-09 17:44:14 +0900 (1570a27) +++ /dev/null @@ -1,117 +0,0 @@ -# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -module Groonga - class Client - class Searcher - module Select - class ResultSet - def initialize(response) - @response = response - end - - def n_hits - @response.n_hits - end - # For Kaminari - alias_method :total_count, :n_hits - - # For Kaminari - def limit_value - (@response.command[:limit] || 10).to_i - end - - # For Kaminari - def offset_value - (@response.command[:offset] || 0).to_i - end - - def records - @records ||= build_records - end - - def sources - @sources ||= fetch_sources - end - - def drilldowns - @response.drilldowns - end - - private - def build_records - @response.records.collect.with_index do |record, i| - Record.new(self, i, record) - end - end - - def fetch_sources - source_ids = {} - records.collect do |record| - model_name, id = record["_key"].split(/-/, 2) - source_ids[model_name] ||= [] - source_ids[model_name] << id - end - sources = {} - source_ids.each do |model_name, ids| - model_name.constantize.find(ids).each_with_index do |model, i| - sources["#{model_name}-#{ids[i]}"] = model - end - end - records.collect do |record| - sources[record["_key"]] - end - end - - class Record - def initialize(result_set, nth, raw_record) - @result_set = result_set - @nth = nth - @raw_record = raw_record - end - - def source - @result_set.sources[@nth] - end - - def [](name) - @raw_record[normalize_name(name)] - end - - def method_missing(name, *args, &block) - return super unless args.empty? - return super unless @raw_record.key?(name.to_s) - - self[name] - end - - def respond_to_missing?(name, *args, &block) - return super unless args.empty? - return super unless @raw_record.key?(name.to_s) - - @raw_record.key?(normalize_name(name)) - end - - private - def normalize_name(name) - name.to_s - end - end - end - end - end - end -end Added: lib/groonga/client/searcher/select_request.rb (+62 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga/client/searcher/select_request.rb 2016-12-09 18:56:33 +0900 (83d2d3f) @@ -0,0 +1,62 @@ +# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +module Groonga + class Client + class Searcher + module SelectRequest + # For backward compatibility + def result_set + response + end + + private + def create_response + response = super + response.extend(SourcesSupport) + response + end + + module SourcesSupport + def sources + @sources ||= fetch_sources + end + + private + def fetch_sources + source_ids = {} + records.collect do |record| + model_name, id = record._key.split(/-/, 2) + source_ids[model_name] ||= [] + source_ids[model_name] << id + end + sources = {} + source_ids.each do |model_name, ids| + model_name.constantize.find(ids).each_with_index do |model, i| + sources["#{model_name}-#{ids[i]}"] = model + end + end + records.collect do |record| + source = sources[record._key] + record.source = source + source + end + end + end + end + end + end +end -------------- next part -------------- HTML����������������������������...Download