Class: Roar::JSON::JSONAPI::MemberName

Inherits:
Object
  • Object
show all
Defined in:
lib/roar/json/json_api/member_name.rb

Overview

Member Name formatting according to the JSON API specification.

Constant Summary collapse

LENIENT_FILTER_REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

/([^[:alnum:][-_ ]]+)/
STRICT_FILTER_REGEXP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

/([^[0-9a-z][-_]]+)/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(name, options = {}) ⇒ Object

See Also:

Since:

  • 0.1.0



17
18
19
# File 'lib/roar/json/json_api/member_name.rb', line 17

def self.call(name, options = {})
  new.(name, options)
end

Instance Method Details

#call(name, options = {}) ⇒ String

Format a member name

Parameters:

  • name (String, Symbol)

    member name.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :strict (Boolean)

    whether strict mode is enabled.

    Strict mode applies additional JSON Specification RECOMMENDATIONS, permitting only non-reserved, URL safe characters specified in RFC 3986. The member name will be lower-cased and underscores will be transformed to hyphens.

    Non-strict mode permits:

    • non-ASCII alphanumeric Unicode characters.
    • spaces, underscores and hyphens, except as the first or last character.

Returns:

  • (String)

    formatted member name.

Since:

  • 0.1.0



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/roar/json/json_api/member_name.rb', line 40

def call(name, options = {})
  name    = name.to_s
  strict  = options.fetch(:strict, true)
  if strict
    name.downcase!
    name.gsub!(STRICT_FILTER_REGEXP, ''.freeze)
  else
    name.gsub!(LENIENT_FILTER_REGEXP, ''.freeze)
  end
  name.gsub!(/\A([-_ ])/, '')
  name.gsub!(/([-_ ])\z/, '')
  name.tr!('_', '-') if strict
  name
end