Add 'memoize'

This commit is contained in:
0x1eef 2023-10-07 17:44:58 -03:00
parent 599f37d2fe
commit b418b47685
4 changed files with 13 additions and 5 deletions

View file

@ -24,4 +24,5 @@ gem "standard", "~> 1.24"
gem "paint", "~> 2.3"
gem "dotenv", "~> 2.8"
gem "listen", "~> 3.0"
gem "memoize", "~> 1.3"
gem "rake"

View file

@ -65,6 +65,7 @@ GEM
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
memo_wise (1.7.0)
memoize (1.3.1)
nanoc (4.12.15)
addressable (~> 2.5)
colored (~> 1.2)
@ -164,6 +165,7 @@ PLATFORMS
DEPENDENCIES
dotenv (~> 2.8)
listen (~> 3.0)
memoize (~> 1.3)
nanoc (~> 4.12)
nanoc-gzip.rb!
nanoc-webpack.rb!

View file

@ -1,6 +1,9 @@
# frozen_string_literal: true
module Mixin
require "memoize"
extend Memoize
require_relative "mixin/t"
require_relative "mixin/inline"
require_relative "mixin/erb"
@ -10,6 +13,7 @@ module Mixin
nanoc = Ryo.from YAML.load_file(File.join(Dir.getwd, "nanoc.yaml"))
nanoc.output_dir
end
memoize :build_dir
include T
include Inline

View file

@ -1,14 +1,15 @@
module Mixin::T
extend Memoize
def t(locale, key, local_assigns = {})
str = [locale, *key.split(".")].inject(i18n) { _1[_2] }
format(str, local_assigns)
end
def i18n
@i18n ||= Ryo.from(
JSON.parse(
File.read(File.join(Dir.getwd, "src", "i18n.json"))
)
)
path = File.join(Dir.getwd, "src", "i18n.json")
json = File.binread(path)
Ryo.from JSON.parse(json)
end
memoize :i18n
end