Class: StringFormatter

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

Constant Summary collapse

PUNCTUATION_FORMATS =

# Constants #

#
{
  'ampersand'     => '&',
  'at'            => '@',
  'backslash'     => '\\',
  'backtick'      => '`',
  'bang'          => '!',
  'caret'         => '^',
  'cash'          => '$',
  'colon'         => ':',
  'comma'         => ',',
  'dash'          => '-',
  'double_quote'  => '"',
  'eight'         => '8',
  'equals'        => '=',
  'five'          => '5',
  'four'          => '4',
  'hash'          => '#',
  'left_brace'    => '{',
  'left_bracket'  => '[',
  'left_chevron'  => '<',
  'left_paren'    => '(',
  'left_shovel'   => '<<',
  'left_stache'   => '{{',
  'nine'          => '9',
  'one'           => '1',
  # Defining this method is not recommended:
  'percent'       => '%',
  'period'        => '.',
  'pipe'          => '|',
  'plus'          => '+',
  'quote'         => "'",
  'right_brace'   => '}',
  'right_bracket' => ']',
  'right_chevron' => '>',
  'right_paren'   => ')',
  'right_shovel'  => '>>',
  'right_stache'  => '}}',
  'semicolon'     => ';',
  'seven'         => '7',
  'six'           => '6',
  'slash'         => '/',
  'splat'         => '*',
  'three'         => '3',
  'tilde'         => '~',
  'two'           => '2',
  'underscore'    => '_',
  'what'          => '?',
  'zero'          => '0'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.method_missing(escape_sequence, *args, &behavior) ⇒ Object

# Class Methods #

#


69
70
71
72
73
74
75
76
77
78
# File 'lib/string_formatter.rb', line 69

def self.method_missing(escape_sequence, *args, &behavior)
  super unless behavior

  if escaping_as_punctuation? && valid_punctuation_sequence?(escape_sequence)
    escape_sequence = characters_for_punctuation_sequence(escape_sequence)
  end

  set_escape(escape_sequence, behavior)
  set_escape_options(escape_sequence, args.first) if args.length > 0
end

.punctuationObject



80
81
82
# File 'lib/string_formatter.rb', line 80

def self.punctuation
  @parsing_punctuation = true
end

Instance Method Details

#escape(object, format_string) ⇒ Object

# Instance Methods #

#


122
123
124
125
# File 'lib/string_formatter.rb', line 122

def escape(object, format_string)
  escape_method = self.class.escapes[format_string]
  escape_method ? escape_method[object] : format_string
end

#format(object, format_string) ⇒ Object



127
128
129
130
131
# File 'lib/string_formatter.rb', line 127

def format(object, format_string)
  parsed_string = parser.parse(format_string)

  evaluator.evaluate(object, parsed_string)
end