class Lurch::Inflector

Public Class Methods

classify(str) click to toggle source
# File lib/lurch/inflector.rb, line 8
def self.classify(str)
  Inflecto.classify(str)
end
decode_key(key) click to toggle source
# File lib/lurch/inflector.rb, line 12
def self.decode_key(key)
  Inflecto.underscore(key.to_s).to_sym
end
decode_type(type) click to toggle source
# File lib/lurch/inflector.rb, line 16
def self.decode_type(type)
  Inflecto.pluralize(decode_key(type)).to_sym
end
new(inflection_mode, types_mode) click to toggle source
# File lib/lurch/inflector.rb, line 3
def initialize(inflection_mode, types_mode)
  define_encode_key(inflection_mode)
  define_encode_type(types_mode)
end

Public Instance Methods

define_encode_key(inflection_mode) click to toggle source
# File lib/lurch/inflector.rb, line 20
def define_encode_key(inflection_mode)
  case inflection_mode
  when :dasherize
    define_singleton_method(:encode_key) { |key| Inflecto.dasherize(key.to_s) }
  when :underscore
    define_singleton_method(:encode_key) { |key| Inflecto.underscore(key.to_s) }
  else
    raise ArgumentError, "Invalid inflection mode: #{inflection_mode}"
  end
end
define_encode_type(types_mode) click to toggle source
# File lib/lurch/inflector.rb, line 31
def define_encode_type(types_mode)
  case types_mode
  when :pluralize
    define_singleton_method(:encode_type) do |type|
      key = encode_key(type)
      Inflecto.pluralize(key)
    end
  when :singularize
    define_singleton_method(:encode_type) do |type|
      key = encode_key(type)
      Inflecto.singularize(key)
    end
  else
    raise ArgumentError, "Invalid types mode: #{types_mode}"
  end
end
encode_keys(hash) { |value| ... } click to toggle source
# File lib/lurch/inflector.rb, line 48
def encode_keys(hash)
  hash.each_with_object({}) do |(key, value), acc|
    acc[encode_key(key)] = block_given? ? yield(value) : value
  end
end
encode_types(hash) { |value| ... } click to toggle source
# File lib/lurch/inflector.rb, line 54
def encode_types(hash)
  hash.each_with_object({}) do |(key, value), acc|
    acc[encode_type(key)] = block_given? ? yield(value) : value
  end
end