From 0fd1bdcdfc42266a079f227841fe1deb958ef55d Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Fri, 7 Apr 2017 16:51:37 -0400 Subject: [PATCH] ensure to_h doesnt cause things to explode --- lib/jekyll-seo-tag/drop.rb | 10 +++++++--- spec/jekyll_seo_tag/drop_spec.rb | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/jekyll-seo-tag/drop.rb b/lib/jekyll-seo-tag/drop.rb index 082a745..3910407 100644 --- a/lib/jekyll-seo-tag/drop.rb +++ b/lib/jekyll-seo-tag/drop.rb @@ -2,8 +2,12 @@ module Jekyll class SeoTag class Drop < Jekyll::Drops::Drop TITLE_SEPARATOR = " | ".freeze - include Jekyll::Filters - include Liquid::StandardFilters + INCLUDES = [Jekyll::Filters, Liquid::StandardFilters].freeze + + INCLUDES.each do |klass| + include klass + klass.instance_methods.each { |method| private method } + end def initialize(text, context) @obj = {} @@ -190,7 +194,7 @@ module Jekyll def format_string(string) methods = %i[markdownify strip_html normalize_whitespace escape_once] methods.each do |method| - string = public_send(method, string) + string = send(method, string) end string unless string.empty? diff --git a/spec/jekyll_seo_tag/drop_spec.rb b/spec/jekyll_seo_tag/drop_spec.rb index d56926e..34ef79c 100644 --- a/spec/jekyll_seo_tag/drop_spec.rb +++ b/spec/jekyll_seo_tag/drop_spec.rb @@ -7,6 +7,14 @@ RSpec.describe Jekyll::SeoTag::Drop do let(:text) { "" } subject { described_class.new(text, context) } + # Drop includes liquid filters which expect arguments + # By default, in drops, `to_h` will call each public method with no arugments + # Here, that would cause the filters to explode. This test ensures that all + # public methods don't explode when called without arguments. Don't explode. + it "doesn't blow up on to_h" do + expect { subject.to_h }.to_not raise_error + end + it "returns the version" do expect(subject.version).to eql(Jekyll::SeoTag::VERSION) end