Sha256: 840657bec86374c2bce5a7c0d2ec6585416829dc14beff0fd163bc9a386b0f0b

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module Locomotive
  module Steam
    module Liquid
      module Filters
        module Json

          def json(input, fields = nil)
            if fields && fields.is_a?(String)
              fields = fields.split(',').map(&:strip)
            end

            if fields.blank?
              input.to_json
            elsif input.respond_to?(:each)
              if fields.size == 1
                input.map { |object| object[fields.first].to_json }.join(',')
              else
                input.map { |object| "{" + object_to_json(object, fields) + "}" }.join(',')
              end
            else
              object_to_json(input, fields)
            end
          end

          protected

          def object_to_json(input, fields)
            [].tap do |output|
              fields.each do |field|
                output << %("#{field}":#{input[field].to_json})
              end
            end.join(',')
          end

        end

        ::Liquid::Template.register_filter(Json)

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
locomotivecms_steam-1.0.0.pre.beta.3 lib/locomotive/steam/liquid/filters/json.rb