class Rabl::Configuration

Rabl.host

Constants

CAMELIZED_KEYS_LIMIT

Names camelized in each style, by whether the first letter is upper case: a collection builds the same keys for each of its items, and camelizing one takes about 0.5 µs. ceiling: one entry per distinct key name, so a template that names keys at runtime (node(field.key)) keeps them for the life of the process until a new name arrives and the map is already full, when it is emptied; a name over CAMELIZED_KEY_LENGTH_LIMIT bytes isn’t kept at all; and a name keeps the acronyms it was first camelized with, so an inflection added later doesn’t reach a name that was already seen.

CAMELIZED_KEYS_LOCK

Files a new name in a style’s map: the maps are shared by every thread

CAMELIZED_KEY_LENGTH_LIMIT

A name longer than this, in bytes, is camelized each time instead of being kept, so a map holds at most CAMELIZED_KEYS_LIMIT names of up to this many bytes each

DEFAULT_XML_OPTIONS
OJ_APP_MODES

Oj modes an app can switch to after loading rabl that rabl’s output follows, as it did when rabl’s settings were global (e.g. :rails, which escapes “<”, “>” and “&”)

OJ_DEFAULTS_AT_LOAD

Oj.default_options once rabl was loaded, to tell which ones the app changed afterwards

OJ_OPTIONS

