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
-
.backend ⇒ Object
Returns the value of attribute backend.
-
.encoder ⇒ Object
Returns the value of attribute encoder.
-
.inflector ⇒ Object
Getter for inflector, a module responsible for inflecting strings.
Class Method Summary collapse
-
.disable_inference! ⇒ Object
deprecated
Deprecated.
Use Alba.inflector= instead
-
.enable_inference!(with:) ⇒ Object
deprecated
Deprecated.
Use Alba.inflector= instead
-
.find_type(name) ⇒ Alba::Type
Find type by name.
-
.hashify(object = nil, root_key: nil, &block) ⇒ String
Hashify the object with inline definitions.
-
.infer_resource_class(name, nesting: nil) ⇒ Class<Alba::Resource>
Resource class.
-
.inferring ⇒ Boolean
deprecated
Deprecated.
Use Alba.inflector instead
-
.register_type(name, check: false, converter: nil, auto_convert: false) ⇒ void
Register types, used for both builtin and custom types.
-
.regularize_key(key) ⇒ Symbol, ...
Regularize key to be either Symbol or String depending on @symbolize_keys Returns nil if key is nil.
-
.reset! ⇒ Object
Reset config variables Useful for test cleanup.
-
.resource_class(&block) ⇒ Class<Alba::Resource>
Resource class.
-
.resource_with(object, &block) ⇒ Object
This method could be part of public API, but for now it’s private.
-
.serialize(object = nil, root_key: nil, &block) ⇒ String
Serialize the object with inline definitions.
-
.stringify_keys! ⇒ Object
Configure Alba to stringify (not symbolize) keys.
-
.symbolize_keys! ⇒ Object
Configure Alba to symbolize keys.
-
.transform_key(key, transform_type:) ⇒ String
Transform a key with given transform_type.
Class Attribute Details
.backend ⇒ Object
Returns the value of attribute backend.
14 15 16 |
# File 'lib/alba.rb', line 14 def backend @backend end |
.encoder ⇒ Object
Returns the value of attribute encoder.
14 15 16 |
# File 'lib/alba.rb', line 14 def encoder @encoder end |
.inflector ⇒ Object
Getter for inflector, a module responsible for inflecting strings
17 18 19 |
# File 'lib/alba.rb', line 17 def inflector @inflector end |
Class Method Details
.disable_inference! ⇒ Object
Use inflector= instead
Disable inference for key and resource name
81 82 83 84 85 |
# File 'lib/alba.rb', line 81 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
Use inflector= instead
Enable inference for key and resource name
72 73 74 75 76 |
# File 'lib/alba.rb', line 72 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
178 179 180 181 182 |
# File 'lib/alba.rb', line 178 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
61 62 63 64 |
# File 'lib/alba.rb', line 61 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.
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/alba.rb', line 115 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 |
.inferring ⇒ Boolean
Use inflector instead
Returns whether inference is enabled or not.
89 90 91 92 |
# File 'lib/alba.rb', line 89 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
171 172 173 |
# File 'lib/alba.rb', line 171 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
141 142 143 144 145 |
# File 'lib/alba.rb', line 141 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
186 187 188 189 190 191 192 193 |
# File 'lib/alba.rb', line 186 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.
105 106 107 108 109 110 |
# File 'lib/alba.rb', line 105 def resource_class(&block) klass = Class.new klass.include(Alba::Resource) klass.class_eval(&block) if block klass end |
.resource_with(object, &block) ⇒ Object
This method could be part of public API, but for now it’s private
196 197 198 199 200 |
# File 'lib/alba.rb', line 196 def resource_with(object, &block) klass = block ? resource_class(&block) : infer_resource_class(object.class.name) klass.new(object) end |
.serialize(object = nil, root_key: nil, &block) ⇒ String
Serialize the object with inline definitions
49 50 51 52 |
# File 'lib/alba.rb', line 49 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
132 133 134 |
# File 'lib/alba.rb', line 132 def stringify_keys! @symbolize_keys = false end |
.symbolize_keys! ⇒ Object
Configure Alba to symbolize keys
127 128 129 |
# File 'lib/alba.rb', line 127 def symbolize_keys! @symbolize_keys = true end |
.transform_key(key, transform_type:) ⇒ String
Transform a key with given transform_type
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/alba.rb', line 152 def transform_key(key, transform_type:) raise Alba::Error, 'Inflector is nil. You must set inflector before transforming keys.' unless inflector key = key.to_s k = case transform_type when :camel then inflector.camelize(key) when :lower_camel then inflector.camelize_lower(key) when :dash then inflector.dasherize(key) when :snake then inflector.underscore(key) else raise Alba::Error, "Unknown transform type: #{transform_type}" end regularize_key(k) end |