Skip to content

Commit

Permalink
Initial batch of swapping Active Support extensions for in-house alte…
Browse files Browse the repository at this point in the history
…rnatives (WIP) (#881)

* Initial batch of swapping Active Support extensions for in-house alternatives

- new `within?` method offers some unique semantics

* Refactor core exts, ensure tests pass

* Fix more tests, add string indentation

- Remove unneeded Active Support extensions once thought to be necessary for Thor actions

* Move core extensions to new Foundation gem…more to follow!

* Add more functionality to Foundation

- translation safety
- module nesting
- also clean up outdated subclass tracking for route class files

* Support Ruby 3+ pattern matching for Collection and Resource::Base

* Convert many Foundation extensions to refinements for cleaner monkeypatching

- new .() and .with() features for Symbol

* Load in HashWithDotAccess 2.0, remove AS dependency of indifferent access

* Add deep_dup to Foundation, remove AS dependency

* Move HashWithDotAccess dep over to Foundation, remove more AS deps

* Rewrite Current and "class attributes" to remove AS dep

* Remove Active Model, AS callbacks, spurious reference to indifferent access

* Move Ansi color helpers to Foundation, remove colorator dep

* various fixes

* fix spurious references to colorator in test

* Add new `Bridgetown::Inflector` based on `Dry::Inflector`

* Change classname based on inflection rules

* more inflection fixes

* Ensure tests pass with inflector equality check

* Add `foundation` helper so refinements are activated for any wrapped object

* Fix website lock

* Backtrack on odd symbol syntax

* Include `foundation` helper in more contexts

* Fix additional symbol syntax in backtracking

* Support multiple `foundation` arguments, use SimpleDelegator

* Backtrack from `foundation` helper, use base module system

* Set up packaging system using the Inclusive gem

* Remove `with` Symbol refinement, no use for it
  • Loading branch information
jaredcwhite authored Jul 14, 2024
1 parent c428f02 commit 98b6a7b
Show file tree
Hide file tree
Showing 92 changed files with 1,186 additions and 443 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ end
gem "bridgetown", path: "bridgetown"
gem "bridgetown-builder", path: "bridgetown-builder"
gem "bridgetown-core", path: "bridgetown-core"
gem "bridgetown-foundation", path: "bridgetown-foundation"
gem "bridgetown-paginate", path: "bridgetown-paginate"
28 changes: 17 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ PATH
remote: bridgetown-core
specs:
bridgetown-core (1.3.4)
activemodel (>= 6.0, < 8.0)
activesupport (>= 6.0, < 8.0)
addressable (~> 2.4)
amazing_print (~> 1.2)
colorator (~> 1.0)
bridgetown-foundation (= 1.3.4)
csv (~> 3.2)
dry-inflector (>= 1.0)
erubi (~> 1.9)
faraday (~> 2.0)
faraday-follow_redirects (~> 0.3)
hash_with_dot_access (~> 1.2)
i18n (~> 1.0)
kramdown (~> 2.1)
kramdown-parser-gfm (~> 1.0)
Expand All @@ -35,6 +34,14 @@ PATH
tilt (~> 2.0)
zeitwerk (~> 2.5)

PATH
remote: bridgetown-foundation
specs:
bridgetown-foundation (1.3.4)
hash_with_dot_access (~> 2.0)
inclusive (~> 1.0)
zeitwerk (~> 2.5)

PATH
remote: bridgetown-paginate
specs:
Expand All @@ -52,8 +59,6 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activemodel (7.1.3.2)
activesupport (= 7.1.3.2)
activesupport (7.1.3.2)
base64
bigdecimal
Expand All @@ -75,13 +80,13 @@ GEM
benchmark-ips (2.13.0)
bigdecimal (3.1.7)
builder (3.2.4)
colorator (1.1.0)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
csv (3.3.0)
diff-lcs (1.5.1)
docile (1.4.0)
drb (2.2.1)
dry-inflector (1.0.0)
e2mmap (0.1.0)
erubi (1.12.0)
faraday (2.9.0)
Expand All @@ -91,10 +96,10 @@ GEM
faraday-net_http (3.1.0)
net-http
ffi (1.16.3)
hash_with_dot_access (1.2.0)
activesupport (>= 5.0.0, < 8.0)
hash_with_dot_access (2.1.1)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
inclusive (1.0.0)
jaro_winkler (1.5.6)
json (2.7.2)
kramdown (2.4.0)
Expand All @@ -107,7 +112,7 @@ GEM
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
memory_profiler (1.0.1)
mini_portile2 (2.8.5)
mini_portile2 (2.8.6)
minitest (5.22.3)
minitest-profile (0.0.2)
minitest-reporters (1.6.1)
Expand Down Expand Up @@ -153,14 +158,14 @@ GEM
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.6)
roda (3.78.0)
roda (3.79.0)
rack
rouge (4.2.1)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.63.1)
rubocop (1.63.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
Expand Down Expand Up @@ -239,6 +244,7 @@ DEPENDENCIES
bridgetown!
bridgetown-builder!
bridgetown-core!
bridgetown-foundation!
bridgetown-paginate!
memory_profiler
minitest
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ task default: %w(test_all)
desc "Test all Bridgetown gems in monorepo"
task :test_all do
sh "cd bridgetown-core && script/cibuild"
sh "cd bridgetown-foundation && script/cibuild"
sh "cd bridgetown-builder && script/cibuild"
sh "cd bridgetown-paginate && script/cibuild"
sh "cd bridgetown-routes && script/cibuild"
end

task :release_all_unsafe do
sh "cd bridgetown-core && bundle exec rake release"
sh "cd bridgetown-foundation && bundle exec rake release"
sh "cd bridgetown-builder && bundle exec rake release"
sh "cd bridgetown-paginate && bundle exec rake release"
sh "cd bridgetown-routes && bundle exec rake release"
Expand Down
22 changes: 12 additions & 10 deletions bridgetown-builder/lib/bridgetown-builder/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@
module Bridgetown
# Superclass for a website's SiteBuilder abstract class
class Builder < Bridgetown::Builders::PluginBuilder
extend ActiveSupport::DescendantsTracker
include ActiveSupport::Callbacks

define_callbacks :build

class << self
def register
Bridgetown::Builders::PluginBuilder.plugin_registrations << self
end

def before_build(...)
set_callback(:build, :before, ...)
add_callback(:before, ...)
end

def after_build(...)
set_callback(:build, :after, ...)
add_callback(:after, ...)
end

def callbacks
@callbacks ||= {}
end

def around_build(...)
set_callback(:build, :around, ...)
def add_callback(name, method_name = nil, &block)
callbacks[name] ||= []
callbacks[name] << (block || proc { send(method_name) })
end
end

def build_with_callbacks
run_callbacks(:build) { build }
self.class.callbacks[:before]&.each { instance_exec(&_1) }
build
self.class.callbacks[:after]&.each { instance_exec(&_1) }
self
end

Expand Down
16 changes: 16 additions & 0 deletions bridgetown-builder/test/test_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,23 @@ class SiteBuilder < Builder
end

class SubclassOfSiteBuilder < SiteBuilder
class << self
attr_accessor :final_value
end

after_build :run_after

def build
site.config[:site_builder_subclass_loaded] = true
end

def run_after
self.class.final_value = [@initial_value, :goodbye]
end
end

SubclassOfSiteBuilder.before_build do
@initial_value = :hello
end

class TestHooks < BridgetownUnitTest
Expand Down Expand Up @@ -50,9 +64,11 @@ class TestHooks < BridgetownUnitTest
@site.reset
@site.loaders_manager.unload_loaders
@site.setup
SubclassOfSiteBuilder.final_value = nil
Bridgetown::Hooks.trigger :site, :pre_read, @site

assert @site.config[:site_builder_subclass_loaded]
assert_equal [:hello, :goodbye], SiteBuilder.subclasses.first.final_value
end
end
end
108 changes: 108 additions & 0 deletions bridgetown-core/benchmark/dot-access.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler"
Bundler.setup
require "benchmark/ips"
require "active_support/core_ext/hash/indifferent_access"
require "hash_with_dot_access"

class User < HashWithDotAccess::Hash
end

user = User.new({ address: { category: { desc: "Urban" } } })

using HashWithDotAccess::Refinements

# Enable and start GC before each job run. Disable GC afterwards.
#
# Inspired by https://www.omniref.com/ruby/2.2.1/symbols/Benchmark/bm?#annotation=4095926&line=182
class GCSuite
def warming(*)
run_gc
end

def running(*)
run_gc
end

def warmup_stats(*); end

def add_report(*); end

private

def run_gc
GC.enable
GC.start
GC.disable
end
end

suite = GCSuite.new

Benchmark.ips do |x|
x.config(suite:, time: 1, warmup: 1)

x.report("standard hash") do
h = { "foo" => "bar" }
h["foo"]
end
x.report("standard hash with fetch") do
h = { "foo" => "bar" }
h.fetch("foo", nil)
end
x.report("standard hash - symbol keys") do
h = { foo: "bar" }
h[:foo]
end
x.report("standard hash with fetch - symbol keys") do
h = { foo: "bar" }
h.fetch(:foo, nil)
end
x.report("hash with indifferent access string") do
h = { "foo" => "bar" }.with_indifferent_access
h["foo"]
end
x.report("hash with indifferent access symbol") do
h = { "foo" => "bar" }.with_indifferent_access
h[:foo]
end
x.report("hash with indifferent access via new method") do
h = ActiveSupport::HashWithIndifferentAccess.new({ "foo" => "bar" })
h[:foo]
end
# x.report("hash with indifferent access via []") do
# h = ActiveSupport::HashWithIndifferentAccess[{ "foo" => "bar" }]
# h[:foo]
# end
x.report("hash as_dots and symbol access") do
h = { foo: "bar" }.as_dots
h[:foo]
end
x.report("hash as_dots and method access") do
h = { foo: "bar" }.as_dots
h.foo
end
x.report("hash with dot access new method, string init, and symbol access") do
h = HashWithDotAccess::Hash.new({ "foo" => "bar" })
h[:foo]
end
x.report("hash with dot access new method, symbol init, and method access") do
h = HashWithDotAccess::Hash.new(foo: "bar")
h.foo
end
x.report("hash with dot access new method, string access") do
h = HashWithDotAccess::Hash.new({ "foo" => "bar" })
h["foo"]
end
user = { address: { category: { desc: "Urban" } } }
x.report("nested symbols") do
user[:address][:category][:desc]
end
userd = User.new({ address: { category: { desc: "Urban" } } })
x.report("nested dots") do
userd.address.category.desc
end
x.compare!
end
56 changes: 0 additions & 56 deletions bridgetown-core/benchmark/indifferent-access.rb

This file was deleted.

6 changes: 0 additions & 6 deletions bridgetown-core/bin/bridgetown
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ STDOUT.sync = true

$LOAD_PATH.unshift File.expand_path("../../bridgetown/lib", __dir__)

# Used by commands/automations
require "active_support"
require "active_support/core_ext/array/extract_options"
require "active_support/core_ext/string/strip"
require "active_support/core_ext/string/indent"

require "bridgetown"

# Support NO_COLOR: https://no-color.org
Expand Down
Loading

0 comments on commit 98b6a7b

Please sign in to comment.