The Oj options rabl renders with, set globally at load as rabl always did (without :nan). :nan => :word keeps NaN/Infinity as words when :mode is passed per call (#744); a global :compat already writes them that way.

OJ_PER_CALL_OPTIONS

Oj before 3.17.7 hands the options given to Oj.dump on to to_json, which json 3.x rejects (ArgumentError: unknown keywords). So rabl leaves the app’s own Oj options alone and passes its own per call only from 3.17.7 (#744); with older Oj it behaves as it always did: it sets OJ_OPTIONS globally at load and calls Oj.dump(obj).

OJ_SHIPPED_OPTIONS

What Oj ships for the options rabl sets globally

Attributes

bson_check_keys[RW]
bson_engine[W]
bson_move_id[RW]
cache_all_output[RW]
cache_engine[RW]
cache_sources[RW]
camelize_keys[RW]
enable_json_callbacks[RW]
escape_all_output[RW]
exclude_empty_values_in_collections[RW]
exclude_nil_values[RW]
include_bson_root[RW]
include_child_root[RW]
include_json_root[RW]
include_msgpack_root[RW]
include_plist_root[RW]
include_xml_root[RW]
json_engine[W]
msgpack_engine[W]
perform_caching[RW]
plist_engine[W]
raise_on_missing_attribute[RW]
replace_empty_string_values_with_nil_values[RW]
replace_nil_values_with_empty_strings[RW]
use_read_multi[RW]
view_paths[RW]
xml_options[W]

Public Class Methods

new() click to toggle source
# File lib/rabl/configuration.rb, line 98
def initialize
  @include_json_root                            = true
  @include_child_root                           = true
  @include_msgpack_root                         = true
  @include_plist_root                           = true
  @include_xml_root                             = false
  @include_bson_root                            = true
  @enable_json_callbacks                        = false
  @bson_check_keys                              = false
  @bson_move_id                                 = false
  @json_engine                                  = nil
  @msgpack_engine                               = nil
  @bson_engine                                  = nil
  @plist_engine                                 = nil
  @xml_options                                  = {}
  @cache_sources                                = false
  @cache_all_output                             = false
  @escape_all_output                            = false
  @view_paths                                   = []
  @cache_engine                                 = Rabl::CacheEngine.new
  @perform_caching                              = false
  @use_read_multi                               = true
  @replace_nil_values_with_empty_strings        = false
  @replace_empty_string_values_with_nil_values  = false
  @exclude_nil_values                           = false
  @exclude_empty_values_in_collections          = false
  @camelize_keys                                = false
  @camelized_keys                               = { false => {}, true => {} }
end
oj_version_at_least?(version, minimum) click to toggle source

True when version (e.g. Oj::VERSION) is at least minimum, compared part by part as numbers: oj_version_at_least?(“3.10.0”, [3, 17, 7]) => false

# File lib/rabl/configuration.rb, line 63
def self.oj_version_at_least?(version, minimum)
  (version.to_s.split('.').map(&:to_i) <=> minimum) >= 0
end

Public Instance Methods

[](option) click to toggle source

Allows config options to be read like a hash

@param [Symbol] option Key for a given attribute

# File lib/rabl/configuration.rb, line 225
def [](option)
  __send__(option)
end
bson_engine() click to toggle source

@return the Bson encoder/engine to use.

# File lib/rabl/configuration.rb, line 212
def bson_engine
  @bson_engine || ::BSON
end
camelized_key(name, upper = false) click to toggle source

Returns name camelized in the given style, camelizing each kept name once

# File lib/rabl/configuration.rb, line 144
def camelized_key(name, upper = false)
  keys = @camelized_keys[upper]
  cached = keys[name]
  return cached if cached

  string = name.to_s
  style = upper ? :upper : :lower
  return string.camelize(style).to_sym if string.bytesize > CAMELIZED_KEY_LENGTH_LIMIT

  # a Hash freezes a plain String key and copies it, but keeps an unfrozen String subclass,
  # such as ActiveSupport::SafeBuffer, as it is: a caller that changes the name in place would
  # file another name under the camelized value, so the key is copied and frozen here
  # the map is shared by every thread, so a new name is filed under the lock, as in LOOKUP_IDS
  key = Rabl.frozen_copy(name)
  CAMELIZED_KEYS_LOCK.synchronize do
    keys.fetch(key) do
      keys.clear if keys.size >= camelized_keys_limit
      keys[key] = string.camelize(style).to_sym
    end
  end
end
camelized_keys_limit() click to toggle source

How many names a style’s map keeps before a new name empties it

# File lib/rabl/configuration.rb, line 167
def camelized_keys_limit
  CAMELIZED_KEYS_LIMIT
end
default_xml_options() click to toggle source

Returns merged default and inputted xml options, as they are now

# File lib/rabl/configuration.rb, line 230
def default_xml_options
  @xml_options.reverse_merge(DEFAULT_XML_OPTIONS)
end
json_engine() click to toggle source

@return The JSON engine used to encode Rabl templates into JSON

# File lib/rabl/configuration.rb, line 172
def json_engine
  @json_engine || (defined?(::Oj) ? ::Oj : ::JSON)
end
msgpack_engine() click to toggle source

@return the MessagePack encoder/engine to use.

# File lib/rabl/configuration.rb, line 206
def msgpack_engine
  @msgpack_engine || ::MessagePack
end
oj_dump_options() click to toggle source

The options rabl passes to Oj.dump (#744): OJ_OPTIONS, except where the app changed Oj.default_options after rabl was loaded. Those changes win, as they did when rabl’s settings were global: a new :time_format or :use_to_json, and a new mode if it is one of OJ_APP_MODES (then :nan follows that mode too). Another new mode (:strict, :null, :object, …) is ignored, since rabl’s output would raise on or drop plain objects. Options the app set before rabl was loaded are ignored too: rabl’s output stays as it was when rabl overwrote them.

Only options that differ from the current global ones are returned, so with rabl’s globals in place the hash is empty and Oj.dump gets no options, as before. :nan is only needed with an explicit :mode. With Oj before 3.17.7 it is always empty (see OJ_PER_CALL_OPTIONS). ceiling: Oj.default_options builds a new Hash on each render (a few microseconds); caching it would miss changes the app makes later.

# File lib/rabl/configuration.rb, line 190
def oj_dump_options
  return {} unless OJ_PER_CALL_OPTIONS

  current = ::Oj.default_options
  changed = lambda { |key| OJ_DEFAULTS_AT_LOAD && current[key] != OJ_DEFAULTS_AT_LOAD[key] }
  app_mode = changed.call(:mode) && OJ_APP_MODES.include?(current[:mode])

  options = OJ_OPTIONS.reject do |key, value|
    ([:mode, :nan].include?(key) ? app_mode : changed.call(key)) || current[key] == value
  end
  options.delete(:nan) unless options.key?(:mode)
  options
end
plist_engine() click to toggle source

@return the Plist encoder/engine to use.

# File lib/rabl/configuration.rb, line 218
def plist_engine
  @plist_engine || ::Plist::Emit
end