Class: Arstotzka::Config

Inherits:
Sinclair::Config
  • Object
show all
Defined in:
lib/arstotzka/config.rb

Overview

Arstotzka configuration

Configuration of Arstotzka is done through Arstotzka.configure which configures using Config by Sinclar::Configurable

Examples:

Redefining json method and case type

class Office
  include Arstotzka

  expose :employes, full_path: 'employes.first_name',
                    compact: true

  def initialize(hash)
    @hash = hash
  end
end

hash = {
  employes: [{
    first_name: 'Rob'
  }, {
    first_name: 'Klara'
  }]
}

office = Office.new(hash)

office.employes # raises NoMethodError as json is not a method

Arstotzka.configure { json :@hash }

office.employes # returns []

Arstotzka.configure { |c| c.case :snake }

office.employes # returns %w[Rob Klara]

Generating options

Arstotzka.configure do |config|
  config.case :snake
end

config = Arstotzka.config
options = config.options(klass: Person)

options.to_h
# returns
# {
#   after:      false,
#   after_each: nil,
#   cached:     false,
#   case:       :snake,
#   compact:    false,
#   default:    nil,
#   flatten:    false,
#   json:       :json,
#   klass:      Person,
#   type:       :none,
#   full_path:  nil,
#   key:        nil,
#   instance:   nil,
#   befor:      nil
# }

See Also:

Constant Summary collapse

DEFAULT_CONFIGS =

Default values for Arstotzka::ClassMethods#expose

{
  after:      false,
  after_each: nil,
  cached:     false,
  case:       :lower_camel,
  compact:    false,
  default:    nil,
  flatten:    false,
  json:       :json,
  klass:      nil,
  type:       :none,
  before:     nil
}.freeze

Instance Method Summary collapse

Instance Method Details

#options(options_hash = {}) ⇒ Options

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 Options

the new instance will have it’s values as a merge from configuration and given options_hash

Examples:

Generating options

Arstotzka.configure do |config|
  config.case :snake
end

config = Arstotzka.config
options = config.options(klass: Person)

options.to_h
# returns
# {
#   after:      false,
#   after_each: nil,
#   cached:     false,
#   case:       :snake,
#   compact:    false,
#   default:    nil,
#   flatten:    false,
#   json:       :json,
#   klass:      Person,
#   type:       :none,
#   full_path:  nil,
#   key:        nil,
#   instance:   nil,
#   befor:      nil
# }

Parameters:

  • options_hash (Hash) (defaults to: {})

    options override

Returns:



103
104
105
106
107
108
109
# File 'lib/arstotzka/config.rb', line 103

def options(options_hash = {})
  Options.new(
    to_hash.symbolize_keys.merge(
      options_hash.symbolize_keys
    )
  )
end