Class: Arstotzka::KeyChanger Private

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/arstotzka/key_changer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class responsible for changing a key

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#options=

Constructor Details

#initialize(base_key, options_hash) ⇒ KeyChanger #initialize(base_key, options) ⇒ KeyChanger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of KeyChanger.

Examples:

changer = Arstotzka::KeyChanger.new('the_key', case: :upper_camel)
changer.key # returns 'TheKey'

Overloads:

  • #initialize(base_key, options_hash) ⇒ KeyChanger

    Parameters:

    • options_hash (Hash)

      options passed on expose

    Options Hash (options_hash):

    • case (Symbol)

      case type for key transformation

      • :snake : snake_cased keys

      • :lower_camel : lowerCamelCased keys

      • :upper_camel : UperCamelCased keys

  • #initialize(base_key, options) ⇒ KeyChanger

    Parameters:

    • options (Option)

      options passed on expose

Parameters:

  • base_key (String)

    The key to be checked (before case change)



26
27
28
29
# File 'lib/arstotzka/key_changer.rb', line 26

def initialize(base_key, options_hash = {})
  self.options = options_hash
  @base_key = base_key
end

Instance Attribute Details

#base_keyObject (readonly, private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
# File 'lib/arstotzka/key_changer.rb', line 52

def base_key
  @base_key
end

#optionsObject (readonly, private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



52
53
54
# File 'lib/arstotzka/key_changer.rb', line 52

def options
  @options
end

Instance Method Details

#keyString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (String)

    the string transformed



39
40
41
42
43
44
45
46
47
48
# File 'lib/arstotzka/key_changer.rb', line 39

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