Module: Alba

Defined in:
lib/alba.rb,
lib/alba/type.rb,
lib/alba/errors.rb,
lib/alba/layout.rb,
lib/alba/railtie.rb,
lib/alba/resource.rb,
lib/alba/constants.rb,
lib/alba/association.rb,
lib/alba/deprecation.rb,
lib/alba/typed_attribute.rb,
lib/alba/nested_attribute.rb,
lib/alba/default_inflector.rb,
lib/alba/conditional_attribute.rb

Overview

This file includes public constants to prevent circular dependencies.

Defined Under Namespace

Modules: DefaultInflector, Deprecation, Resource Classes: Association, ConditionalAttribute, Error, Layout, NestedAttribute, Railtie, Type, TypedAttribute, UnsupportedBackend, UnsupportedType

Constant Summary collapse

Serializer =
Resource
REMOVE_KEY =

A constant to remove key from serialized JSON

Object.new.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.backendObject

Returns the value of attribute backend.



12
13
14
# File 'lib/alba.rb', line 12

def backend
  @backend
end

.encoderObject

Returns the value of attribute encoder.



12
13
14
# File 'lib/alba.rb', line 12

def encoder
  @encoder
end

.inflectorObject

Getter for inflector, a module responsible for inflecting strings



15
16
17
# File 'lib/alba.rb', line 15

def inflector
  @inflector
end

Class Method Details

.disable_inference!Object

Deprecated.

Use inflector= instead

Disable inference for key and resource name



79
80
81
82
83
# File 'lib/alba.rb', line 79

def disable_inference!
  Alba::Deprecation.warn('Alba.disable_inference! is deprecated. Use `Alba.inflector = nil` instead.')
  @inferring = false
  @inflector = nil
end

.enable_inference!(with:) ⇒ Object

Deprecated.

Use inflector= instead

Enable inference for key and resource name

Parameters:

  • with (Symbol, Class, Module)

    inflector When it’s a Symbol, it sets inflector with given name When it’s a Class or a Module, it sets given object to inflector



70
71
72
73
74
# File 'lib/alba.rb', line 70

def enable_inference!(with:)
  Alba::Deprecation.warn('Alba.enable_inference! is deprecated. Use `Alba.inflector=` instead.')
  @inflector = inflector_from(with)
  @inferring = true
end

.find_type(name) ⇒ Alba::Type

Find type by name

Returns:



156
157
158
159
160
# File 'lib/alba.rb', line 156

def find_type(name)
  @types.fetch(name) do
    raise(Alba::UnsupportedType, "Unknown type: #{name}")
  end
end

.hashify(object = nil, root_key: nil, &block) ⇒ String

Hashify the object with inline definitions

Parameters:

  • object (Object) (defaults to: nil)

    the object to be serialized

  • root_key (Symbol, nil, true) (defaults to: nil)
  • block (Block)

    resource block

Returns:

  • (String)

    serialized JSON string

Raises:

  • (ArgumentError)

    if block is absent or ‘with` argument’s type is wrong



59
60
61
62
# File 'lib/alba.rb', line 59

def hashify(object = nil, root_key: nil, &block)
  resource = resource_with(object, &block)
  resource.as_json(root_key: root_key)
end

.infer_resource_class(name, nesting: nil) ⇒ Class<Alba::Resource>

Returns resource class.

Parameters:

  • name (String)

    a String Alba infers resource name with

  • nesting (String, nil) (defaults to: nil)

    namespace Alba tries to find resource class in

Returns:

Raises:



113
114
115
116
117
118
119
120
121
122
# File 'lib/alba.rb', line 113

def infer_resource_class(name, nesting: nil)
  raise Alba::Error, 'Inference is disabled so Alba cannot infer resource name. Set inflector before use.' unless Alba.inflector

  const_parent = nesting.nil? ? Object : Object.const_get(nesting)
  begin
    const_parent.const_get("#{inflector.classify(name)}Resource")
  rescue NameError # Retry for serializer
    const_parent.const_get("#{inflector.classify(name)}Serializer")
  end
end

.inferringBoolean

Deprecated.

Use inflector instead

Returns whether inference is enabled or not.

Returns:

  • (Boolean)

    whether inference is enabled or not



87
88
89
90
# File 'lib/alba.rb', line 87

def inferring
  Alba::Deprecation.warn('Alba.inferring is deprecated. Use `Alba.inflector` instead.')
  @inferring
end

.register_type(name, check: false, converter: nil, auto_convert: false) ⇒ void

This method returns an undefined value.

Register types, used for both builtin and custom types

See Also:



149
150
151
# File 'lib/alba.rb', line 149

def register_type(name, check: false, converter: nil, auto_convert: false)
  @types[name] = Type.new(name, check: check, converter: converter, auto_convert: auto_convert)
end

.regularize_key(key) ⇒ Symbol, ...

Regularize key to be either Symbol or String depending on @symbolize_keys Returns nil if key is nil

Parameters:

  • key (String, Symbol, nil)

Returns:

  • (Symbol, String, nil)


139
140
141
142
143
# File 'lib/alba.rb', line 139

def regularize_key(key)
  return if key.nil?

  @symbolize_keys ? key.to_sym : key.to_s
end

.reset!Object

Reset config variables Useful for test cleanup



164
165
166
167
168
169
170
171
# File 'lib/alba.rb', line 164

def reset!
  @encoder = default_encoder
  @symbolize_keys = false
  @_on_error = :raise
  @_on_nil = nil
  @types = {}
  register_default_types
end

.resource_class(&block) ⇒ Class<Alba::Resource>

Returns resource class.

Parameters:

  • block (Block)

    resource body

Returns:



103
104
105
106
107
108
# File 'lib/alba.rb', line 103

def resource_class(&block)
  klass = Class.new
  klass.include(Alba::Resource)
  klass.class_eval(&block) if block
  klass
end

.serialize(object = nil, root_key: nil, &block) ⇒ String

Serialize the object with inline definitions

Parameters:

  • object (Object) (defaults to: nil)

    the object to be serialized

  • root_key (Symbol, nil, true) (defaults to: nil)
  • block (Block)

    resource block

Returns:

  • (String)

    serialized JSON string

Raises:

  • (ArgumentError)

    if block is absent or ‘with` argument’s type is wrong



47
48
49
50
# File 'lib/alba.rb', line 47

def serialize(object = nil, root_key: nil, &block)
  resource = resource_with(object, &block)
  resource.serialize(root_key: root_key)
end

.stringify_keys!Object

Configure Alba to stringify (not symbolize) keys



130
131
132
# File 'lib/alba.rb', line 130

def stringify_keys!
  @symbolize_keys = false
end

.symbolize_keys!Object

Configure Alba to symbolize keys



125
126
127
# File 'lib/alba.rb', line 125

def symbolize_keys!
  @symbolize_keys = true
end