Skip to content

Commit

Permalink
Load in HashWithDotAccess 2.0, remove AS dependency of indifferent ac…
Browse files Browse the repository at this point in the history
…cess
  • Loading branch information
jaredcwhite committed Apr 20, 2024
1 parent 5163929 commit 486a456
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 126 deletions.
5 changes: 2 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PATH
erubi (~> 1.9)
faraday (~> 2.0)
faraday-follow_redirects (~> 0.3)
hash_with_dot_access (~> 1.2)
hash_with_dot_access (~> 2.0)
i18n (~> 1.0)
kramdown (~> 2.1)
kramdown-parser-gfm (~> 1.0)
Expand Down Expand Up @@ -97,8 +97,7 @@ 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.0.0)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.6)
Expand Down
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.

2 changes: 1 addition & 1 deletion bridgetown-core/bridgetown-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("erubi", "~> 1.9")
s.add_runtime_dependency("faraday", "~> 2.0")
s.add_runtime_dependency("faraday-follow_redirects", "~> 0.3")
s.add_runtime_dependency("hash_with_dot_access", "~> 1.2")
s.add_runtime_dependency("hash_with_dot_access", "~> 2.0")
s.add_runtime_dependency("i18n", "~> 1.0")
s.add_runtime_dependency("kramdown", "~> 2.1")
s.add_runtime_dependency("kramdown-parser-gfm", "~> 1.0")
Expand Down
3 changes: 2 additions & 1 deletion bridgetown-core/lib/bridgetown-core/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Bridgetown
class Collection
using Bridgetown::Refinements
using HashWithDotAccess::Refinements
include Enumerable

# @return [Bridgetown::Site]
Expand Down Expand Up @@ -250,7 +251,7 @@ def merge_data_resources # rubocop:todo Metrics/AbcSize, Metrics/CyclomaticCompl
end
end

merge_environment_specific_metadata(data_contents).with_dot_access
merge_environment_specific_metadata(data_contents).as_dots
end

def merge_environment_specific_metadata(data_contents)
Expand Down
7 changes: 4 additions & 3 deletions bridgetown-core/lib/bridgetown-core/concerns/site/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class Bridgetown::Site
# Content is king!
module Content
using HashWithDotAccess::Refinements
def resources_grouped_by_taxonomy(taxonomy)
data.site_taxonomies_hash ||= {}
data.site_taxonomies_hash[taxonomy.label] ||= taxonomy.terms.transform_values do |terms|
Expand Down Expand Up @@ -45,10 +46,10 @@ def site_payload
#
# If `config["collections"]` is not specified, a blank hash is returned.
#
# @return [Hash{String, Symbol => Collection}] A Hash
# @return [HashWithDotAccess::Hash{String, Symbol => Collection}] A Hash
# containing a collection name-to-instance pairs.
#
# @return [Hash] Returns a blank hash if no items found
# @return [HashWithDotAccess::Hash] Returns a blank hash if no items found
def collections
@collections ||= collection_names.each_with_object(
HashWithDotAccess::Hash.new
Expand Down Expand Up @@ -78,7 +79,7 @@ def taxonomy_types
[label, Bridgetown::Resource::TaxonomyType.new(
site: self, label:, key:, metadata: tax_metadata
),]
end.with_dot_access
end.as_dots
end

# Get all loaded resources.
Expand Down
3 changes: 2 additions & 1 deletion bridgetown-core/lib/bridgetown-core/layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Bridgetown
class Layout
using HashWithDotAccess::Refinements
include FrontMatter::Importer
include LiquidRenderable

Expand Down Expand Up @@ -67,7 +68,7 @@ def initialize(site, base, name, from_plugin: false)
@relative_path = @path.sub(@base_dir, "")
@ext = File.extname(name)

@data = read_front_matter(@path)&.with_dot_access
@data = read_front_matter(@path)&.as_dots
rescue SyntaxError => e
Bridgetown.logger.error "Error:",
"Ruby Exception in #{e.message}"
Expand Down
6 changes: 2 additions & 4 deletions bridgetown-core/lib/bridgetown-core/resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Bridgetown
module Resource
class Base # rubocop:todo Metrics/ClassLength
using HashWithDotAccess::Refinements
include Comparable
include Bridgetown::Publishable
include Bridgetown::LayoutPlaceable
Expand Down Expand Up @@ -93,10 +94,7 @@ def relations
#
# @return [HashWithDotAccess::Hash]
def front_matter_defaults
site.frontmatter_defaults.all(
relative_path.to_s,
collection.label.to_sym
).with_dot_access
site.frontmatter_defaults.all(relative_path.to_s, collection.label.to_sym).as_dots
end

# @return [HashWithDotAccess::Hash]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Bridgetown
module Resource
class TaxonomyType
using HashWithDotAccess::Refinements

# @return [Bridgetown::Site]
attr_reader :site

Expand All @@ -28,7 +30,7 @@ def initialize(site:, label:, key:, metadata:)
def terms
site.resources.map do |resource|
resource.taxonomies[label].terms
end.flatten.group_by(&:label).with_dot_access
end.flatten.group_by(&:label).as_dots
end

def inspect
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def initialize(config, label: :main, loaders_manager: nil)
@reader = Reader.new(self)
@liquid_renderer = LiquidRenderer.new(self)

Bridgetown::Cache.base_cache["site_tmp"] = {}.with_dot_access
Bridgetown::Cache.base_cache["site_tmp"] = HashWithDotAccess::Hash.new
ensure_not_in_dest

Bridgetown::Current.sites[@label] = self
Expand Down
3 changes: 2 additions & 1 deletion bridgetown-core/lib/bridgetown-core/static_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Bridgetown
class StaticFile
using HashWithDotAccess::Refinements
extend Forwardable

attr_reader :relative_path, :extname, :name, :data, :site, :collection
Expand Down Expand Up @@ -34,7 +35,7 @@ def initialize(site, base, dir, name, collection = nil) # rubocop:disable Metric
@collection = collection
@relative_path = File.join(*[@dir, @name].compact)
@extname = File.extname(@name)
@data = @site.frontmatter_defaults.all(relative_path, type).with_dot_access
@data = @site.frontmatter_defaults.all(relative_path, type).as_dots
data.permalink ||= if collection && !collection.builtin?
"#{collection.default_permalink.chomp("/").chomp(".*")}.*"
else
Expand Down
Loading

0 comments on commit 486a456

Please sign in to comment.