Class: NOSJ::RailsEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/nosj/rails.rb,
sig/nosj.rbs

Overview

Defined by require "nosj/rails", which also installs it as ActiveSupport::JSON::Encoding.json_encoder.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ RailsEncoder

Returns a new instance of RailsEncoder.

Parameters:

  • options (Hash[Symbol, untyped], nil) (defaults to: nil)


51
52
53
# File 'lib/nosj/rails.rb', line 51

def initialize(options = nil)
  @options = options || {}
end

Instance Attribute Details

#optionsHash[Symbol, untyped] (readonly)

Returns the value of attribute options.

Returns:

  • (Hash[Symbol, untyped])


49
50
51
# File 'lib/nosj/rails.rb', line 49

def options
  @options
end

Instance Method Details

#encode(value) ⇒ String

Parameters:

  • value (Object)

Returns:

  • (String)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nosj/rails.rb', line 55

def encode(value)
  value = value.as_json(@options.dup) unless @options.empty?
  if @options.fetch(:escape, true)
    escape_html = @options.fetch(:escape_html_entities) do
      ActiveSupport::JSON::Encoding.escape_html_entities_in_json
    end
    NOSJ.generate_rails_native(value, !!escape_html, escape_js_separators?)
  else
    NOSJ.generate_rails_native(value, false, false)
  end
end