Sha256: 72cdd2b5df2bb4b722c7c56a4fd49eb498d252a16ab9babbae73582604e19f23

Contents?: true

Size: 1012 Bytes

Versions: 1

Compression:

Stored size: 1012 Bytes

Contents

require 'active_support/all'

class FalseClass
  def to_builder
    'false'
  end
end

class TrueClass
  def to_builder
    'true'
  end
end

class String
  JS_ESCAPE_MAP = {
    '\\'    => '\\\\',
    '</'    => '<\/',
    "\r\n"  => '\n',
    "\n"    => '\n',
    "\r"    => '\n',
    '"'     => '\\"',
    "'"     => "\'"
  }

  def to_builder
    %("#{json_escape}")
  end

  private

  def json_escape
    gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) { |match|
      JS_ESCAPE_MAP[match]
    }
  end
end

class Hash
  def to_builder
    to_json
  end
end

class NilClass
  def to_builder
    'null'
  end
end

module ActiveSupport
  class TimeWithZone
    def to_builder
      %("#{iso8601}")
    end
  end
end

class Time
  def to_builder
    %("#{iso8601}")
  end
end

class Date
  def to_builder
    %("#{to_time.iso8601}")
  end
end

class DateTime
  def to_builder
    %("#{to_time.iso8601}")
  end
end

module BSON
  class ObjectId
    def to_builder
      %("#{self}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_builder-3.1.5 lib/json_builder/extensions.rb