diff --git a/.rubocop.yml b/.rubocop.yml index 310aa0c8..9544655c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -44,7 +44,7 @@ Style/GuardClause: MinBodyLength: 4 Style/HashSyntax: - EnforcedStyle: hash_rockets + Enabled: False Style/IndentHash: EnforcedStyle: consistent diff --git a/lib/protobuf.rb b/lib/protobuf.rb index 69139e2c..ccd22ccf 100644 --- a/lib/protobuf.rb +++ b/lib/protobuf.rb @@ -1,14 +1,27 @@ require 'base64' +require 'json' require 'logger' require 'pp' require 'socket' require 'stringio' -require 'active_support/core_ext/object/blank' -require 'active_support/core_ext/object/try' -require 'active_support/inflector' -require 'active_support/json' -require 'active_support/notifications' +# rubocop:disable Lint/HandleExceptions +begin + require 'active_support/lazy_load_hooks' +rescue LoadError +end +begin + require 'active_support/notifications' +rescue LoadError +end +# rubocop:enable Lint/HandleExceptions + +require 'protobuf/refinements/string/classify' +require 'protobuf/refinements/string/constantize' + +using Protobuf::Refinements::String::Classify +using Protobuf::Refinements::String::Constantize + # Under MRI, this optimizes proto decoding by around 15% in tests. # When unavailable, we fall to pure Ruby. # rubocop:disable Lint/HandleExceptions @@ -55,17 +68,33 @@ class << self end def self.after_server_bind(&block) + unless Object.const_defined?('::ActiveSupport::Notifications') + warn_no_active_support_notifications('after_server_bind') + return + end + ::ActiveSupport::Notifications.subscribe('after_server_bind') do |*args| block.call(*args) end end def self.before_server_bind(&block) + unless Object.const_defined?('::ActiveSupport::Notifications') + warn_no_active_support_notifications('before_server_bind') + return + end + ::ActiveSupport::Notifications.subscribe('before_server_bind') do |*args| block.call(*args) end end + def self.warn_no_active_support_notifications(method) + return if ENV['PROTOBUF_SUPPRESS_NO_ACTIVE_SUPPORT_NOTIFICATIONS_WARNING'] == '1' + + STDERR.puts("[WARN] ActiveSupport::Notifications is not loaded. To use `Protobuf.#{method}`, you need to require 'active_support/notifications'") + end + def self.client_host @client_host ||= Socket.gethostname end diff --git a/lib/protobuf/cli.rb b/lib/protobuf/cli.rb index 35c6d936..c1bb6c11 100644 --- a/lib/protobuf/cli.rb +++ b/lib/protobuf/cli.rb @@ -1,11 +1,15 @@ -require 'active_support/core_ext/hash/keys' -require 'active_support/inflector' - require 'thor' require 'protobuf/version' require 'protobuf/logging' require 'protobuf/rpc/servers/socket_runner' require 'protobuf/rpc/servers/zmq_runner' +require 'protobuf/refinements/hash/symbolize_keys' +require 'protobuf/refinements/string/classify' +require 'protobuf/refinements/string/constantize' + +using Protobuf::Refinements::Hash::SymbolizeKeys +using Protobuf::Refinements::String::Classify +using Protobuf::Refinements::String::Constantize module Protobuf class CLI < ::Thor @@ -240,14 +244,14 @@ def shutdown_server def start_server debug_say('Running server') - ::ActiveSupport::Notifications.instrument("before_server_bind") + ::ActiveSupport::Notifications.instrument("before_server_bind") if Object.const_defined?('::ActiveSupport::Notifications') runner.run do logger.info do "pid #{::Process.pid} -- #{mode} RPC Server listening at #{options.host}:#{options.port}" end - ::ActiveSupport::Notifications.instrument("after_server_bind") + ::ActiveSupport::Notifications.instrument("after_server_bind") if Object.const_defined?('::ActiveSupport::Notifications') end logger.info { 'Shutdown complete' } diff --git a/lib/protobuf/code_generator.rb b/lib/protobuf/code_generator.rb index 7b04a32c..8ef70ba2 100644 --- a/lib/protobuf/code_generator.rb +++ b/lib/protobuf/code_generator.rb @@ -1,4 +1,3 @@ -require 'active_support/core_ext/module/aliasing' require 'protobuf/generators/file_generator' module Protobuf diff --git a/lib/protobuf/deprecation.rb b/lib/protobuf/deprecation.rb index 8c0d415e..d1bc1328 100644 --- a/lib/protobuf/deprecation.rb +++ b/lib/protobuf/deprecation.rb @@ -1,7 +1,20 @@ -require 'active_support/deprecation' +begin + require 'active_support/deprecation' +rescue LoadError # rubocop:disable Lint/HandleExceptions +end module Protobuf - if ::ActiveSupport::Deprecation.is_a?(Class) + if !Object.const_defined?('::ActiveSupport::Deprecation') + class NullDeprecator + def initialize(*); end + def define_deprecated_methods(*); end + def deprecate_methods(*); end + attr_accessor :silenced, :behavior + end + + Deprecation = NullDeprecator.clone + FieldDeprecation = NullDeprecator.clone + elsif ::ActiveSupport::Deprecation.is_a?(Class) class DeprecationBase < ::ActiveSupport::Deprecation def deprecate_methods(*args) deprecation_options = { :deprecator => self } diff --git a/lib/protobuf/enum.rb b/lib/protobuf/enum.rb index c0a2a809..beba312f 100644 --- a/lib/protobuf/enum.rb +++ b/lib/protobuf/enum.rb @@ -1,5 +1,9 @@ require 'delegate' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try + ## # Adding extension to Numeric until # we can get people to stop calling #value diff --git a/lib/protobuf/field/base_field.rb b/lib/protobuf/field/base_field.rb index 6f2f4f7f..6341ae88 100644 --- a/lib/protobuf/field/base_field.rb +++ b/lib/protobuf/field/base_field.rb @@ -1,7 +1,9 @@ -require 'active_support/core_ext/hash/slice' require 'protobuf/field/field_array' require 'protobuf/field/field_hash' require 'protobuf/field/base_field_object_definitions' +require 'protobuf/refinements/hash/slice' + +using Protobuf::Refinements::Hash::Slice module Protobuf module Field diff --git a/lib/protobuf/field/base_field_object_definitions.rb b/lib/protobuf/field/base_field_object_definitions.rb index 93a93226..8a3dba1d 100644 --- a/lib/protobuf/field/base_field_object_definitions.rb +++ b/lib/protobuf/field/base_field_object_definitions.rb @@ -1,3 +1,7 @@ +require 'protobuf/refinements/kernel/blank' + +using Protobuf::Refinements::Kernel::Blank + module Protobuf module Field module BaseFieldObjectDefinitions diff --git a/lib/protobuf/field/bytes_field.rb b/lib/protobuf/field/bytes_field.rb index e120835c..eb687e89 100644 --- a/lib/protobuf/field/bytes_field.rb +++ b/lib/protobuf/field/bytes_field.rb @@ -70,7 +70,7 @@ def coerce!(value) end end - def json_encode(value, options={}) + def json_encode(value, _options = {}) Base64.strict_encode64(value) end end diff --git a/lib/protobuf/field/enum_field.rb b/lib/protobuf/field/enum_field.rb index 12867adf..add8bf06 100644 --- a/lib/protobuf/field/enum_field.rb +++ b/lib/protobuf/field/enum_field.rb @@ -37,7 +37,7 @@ def coerce!(value) type_class.fetch(value) || fail(TypeError, "Invalid Enum value: #{value.inspect} for #{name}") end - def json_encode(value, options={}) + def json_encode(value, _options = {}) enum = type_class.enums.find { |e| e.to_i == value } enum.to_s(:name) end diff --git a/lib/protobuf/field/string_field.rb b/lib/protobuf/field/string_field.rb index 551bdd23..08dc1136 100644 --- a/lib/protobuf/field/string_field.rb +++ b/lib/protobuf/field/string_field.rb @@ -43,7 +43,7 @@ def encode(value) "#{::Protobuf::Field::VarintField.encode(value_to_encode.bytesize)}#{value_to_encode}" end - def json_encode(value, options={}) + def json_encode(value, _options = {}) value end end diff --git a/lib/protobuf/generators/enum_generator.rb b/lib/protobuf/generators/enum_generator.rb index 1088e3ed..9b836ddc 100644 --- a/lib/protobuf/generators/enum_generator.rb +++ b/lib/protobuf/generators/enum_generator.rb @@ -1,5 +1,8 @@ require 'protobuf/generators/base' require 'protobuf/generators/option_generator' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try module Protobuf module Generators diff --git a/lib/protobuf/generators/field_generator.rb b/lib/protobuf/generators/field_generator.rb index 68652fcc..c718d90a 100644 --- a/lib/protobuf/generators/field_generator.rb +++ b/lib/protobuf/generators/field_generator.rb @@ -1,4 +1,7 @@ require 'protobuf/generators/base' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try module Protobuf module Generators diff --git a/lib/protobuf/generators/file_generator.rb b/lib/protobuf/generators/file_generator.rb index f94d2264..18af88d5 100644 --- a/lib/protobuf/generators/file_generator.rb +++ b/lib/protobuf/generators/file_generator.rb @@ -1,6 +1,9 @@ require 'set' require 'protobuf/generators/base' require 'protobuf/generators/group_generator' +require 'protobuf/refinements/string/constantize' + +using Protobuf::Refinements::String::Constantize module Protobuf module Generators diff --git a/lib/protobuf/generators/group_generator.rb b/lib/protobuf/generators/group_generator.rb index 33e7b5be..81b81683 100644 --- a/lib/protobuf/generators/group_generator.rb +++ b/lib/protobuf/generators/group_generator.rb @@ -4,6 +4,9 @@ require 'protobuf/generators/message_generator' require 'protobuf/generators/option_generator' require 'protobuf/generators/service_generator' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try module Protobuf module Generators diff --git a/lib/protobuf/generators/service_generator.rb b/lib/protobuf/generators/service_generator.rb index f7e9febd..a65d991b 100644 --- a/lib/protobuf/generators/service_generator.rb +++ b/lib/protobuf/generators/service_generator.rb @@ -1,5 +1,8 @@ require 'protobuf/generators/base' require 'protobuf/generators/option_generator' +require 'protobuf/refinements/string/underscore' + +using Protobuf::Refinements::String::Underscore module Protobuf module Generators diff --git a/lib/protobuf/lifecycle.rb b/lib/protobuf/lifecycle.rb index a17aac5c..96ee5501 100644 --- a/lib/protobuf/lifecycle.rb +++ b/lib/protobuf/lifecycle.rb @@ -5,8 +5,10 @@ def register(event_name) fail "Lifecycle register must have a block" unless block_given? event_name = normalized_event_name(event_name) - ::ActiveSupport::Notifications.subscribe(event_name) do |_name, _start, _finish, _id, args| - yield(*args) + if Object.const_defined?('::ActiveSupport::Notifications') + ::ActiveSupport::Notifications.subscribe(event_name) do |_name, _start, _finish, _id, args| + yield(*args) + end end end alias :on register @@ -14,16 +16,18 @@ def register(event_name) def trigger(event_name, *args) event_name = normalized_event_name(event_name) - ::ActiveSupport::Notifications.instrument(event_name, args) + ::ActiveSupport::Notifications.instrument(event_name, args) if Object.const_defined?('::ActiveSupport::Notifications') end - replacement = ::ActiveSupport::Notifications + if Object.const_defined?('::ActiveSupport::Notifications') + replacement = ::ActiveSupport::Notifications - ::Protobuf.deprecator.deprecate_methods( - self, - :register => "#{replacement}.#{replacement.method(:subscribe).name}".to_sym, - :trigger => "#{replacement}.#{replacement.method(:instrument).name}".to_sym, - ) + ::Protobuf.deprecator.deprecate_methods( + self, + :register => "#{replacement}.#{replacement.method(:subscribe).name}".to_sym, + :trigger => "#{replacement}.#{replacement.method(:instrument).name}".to_sym, + ) + end def normalized_event_name(event_name) event_name.to_s.downcase diff --git a/lib/protobuf/message.rb b/lib/protobuf/message.rb index b5b723f5..80eda29a 100644 --- a/lib/protobuf/message.rb +++ b/lib/protobuf/message.rb @@ -1,6 +1,11 @@ require 'protobuf/message/fields' require 'protobuf/message/serialization' require 'protobuf/varint' +require 'protobuf/refinements/string/camelize' +require 'protobuf/refinements/string/underscore' + +using Protobuf::Refinements::String::Camelize +using Protobuf::Refinements::String::Underscore module Protobuf class Message @@ -174,7 +179,17 @@ def to_json_hash(options = {}) value end - if proto3 && (hashed_value.nil? || value == field.class.default rescue field.default rescue nil) + if proto3 && ( + begin + begin + hashed_value.nil? || value == field.class.default + rescue + field.default + end + rescue + nil + end + ) result.delete(field.name) else key = proto3 ? field.name.to_s.camelize(:lower).to_sym : field.name diff --git a/lib/protobuf/message/fields.rb b/lib/protobuf/message/fields.rb index d07e1eff..f1c9d3a3 100644 --- a/lib/protobuf/message/fields.rb +++ b/lib/protobuf/message/fields.rb @@ -1,4 +1,9 @@ require "set" +require 'protobuf/refinements/kernel/blank' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Kernel::Blank +using Protobuf::Refinements::Object::Try module Protobuf class Message diff --git a/lib/protobuf/optionable.rb b/lib/protobuf/optionable.rb index 34b00c93..e6dc0a6f 100644 --- a/lib/protobuf/optionable.rb +++ b/lib/protobuf/optionable.rb @@ -1,3 +1,7 @@ +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try + module Protobuf module Optionable module ClassMethods diff --git a/lib/protobuf/refinements/hash/slice.rb b/lib/protobuf/refinements/hash/slice.rb new file mode 100644 index 00000000..95efdb85 --- /dev/null +++ b/lib/protobuf/refinements/hash/slice.rb @@ -0,0 +1,36 @@ +# Adapted from: https://github.com/rubyworks/facets/blob/master/lib/core/facets/hash/slice.rb + +module Protobuf + module Refinements + module Hash + module Slice + refine ::Hash do + def slice(*keep_keys) + if block_given? + each do |k, v| + keep_keys << k if yield(k, v) + end + end + + keep_keys.each_with_object({}) do |key, hash| + hash[key] = fetch(key) if key?(key) + end + end + + def slice!(*keep_keys) + if block_given? + each do |k, v| + keep_keys << k if yield(k, v) + end + end + + rejected = keys - keep_keys + removed = {} + rejected.each { |k| removed[k] = delete(k) } + removed + end + end + end + end + end +end diff --git a/lib/protobuf/refinements/hash/symbolize_keys.rb b/lib/protobuf/refinements/hash/symbolize_keys.rb new file mode 100644 index 00000000..205003d0 --- /dev/null +++ b/lib/protobuf/refinements/hash/symbolize_keys.rb @@ -0,0 +1,21 @@ +# Adapted from: https://github.com/rails/rails/blob/v6.0.3.2/activesupport/lib/active_support/core_ext/hash/keys.rb + +module Protobuf + module Refinements + module Hash + module SymbolizeKeys + refine ::Hash do + def symbolize_keys + transform_keys do |key| + begin + key.to_sym + rescue + key + end + end + end + end + end + end + end +end diff --git a/lib/protobuf/refinements/kernel/blank.rb b/lib/protobuf/refinements/kernel/blank.rb new file mode 100644 index 00000000..e9dd480b --- /dev/null +++ b/lib/protobuf/refinements/kernel/blank.rb @@ -0,0 +1,70 @@ +# Adapted from: https://github.com/rubyworks/facets/blob/master/lib/core/facets/kernel/blank.rb + +module Protobuf + module Refinements + module Kernel + module Blank + refine ::Kernel do + def blank? + return empty? if respond_to?(:empty?) + !self + end + + def present? + !blank? + end + + def presence + self if present? + end + end + + refine ::NilClass do + def blank? + true + end + end + + refine ::FalseClass do + def blank? + true + end + end + + refine ::TrueClass do + def blank? + false + end + end + + refine ::Array do + alias_method :blank?, :empty? + end + + refine ::Hash do + alias_method :blank?, :empty? + end + + refine ::String do + def blank? + /\S/ !~ self + end + end + + refine ::Numeric do + def blank? + false + end + end + + if Object.const_defined?('::Delegator') + refine ::Delegator do + def present? + __getobj__.present? + end + end + end + end + end + end +end diff --git a/lib/protobuf/refinements/object/try.rb b/lib/protobuf/refinements/object/try.rb new file mode 100644 index 00000000..e2e6bba8 --- /dev/null +++ b/lib/protobuf/refinements/object/try.rb @@ -0,0 +1,57 @@ +# Adapted from: https://github.com/rails/rails/blob/v6.0.3.2/activesupport/lib/active_support/core_ext/object/try.rb + +module Protobuf + module Refinements + module Object + module Try + module Tryable #:nodoc: + def try(method_name = nil, *args, &b) + if method_name.nil? && block_given? + if b.arity == 0 + instance_eval(&b) + else + yield self + end + elsif respond_to?(method_name) + public_send(method_name, *args, &b) + end + end + ruby2_keywords(:try) if respond_to?(:ruby2_keywords, true) + + def try!(method_name = nil, *args, &b) + if method_name.nil? && block_given? + if b.arity == 0 + instance_eval(&b) + else + yield self + end + else + public_send(method_name, *args, &b) + end + end + ruby2_keywords(:try!) if respond_to?(:ruby2_keywords, true) + end + + refine ::Object do + include Tryable + end + + if Object.const_defined?('::Delegator') + refine ::Delegator do + include Tryable + end + end + + refine ::NilClass do + def try(_method_name = nil, *_args) + nil + end + + def try!(_method_name = nil, *_args) + nil + end + end + end + end + end +end diff --git a/lib/protobuf/refinements/string/camelize.rb b/lib/protobuf/refinements/string/camelize.rb new file mode 100644 index 00000000..7505974d --- /dev/null +++ b/lib/protobuf/refinements/string/camelize.rb @@ -0,0 +1,35 @@ +# Adapted from: https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/camelcase.rb + +module Protobuf + module Refinements + module String + module Camelize + refine ::String do + def camelize(*separators) + case separators.first + when Symbol, TrueClass, FalseClass, NilClass + first_letter = separators.shift + end + + separators = ['_', '\s'] if separators.empty? + + str = dup + + separators.each do |s| + str = str.gsub(/(?:#{s}+)([a-z])/) { $1.upcase } # rubocop:disable Style/PerlBackrefs + end + + case first_letter + when :upper, true + str = str.gsub(/(\A|\s)([a-z])/) { $1 + $2.upcase } # rubocop:disable Style/PerlBackrefs + when :lower, false + str = str.gsub(/(\A|\s)([A-Z])/) { $1 + $2.downcase } # rubocop:disable Style/PerlBackrefs + end + + str + end + end + end + end + end +end diff --git a/lib/protobuf/refinements/string/classify.rb b/lib/protobuf/refinements/string/classify.rb new file mode 100644 index 00000000..616efac6 --- /dev/null +++ b/lib/protobuf/refinements/string/classify.rb @@ -0,0 +1,19 @@ +# Adapted from: https://github.com/rails/rails/blob/v6.0.3.2/activesupport/lib/active_support/inflector/methods.rb + +require 'protobuf/refinements/string/camelize' + +module Protobuf + module Refinements + module String + module Classify + refine ::String do + using Protobuf::Refinements::String::Camelize + + def classify + split('/').map { |c| c.camelize(:upper) }.join('::') + end + end + end + end + end +end diff --git a/lib/protobuf/refinements/string/constantize.rb b/lib/protobuf/refinements/string/constantize.rb new file mode 100644 index 00000000..c0d75f18 --- /dev/null +++ b/lib/protobuf/refinements/string/constantize.rb @@ -0,0 +1,53 @@ +# Adapted from: https://github.com/rails/rails/blob/v6.0.3.2/activesupport/lib/active_support/inflector/methods.rb + +module Protobuf + module Refinements + module String + module Constantize + refine ::String do + def constantize + names = split("::") + + # Trigger a built-in NameError exception including the ill-formed constant in the message. + Object.const_get(self) if names.empty? + + # Remove the first blank element in case of '::ClassName' notation. + names.shift if names.size > 1 && names.first.empty? + + names.inject(Object) do |constant, name| + if constant == Object + constant.const_get(name) + else + candidate = constant.const_get(name) + next candidate if constant.const_defined?(name, false) + next candidate unless Object.const_defined?(name) + + # Go down the ancestors to check if it is owned directly. The check + # stops when we reach Object or the end of ancestors tree. + constant = constant.ancestors.inject(constant) do |const, ancestor| # rubocop:disable Style/EachWithObject + break const if ancestor == Object + break ancestor if ancestor.const_defined?(name, false) + const + end + + # owner is in Object, so raise + constant.const_get(name, false) + end + end + end + + def safe_constantize + constantize + rescue NameError => e + raise if e.name && !(to_s.split("::").include?(e.name.to_s) || + e.name.to_s == to_s) + rescue ArgumentError => e + raise unless /not missing constant #{const_regexp(self)}!$/.match?(e.message) + rescue LoadError => e + raise unless /Unable to autoload constant #{const_regexp(self)}/.match?(e.message) + end + end + end + end + end +end diff --git a/lib/protobuf/refinements/string/underscore.rb b/lib/protobuf/refinements/string/underscore.rb new file mode 100644 index 00000000..62a88620 --- /dev/null +++ b/lib/protobuf/refinements/string/underscore.rb @@ -0,0 +1,20 @@ +# Adapted from: https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb + +module Protobuf + module Refinements + module String + module Underscore + refine ::String do + def underscore(*_separators) + gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') + .gsub(/([a-z\d])([A-Z])/, '\1_\2') + .tr('-', '_') + .gsub(/\s/, '_') + .gsub(/__+/, '_') + .downcase + end + end + end + end + end +end diff --git a/lib/protobuf/rpc/client.rb b/lib/protobuf/rpc/client.rb index 74791d23..3e6c1cb5 100644 --- a/lib/protobuf/rpc/client.rb +++ b/lib/protobuf/rpc/client.rb @@ -135,6 +135,6 @@ def method_missing(method_name, *params) end - ActiveSupport.run_load_hooks(:protobuf_rpc_client, Client) + ActiveSupport.run_load_hooks(:protobuf_rpc_client, Client) if Object.const_defined?('::ActiveSupport::LazyLoadHooks') end end diff --git a/lib/protobuf/rpc/connectors/zmq.rb b/lib/protobuf/rpc/connectors/zmq.rb index 3d99fb96..be517d61 100644 --- a/lib/protobuf/rpc/connectors/zmq.rb +++ b/lib/protobuf/rpc/connectors/zmq.rb @@ -2,6 +2,9 @@ require "protobuf/rpc/connectors/base" require "protobuf/rpc/connectors/ping" require "protobuf/rpc/service_directory" +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try module Protobuf module Rpc diff --git a/lib/protobuf/rpc/middleware.rb b/lib/protobuf/rpc/middleware.rb index 3757b646..668f8830 100644 --- a/lib/protobuf/rpc/middleware.rb +++ b/lib/protobuf/rpc/middleware.rb @@ -21,5 +21,5 @@ def self.middleware Rpc.middleware.use(Rpc::Middleware::Logger) Rpc.middleware.use(Rpc::Middleware::ResponseEncoder) - ActiveSupport.run_load_hooks(:protobuf_rpc_middleware, Rpc) + ActiveSupport.run_load_hooks(:protobuf_rpc_middleware, Rpc) if Object.const_defined?('::ActiveSupport::LazyLoadHooks') end diff --git a/lib/protobuf/rpc/middleware/logger.rb b/lib/protobuf/rpc/middleware/logger.rb index 2c02b9a4..d833fa03 100644 --- a/lib/protobuf/rpc/middleware/logger.rb +++ b/lib/protobuf/rpc/middleware/logger.rb @@ -1,3 +1,7 @@ +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try + module Protobuf module Rpc module Middleware diff --git a/lib/protobuf/rpc/middleware/request_decoder.rb b/lib/protobuf/rpc/middleware/request_decoder.rb index c4efe823..5d8795ed 100644 --- a/lib/protobuf/rpc/middleware/request_decoder.rb +++ b/lib/protobuf/rpc/middleware/request_decoder.rb @@ -1,3 +1,9 @@ +require 'protobuf/refinements/string/constantize' +require 'protobuf/refinements/string/underscore' + +using Protobuf::Refinements::String::Constantize +using Protobuf::Refinements::String::Underscore + module Protobuf module Rpc module Middleware diff --git a/lib/protobuf/rpc/servers/socket_runner.rb b/lib/protobuf/rpc/servers/socket_runner.rb index 8d36182d..b866c124 100644 --- a/lib/protobuf/rpc/servers/socket_runner.rb +++ b/lib/protobuf/rpc/servers/socket_runner.rb @@ -1,3 +1,7 @@ +require 'protobuf/refinements/hash/symbolize_keys' + +using Protobuf::Refinements::Hash::SymbolizeKeys + module Protobuf module Rpc class SocketRunner diff --git a/lib/protobuf/rpc/servers/zmq/broker.rb b/lib/protobuf/rpc/servers/zmq/broker.rb index 2b12aee8..5f9e7278 100644 --- a/lib/protobuf/rpc/servers/zmq/broker.rb +++ b/lib/protobuf/rpc/servers/zmq/broker.rb @@ -1,4 +1,7 @@ require 'thread' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try module Protobuf module Rpc diff --git a/lib/protobuf/rpc/servers/zmq/server.rb b/lib/protobuf/rpc/servers/zmq/server.rb index b5dce25a..fb45e928 100644 --- a/lib/protobuf/rpc/servers/zmq/server.rb +++ b/lib/protobuf/rpc/servers/zmq/server.rb @@ -1,3 +1,4 @@ +require 'protobuf/refinements/object/try' require 'protobuf/rpc/servers/zmq/util' require 'protobuf/rpc/servers/zmq/worker' require 'protobuf/rpc/servers/zmq/broker' @@ -5,6 +6,8 @@ require 'securerandom' require 'thread' +using Protobuf::Refinements::Object::Try + module Protobuf module Rpc module Zmq diff --git a/lib/protobuf/rpc/servers/zmq/worker.rb b/lib/protobuf/rpc/servers/zmq/worker.rb index 85e4aa2a..82d20479 100644 --- a/lib/protobuf/rpc/servers/zmq/worker.rb +++ b/lib/protobuf/rpc/servers/zmq/worker.rb @@ -1,7 +1,10 @@ +require 'protobuf/refinements/object/try' require 'protobuf/rpc/server' require 'protobuf/rpc/servers/zmq/util' require 'thread' +using Protobuf::Refinements::Object::Try + module Protobuf module Rpc module Zmq diff --git a/lib/protobuf/rpc/servers/zmq_runner.rb b/lib/protobuf/rpc/servers/zmq_runner.rb index bfab6738..1c392539 100644 --- a/lib/protobuf/rpc/servers/zmq_runner.rb +++ b/lib/protobuf/rpc/servers/zmq_runner.rb @@ -1,5 +1,10 @@ require 'ostruct' require 'thread' +require 'protobuf/refinements/hash/symbolize_keys' +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Hash::SymbolizeKeys +using Protobuf::Refinements::Object::Try module Protobuf module Rpc diff --git a/lib/protobuf/rpc/service.rb b/lib/protobuf/rpc/service.rb index cc1974ad..142537e1 100644 --- a/lib/protobuf/rpc/service.rb +++ b/lib/protobuf/rpc/service.rb @@ -167,6 +167,6 @@ def rpc_failed(message) end end - ActiveSupport.run_load_hooks(:protobuf_rpc_service, Service) + ActiveSupport.run_load_hooks(:protobuf_rpc_service, Service) if Object.const_defined?('::ActiveSupport::LazyLoadHooks') end end diff --git a/lib/protobuf/rpc/service_directory.rb b/lib/protobuf/rpc/service_directory.rb index 7c467358..af1e812f 100644 --- a/lib/protobuf/rpc/service_directory.rb +++ b/lib/protobuf/rpc/service_directory.rb @@ -5,8 +5,11 @@ require 'thread' require 'timeout' +require 'protobuf/refinements/object/try' require 'protobuf/rpc/dynamic_discovery.pb' +using Protobuf::Refinements::Object::Try + module Protobuf module Rpc def self.service_directory @@ -254,6 +257,7 @@ def run end def trigger(action, listing) + return unless Object.const_defined?('::ActiveSupport::Notifications') ::ActiveSupport::Notifications.instrument("directory.listing.#{action}", :listing => listing) end end diff --git a/lib/protobuf/rpc/stat.rb b/lib/protobuf/rpc/stat.rb index 841181a3..fe7dd127 100644 --- a/lib/protobuf/rpc/stat.rb +++ b/lib/protobuf/rpc/stat.rb @@ -1,8 +1,11 @@ require 'date' require 'time' require 'protobuf/logging' +require 'protobuf/refinements/object/try' require 'protobuf/rpc/rpc.pb' +using Protobuf::Refinements::Object::Try + module Protobuf module Rpc class Stat diff --git a/protobuf-cucumber.gemspec b/protobuf-cucumber.gemspec index a38aa0cd..020a3fc0 100644 --- a/protobuf-cucumber.gemspec +++ b/protobuf-cucumber.gemspec @@ -21,7 +21,8 @@ require "protobuf/version" # Hack, as Rails 5 requires Ruby version >= 2.2.2. active_support_max_version = "< 5" if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.2.2") - s.add_dependency "activesupport", '>= 3.2', active_support_max_version + s.add_development_dependency "activesupport", '>= 3.2', active_support_max_version + s.add_dependency 'middleware' s.add_dependency 'thor' s.add_dependency 'thread_safe' diff --git a/spec/bin/protoc-gen-ruby_spec.rb b/spec/bin/protoc-gen-ruby_spec.rb index 1df8b155..9991a8c5 100644 --- a/spec/bin/protoc-gen-ruby_spec.rb +++ b/spec/bin/protoc-gen-ruby_spec.rb @@ -17,7 +17,7 @@ pipe.close_write # needed so we can implicitly read until EOF response_bytes = pipe.read response = ::Google::Protobuf::Compiler::CodeGeneratorResponse.decode(response_bytes) - expect(response.file.first.content).to include("module #{package.titleize}") + expect(response.file.first.content).to include("module Test") end end end diff --git a/spec/lib/protobuf/cli_spec.rb b/spec/lib/protobuf/cli_spec.rb index ea78ed0d..911a8f98 100644 --- a/spec/lib/protobuf/cli_spec.rb +++ b/spec/lib/protobuf/cli_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'active_support/notifications' require 'protobuf/cli' RSpec.describe ::Protobuf::CLI do diff --git a/spec/lib/protobuf/enum_spec.rb b/spec/lib/protobuf/enum_spec.rb index f0ace1d6..511acb1c 100644 --- a/spec/lib/protobuf/enum_spec.rb +++ b/spec/lib/protobuf/enum_spec.rb @@ -1,6 +1,10 @@ require 'spec_helper' require PROTOS_PATH.join('enum.pb') +require 'protobuf/refinements/object/try' + +using Protobuf::Refinements::Object::Try + RSpec.describe Protobuf::Enum do describe 'class dsl' do diff --git a/spec/lib/protobuf/lifecycle_spec.rb b/spec/lib/protobuf/lifecycle_spec.rb index d1414477..d48d3020 100644 --- a/spec/lib/protobuf/lifecycle_spec.rb +++ b/spec/lib/protobuf/lifecycle_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'active_support/notifications' require 'protobuf/lifecycle' RSpec.describe ::Protobuf::Lifecycle do diff --git a/spec/lib/protobuf/message_spec.rb b/spec/lib/protobuf/message_spec.rb index 6a3f6d9c..47fe9890 100644 --- a/spec/lib/protobuf/message_spec.rb +++ b/spec/lib/protobuf/message_spec.rb @@ -5,6 +5,10 @@ require PROTOS_PATH.join('resource.pb') require PROTOS_PATH.join('enum.pb') +require 'protobuf/refinements/kernel/blank' + +using Protobuf::Refinements::Kernel::Blank + RSpec.describe Protobuf::Message do describe '.decode' do diff --git a/spec/lib/protobuf/rpc/service_directory_spec.rb b/spec/lib/protobuf/rpc/service_directory_spec.rb index 268bb021..05cfefcb 100644 --- a/spec/lib/protobuf/rpc/service_directory_spec.rb +++ b/spec/lib/protobuf/rpc/service_directory_spec.rb @@ -1,5 +1,6 @@ require 'spec_helper' +require 'active_support/notifications' require 'protobuf/rpc/service_directory' RSpec.describe ::Protobuf::Rpc::ServiceDirectory do diff --git a/spec/lib/protobuf/rpc/stat_spec.rb b/spec/lib/protobuf/rpc/stat_spec.rb index 9abd627f..e629914b 100644 --- a/spec/lib/protobuf/rpc/stat_spec.rb +++ b/spec/lib/protobuf/rpc/stat_spec.rb @@ -1,6 +1,5 @@ require 'spec_helper' require 'timecop' -require 'active_support/core_ext/numeric/time' RSpec.describe ::Protobuf::Rpc::Stat do @@ -24,14 +23,14 @@ describe 'server mode' do it 'describes a server response to a client' do - ::Timecop.freeze(10.minutes.ago) do + ::Timecop.freeze(Time.now - 10 * 60) do stats = ::Protobuf::Rpc::Stat.new(:SERVER) stats.client = 'myserver1' stats.dispatcher = double('dispatcher', :service => BarService.new(:find_bars)) stats.request_size = 43 stats.response_size = 1302 - ::Timecop.freeze(1.62.seconds.from_now) do + ::Timecop.freeze(Time.now + 1.62) do stats.stop expect(stats.to_s).to eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/1302B - 1.62s - OK - #{::Time.now.iso8601}" end @@ -51,7 +50,7 @@ describe 'client mode' do it 'describes a client request to a server' do - ::Timecop.freeze(10.minutes.ago) do + ::Timecop.freeze(Time.now - 10 * 60) do stats = ::Protobuf::Rpc::Stat.new(:CLIENT) stats.server = ['30000', 'myserver1.myhost.com'] stats.service = 'Foo::BarService' @@ -59,7 +58,7 @@ stats.request_size = 37 stats.response_size = 12345 - ::Timecop.freeze(0.832.seconds.from_now) do + ::Timecop.freeze(Time.now + 0.832) do stats.stop expect(stats.to_s).to eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/12345B - 0.832s - OK - #{::Time.now.iso8601}" end @@ -69,7 +68,7 @@ describe 'error log' do it 'resolves error to a string' do - ::Timecop.freeze(10.minutes.ago) do + ::Timecop.freeze(Time.now - 10 * 60) do stats = ::Protobuf::Rpc::Stat.new(:CLIENT) stats.server = ['30000', 'myserver1.myhost.com'] stats.service = 'Foo::BarService' @@ -78,7 +77,7 @@ stats.request_size = 37 stats.response_size = 12345 - ::Timecop.freeze(0.832.seconds.from_now) do + ::Timecop.freeze(Time.now + 0.832) do stats.stop expect(stats.to_s).to eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/12345B - 0.832s - RPC_ERROR - #{::Time.now.iso8601}" end diff --git a/spec/support/packed_field.rb b/spec/support/packed_field.rb index 9b18efee..aba61357 100644 --- a/spec/support/packed_field.rb +++ b/spec/support/packed_field.rb @@ -1,3 +1,7 @@ +require 'protobuf/refinements/string/underscore' + +using Protobuf::Refinements::String::Underscore + if defined?(RSpec) shared_examples_for :packable_field do |field_klass| diff --git a/spec/support/server.rb b/spec/support/server.rb index 8a0203b6..9c5b7635 100644 --- a/spec/support/server.rb +++ b/spec/support/server.rb @@ -1,7 +1,5 @@ require 'ostruct' -require 'active_support/core_ext/hash/reverse_merge' - require 'spec_helper' require 'protobuf/logging' require 'protobuf/rpc/server' @@ -25,13 +23,13 @@ class StubServer def initialize(options = {}) self.options = OpenStruct.new( - options.reverse_merge( + { :host => '127.0.0.1', :port => 9399, :worker_port => 9400, :delay => 0, :server => Protobuf::Rpc::Socket::Server, - ), + }.merge(options), ) start