Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Remove ActiveSupport #2

Open
wants to merge 19 commits into
base: 3-10-cucumber
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
caa655c
[Remove activesupport] Remove require for unused 'active_support/infl…
ZimbiX Feb 3, 2021
98481bc
[Remove activesupport] Replace 'active_support/core_ext/object/blank'…
ZimbiX Feb 3, 2021
8b62651
[Remove activesupport] Replace 'active_support/core_ext/object/try' w…
ZimbiX Feb 3, 2021
4e4f028
[Remove activesupport] Replace 'active_support/core_ext/hash/keys' wi…
ZimbiX Feb 3, 2021
e5a4384
[Remove activesupport] Remove require for unused 'active_support/core…
ZimbiX Feb 3, 2021
2543ba9
[Remove activesupport] Expand Blank refinement with code from facets
ZimbiX Feb 3, 2021
c163185
[Remove activesupport] Substitute sole usage of #titleize
ZimbiX Feb 3, 2021
c9f2b81
[Remove activesupport] Add refinements for #camelize, #uderscore, #co…
ZimbiX Feb 3, 2021
5c1d572
[Remove activesupport] Replace 'active_support/core_ext/numeric/time'…
ZimbiX Feb 3, 2021
b785a2f
[Remove activesupport] Fix Blank refinement for Enum (SimpleDelegator)
ZimbiX Feb 3, 2021
8f0d595
[Remove activesupport] Replace 'active_support/core_ext/hash/slice' w…
ZimbiX Feb 3, 2021
cfe46f9
[Remove activesupport] Reorder merge and remove 'active_support/core_…
ZimbiX Feb 3, 2021
014fa39
[Remove activesupport] Expose current dependency on 'active_support/l…
ZimbiX Feb 3, 2021
55df098
[Remove activesupport] Switch to inbuilt json from 'active_support/json'
ZimbiX Feb 3, 2021
704f0d1
[Remove activesupport] Only use ActiveSupport::Notifications and Acti…
ZimbiX Feb 3, 2021
cf9851e
[Remove activesupport] Demote activesupport to a development dependency
ZimbiX Feb 3, 2021
488c1f5
[Remove activesupport] Fix Delegator refining failing when it's not b…
ZimbiX Feb 3, 2021
a576f2f
Make existing code satisfy RuboCop
ZimbiX Feb 3, 2021
3f71a77
[Remove activesupport] Satisfy RuboCop
ZimbiX Feb 3, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Style/GuardClause:
MinBodyLength: 4

Style/HashSyntax:
EnforcedStyle: hash_rockets
Enabled: False

Style/IndentHash:
EnforcedStyle: consistent
Expand Down
39 changes: 34 additions & 5 deletions lib/protobuf.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions lib/protobuf/cli.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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' }
Expand Down
1 change: 0 additions & 1 deletion lib/protobuf/code_generator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'active_support/core_ext/module/aliasing'
require 'protobuf/generators/file_generator'

module Protobuf
Expand Down
17 changes: 15 additions & 2 deletions lib/protobuf/deprecation.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
4 changes: 4 additions & 0 deletions lib/protobuf/enum.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/protobuf/field/base_field.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/protobuf/field/base_field_object_definitions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require 'protobuf/refinements/kernel/blank'

using Protobuf::Refinements::Kernel::Blank

module Protobuf
module Field
module BaseFieldObjectDefinitions
Expand Down
2 changes: 1 addition & 1 deletion lib/protobuf/field/bytes_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/protobuf/field/enum_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/protobuf/field/string_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/protobuf/generators/enum_generator.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/protobuf/generators/field_generator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'protobuf/generators/base'
require 'protobuf/refinements/object/try'

using Protobuf::Refinements::Object::Try

module Protobuf
module Generators
Expand Down
3 changes: 3 additions & 0 deletions lib/protobuf/generators/file_generator.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/protobuf/generators/group_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/protobuf/generators/service_generator.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 13 additions & 9 deletions lib/protobuf/lifecycle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ 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

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
Expand Down
17 changes: 16 additions & 1 deletion lib/protobuf/message.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/protobuf/message/fields.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/protobuf/optionable.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require 'protobuf/refinements/object/try'

using Protobuf::Refinements::Object::Try

module Protobuf
module Optionable
module ClassMethods
Expand Down
36 changes: 36 additions & 0 deletions lib/protobuf/refinements/hash/slice.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions lib/protobuf/refinements/hash/symbolize_keys.rb
Original file line number Diff line number Diff line change
@@ -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
Loading