Sha256: b883a4b3b1cddfca42397556cc98cc146c3687c40f101659a1738d3e2ce53cf6

Contents?: true

Size: 980 Bytes

Versions: 2

Compression:

Stored size: 980 Bytes

Contents

module Alba
  # Transform keys using `ActiveSupport::Inflector`
  module KeyTransformer
    begin
      require 'active_support/inflector'
    rescue LoadError
      raise ::Alba::Error, 'To use transform_keys, please install `ActiveSupport` gem.'
    end

    module_function

    # Transform key as given transform_type
    #
    # @params key [String] key to be transformed
    # @params transform_type [Symbol] transform type
    # @return [String] transformed key
    # @raise [Alba::Error] when transform_type is not supported
    def transform(key, transform_type)
      case transform_type
      when :camel
        ActiveSupport::Inflector.camelize(key)
      when :lower_camel
        ActiveSupport::Inflector.camelize(key, false)
      when :dash
        ActiveSupport::Inflector.dasherize(key)
      else
        raise ::Alba::Error, "Unknown transform_type: #{transform_type}. Supported transform_type are :camel, :lower_camel and :dash."
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alba-1.0.0 lib/alba/key_transformer.rb
alba-0.13.1 lib/alba/key_transformer.rb