Sha256: 71c76038b2b8191f3d0a2aa550714ee0ac22eb6cf91e3d4136b2dacd3e457f6b

Contents?: true

Size: 1.56 KB

Versions: 8

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module Arstotzka
  # @api private
  #
  # Class responsible for changing a key
  class KeyChanger
    include Base

    # @param [String] base_key The key to be checked
    #   (before case change)
    #
    # @overload initialize(base_key, options_hash)
    #   @param options_hash [Hash] options passed on expose
    #   @option options_hash case [Symbol] case type for key
    #     transformation
    #     - +:snake+ : snake_cased keys
    #     - +:lower_camel+ : lowerCamelCased keys
    #     - +:upper_camel+ : UperCamelCased keys
    # @overload initialize(base_key, options)
    #   @param options [Option] options passed on expose
    #
    # @example
    #   changer = Arstotzka::KeyChanger.new('the_key', case: :upper_camel)
    #   changer.key # returns 'TheKey'
    def initialize(base_key, options_hash = {})
      self.options = options_hash
      @base_key = base_key
    end

    # Transforms the key to have the correct case
    #
    # the possible cases (instance attribute) are
    # - lower_camel: for cammel case with first letter lowercase
    # - upper_camel: for cammel case with first letter uppercase
    # - snake: for snake case
    #
    # @return [String] the string transformed
    def key
      @key ||= case options.case
               when :lower_camel
                 base_key.camelize(:lower)
               when :upper_camel
                 base_key.camelize(:upper)
               when :snake
                 base_key.underscore
               end
    end

    private

    attr_reader :base_key, :options
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
arstotzka-1.6.2 lib/arstotzka/key_changer.rb
arstotzka-1.6.1 lib/arstotzka/key_changer.rb
arstotzka-1.6.0 lib/arstotzka/key_changer.rb
arstotzka-1.5.0 lib/arstotzka/key_changer.rb
arstotzka-1.4.4 lib/arstotzka/key_changer.rb
arstotzka-1.4.3 lib/arstotzka/key_changer.rb
arstotzka-1.4.2 lib/arstotzka/key_changer.rb
arstotzka-1.4.1 lib/arstotzka/key_changer.rb