From 66507af3ecf498dd21c916943b5eb82c76d6d928 Mon Sep 17 00:00:00 2001 From: Jan De Poorter Date: Mon, 10 Feb 2014 15:20:00 +0100 Subject: [PATCH 1/3] Add a way to cache certain parts of a document --- lib/hypertemplate/builder.rb | 1 + lib/hypertemplate/builder/base.rb | 15 ++++++++- lib/hypertemplate/builder/cache.rb | 49 ++++++++++++++++++++++++++++++ lib/hypertemplate/hook/rails.rb | 6 ++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 lib/hypertemplate/builder/cache.rb diff --git a/lib/hypertemplate/builder.rb b/lib/hypertemplate/builder.rb index 46957f9..d9eb3bb 100644 --- a/lib/hypertemplate/builder.rb +++ b/lib/hypertemplate/builder.rb @@ -2,6 +2,7 @@ module Hypertemplate module Builder require "hypertemplate/builder/base" require "hypertemplate/builder/values" + require "hypertemplate/builder/cache" require "hypertemplate/builder/json" require "hypertemplate/builder/xml" diff --git a/lib/hypertemplate/builder/base.rb b/lib/hypertemplate/builder/base.rb index 8d27636..453e95b 100644 --- a/lib/hypertemplate/builder/base.rb +++ b/lib/hypertemplate/builder/base.rb @@ -6,7 +6,14 @@ class Base undef_method :id if respond_to?(:id) class << self - + def cache + @cache + end + + def cache=(cache) + @cache = cache + end + def media_types @media_types end @@ -98,6 +105,12 @@ def ns(*args, &block) end end + def cache(key) + Cache.new(key, self) do |cache| + yield cache + end + end + # writes a key and value pair to the representation # example: # diff --git a/lib/hypertemplate/builder/cache.rb b/lib/hypertemplate/builder/cache.rb new file mode 100644 index 0000000..1ac6a3d --- /dev/null +++ b/lib/hypertemplate/builder/cache.rb @@ -0,0 +1,49 @@ +module Hypertemplate + module Builder + class Cache #< BasicObject + attr_accessor :builder + + def initialize(key, builder, &block) + @key = key + @builder = builder + + if cached? + raw = cache.read(cache_key) + + if builder.class == Xml + data = Nokogiri::XML(raw) + else + data = MultiJson.decode(raw) + end + else + _builder = builder.class.new({}) + yield _builder + data = _builder.raw + + cache.write(cache_key, _builder.representation) + end + + if current = builder.instance_variable_get('@current') # JSON + current.merge!(data) + else # XML + parent = builder.instance_variable_get('@parent') + data.root.children.each do |element| + element.parent = parent + end + end + end + + def cached? + cache.exist?(cache_key) + end + + def cache + Hypertemplate::Builder::Base.cache + end + + def cache_key + @_generated_key ||= "#{@key}_#{builder.class.to_s.split('::').last}" + end + end + end +end \ No newline at end of file diff --git a/lib/hypertemplate/hook/rails.rb b/lib/hypertemplate/hook/rails.rb index 93a6616..52a7f77 100644 --- a/lib/hypertemplate/hook/rails.rb +++ b/lib/hypertemplate/hook/rails.rb @@ -1,6 +1,12 @@ require 'hypertemplate' unless defined? ::Hypertemplate module Hypertemplate + class Railtie < Rails::Railtie + initializer "hypertemplate.setup_cache" do + Hypertemplate::Builder::Base.cache = Rails.cache + end + end + module RegistryContainer def hypertemplate_registry From 6ae177fab41ae48fdd6045ab673657fbd70168b2 Mon Sep 17 00:00:00 2001 From: Tom Maeckelberghe Date: Mon, 20 Apr 2015 14:02:33 +0200 Subject: [PATCH 2/3] Add support for emoji charaters --- Gemfile | 1 + lib/hypertemplate/builder/json.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 1b2ca7e..a8367f1 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ source :rubygems # Main dependencies gem "json_pure", :require => "json/pure" gem "nokogiri" +gem 'yajl-ruby', require: 'yajl' group :development, :test do gem "ruby-debug" , :platforms => [:mri_18] diff --git a/lib/hypertemplate/builder/json.rb b/lib/hypertemplate/builder/json.rb index a5e952b..d8ae93d 100644 --- a/lib/hypertemplate/builder/json.rb +++ b/lib/hypertemplate/builder/json.rb @@ -75,7 +75,7 @@ def insert_value(name, prefix, *args, &block) end def representation - @raw.to_json + Yajl::Encoder.encode(@raw) end private From 0c8ce4b17d9ffd1b7a1f587249056df5dd681672 Mon Sep 17 00:00:00 2001 From: Jan De Poorter Date: Mon, 19 Jun 2023 16:18:13 +0200 Subject: [PATCH 3/3] Don't prefer one specific JSON solution --- Gemfile | 1 - lib/hypertemplate/builder/json.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index a8367f1..1b2ca7e 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,6 @@ source :rubygems # Main dependencies gem "json_pure", :require => "json/pure" gem "nokogiri" -gem 'yajl-ruby', require: 'yajl' group :development, :test do gem "ruby-debug" , :platforms => [:mri_18] diff --git a/lib/hypertemplate/builder/json.rb b/lib/hypertemplate/builder/json.rb index d8ae93d..9e532ea 100644 --- a/lib/hypertemplate/builder/json.rb +++ b/lib/hypertemplate/builder/json.rb @@ -75,7 +75,7 @@ def insert_value(name, prefix, *args, &block) end def representation - Yajl::Encoder.encode(@raw) + MultiJson.encode(@raw) end private