Class: Reynard::Inflector

Inherits:
Object
  • Object
show all
Defined in:
lib/reynard/inflector.rb

Overview

Transforms property names so they are value Ruby identifiers or more readable to users.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInflector

Returns a new instance of Inflector.



6
7
8
# File 'lib/reynard/inflector.rb', line 6

def initialize
  @snake_case = {}
end

Class Method Details

.snake_case(property) ⇒ Object

Returns the string in snake-case.



22
23
24
25
26
27
28
# File 'lib/reynard/inflector.rb', line 22

def self.snake_case(property)
  property
    .to_s
    .gsub(/([A-Z])(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) { (Regexp.last_match(1) || Regexp.last_match(2)) << '_' }
    .tr("'\"-", '___')
    .downcase
end

Instance Method Details

#snake_case(property) ⇒ Object

Returns the string in snake-case, taking previously registered exceptions into account.



17
18
19
# File 'lib/reynard/inflector.rb', line 17

def snake_case(property)
  @snake_case[property] || self.class.snake_case(property)
end

#snake_cases(exceptions) ⇒ Object

Registers additional exceptions to the regular snake-case algorithm. Registering is additive so you can call this multiple times without losing previously registered exceptions.



12
13
14
# File 'lib/reynard/inflector.rb', line 12

def snake_cases(exceptions)
  @snake_case.merge!(exceptions)
end