fix specs by introducing a new liquid filter: open_json (needed for backward compatibility)
did
committed Sep 12, 2015
commit f9978c875983732932f93ed2f4631a3fa5ebc036
Showing 3
changed files with
39 additions
and 6 deletions
locomotive/steam/liquid/filters/json.rb b/lib/locomotive/steam/liquid/filters/json.rb
+12
-2
| @@ | @@ -10,14 +10,24 @@ module Locomotive |
| end | |
| if input.respond_to?(:each) | |
| - | input.map do |object| |
| + | '[' + input.map do |object| |
| fields.size == 1 ? object[fields.first].to_json : object_to_json(object, fields) | |
| - | end.join(',') |
| + | end.join(',') + ']' |
| else | |
| object_to_json(input, fields) | |
| end | |
| end | |
| + | # without the leading and trailing braces/brackets |
| + | # useful to add a prperty to an object or an element to an array |
| + | def open_json(input) |
| + | if input =~ /\A[\{\[](.*)[\}\]]\Z/m |
| + | $1 |
| + | else |
| + | input |
| + | end |
| + | end |
| + | |
| protected | |
| def object_to_json(input, fields) | |
spec/support/liquid.rb
+1
-1
| @@ | @@ -12,7 +12,7 @@ module Liquid |
| class TestDrop < Liquid::Drop | |
| def initialize(source) | |
| - | @_source = source |
| + | @_source = source.with_indifferent_access |
| end | |
| def before_method(meth) | |
spec/unit/liquid/filters/json_spec.rb
+26
-3
| @@ | @@ -19,7 +19,7 @@ describe Locomotive::Steam::Liquid::Filters::Json do |
| describe 'includes only the fields specified' do | |
| let(:input) { [Liquid::TestDrop.new(title: 'Acme', body: 'Lorem ipsum'), 'title'] } | |
| - | it { expect(subject).to eq %("title":"Acme") } |
| + | it { expect(subject).to eq %({"title":"Acme"}) } |
| end | |
| @@ | @@ -39,7 +39,7 @@ describe Locomotive::Steam::Liquid::Filters::Json do |
| let(:input) { | |
| [[Liquid::TestDrop.new(title: 'Acme', body: 'Lorem ipsum'), | |
| Liquid::TestDrop.new(title: 'Hello world', body: 'Lorem ipsum')], 'title'] } | |
| - | it { expect(subject).to eq %("Acme","Hello world") } |
| + | it { expect(subject).to eq %(["Acme","Hello world"]) } |
| end | |
| @@ | @@ -48,7 +48,30 @@ describe Locomotive::Steam::Liquid::Filters::Json do |
| let(:input) { | |
| [[Liquid::TestDrop.new(title: 'Acme', body: 'Lorem ipsum', date: '2013-12-13'), | |
| Liquid::TestDrop.new(title: 'Hello world', body: 'Lorem ipsum', date: '2013-12-12')], 'title, body'] } | |
| - | it { expect(subject).to eq %({"title":"Acme","body":"Lorem ipsum"},{"title":"Hello world","body":"Lorem ipsum"}) } |
| + | it { expect(subject).to eq %([{"title":"Acme","body":"Lorem ipsum"},{"title":"Hello world","body":"Lorem ipsum"}]) } |
| + | |
| + | end |
| + | |
| + | end |
| + | |
| + | describe '#open_json' do |
| + | |
| + | let(:input) { '' } |
| + | subject { open_json(input) } |
| + | |
| + | it { expect(subject).to eq '' } |
| + | |
| + | context 'without leading and trailing brackets' do |
| + | |
| + | let(:input) { %(["foo",[1,2],"bar"]) } |
| + | it { expect(subject).to eq %("foo",[1,2],"bar") } |
| + | |
| + | end |
| + | |
| + | context 'without leading and trailing braces' do |
| + | |
| + | let(:input) { %({"title":"Acme","body":"Lorem ipsum"}) } |
| + | it { expect(subject).to eq %("title":"Acme","body":"Lorem ipsum") } |
| end | |