Module: Alba::Resource::ClassMethods
- Included in:
- Alba::Resource
- Defined in:
- lib/alba/resource.rb
Overview
Class methods
Instance Method Summary collapse
-
#association(name, condition = nil, resource: nil, key: nil, params: {}, **options, &block) ⇒ void
(also: #one, #many, #has_one, #has_many)
Set association.
-
#attribute(name, **options, &block) ⇒ void
Set an attribute with the given block.
-
#attributes(*attrs, if: nil, **attrs_with_types) ⇒ void
Set multiple attributes at once.
-
#collection_key(key) ⇒ Object
Sets key for collection serialization.
-
#helper(mod = @_helper || Module.new, &block) ⇒ Object
Define helper methods.
-
#layout(file: nil, inline: nil) ⇒ Object
Set layout.
-
#meta(&block) ⇒ Object
Set metadata.
-
#nested_attribute(name, **options, &block) ⇒ void
(also: #nested)
Set a nested attribute with the given block.
-
#on_error(handler = nil, &block) ⇒ Object
Set error handler If this is set it’s used as a error handler overriding global one.
-
#on_nil(&block) ⇒ Object
Set nil handler.
-
#prefer_object_method! ⇒ Object
DSL for alias, purely for readability.
-
#prefer_resource_method! ⇒ Object
DSL for alias, purely for readability.
-
#root_key(key, key_for_collection = nil) ⇒ Object
Set root key.
-
#root_key! ⇒ Object
Set root key to true.
-
#root_key_for_collection(key) ⇒ Object
Set root key for collection.
-
#transform_keys(type, root: true, cascade: true) ⇒ Object
Transform keys as specified type.
Instance Method Details
#association(name, condition = nil, resource: nil, key: nil, params: {}, **options, &block) ⇒ void Also known as: one, many, has_one, has_many
This method returns an undefined value.
Set association
373 374 375 376 377 378 379 |
# File 'lib/alba/resource.rb', line 373 def association(name, condition = nil, resource: nil, key: nil, params: {}, **, &block) transformation = @_key_transformation_cascade ? @_transform_type : :none assoc = Association.new( name: name, condition: condition, resource: resource, params: params, nesting: nesting, key_transformation: transformation, helper: @_helper, &block ) @_attributes[key&.to_sym || name.to_sym] = [:if] ? ConditionalAttribute.new(body: assoc, condition: [:if]) : assoc end |
#attribute(name, **options, &block) ⇒ void
This method returns an undefined value.
Set an attribute with the given block
355 356 357 358 359 |
# File 'lib/alba/resource.rb', line 355 def attribute(name, **, &block) raise ArgumentError, 'No block given in attribute method' unless block @_attributes[name.to_sym] = [:if] ? ConditionalAttribute.new(body: block, condition: [:if]) : block end |
#attributes(*attrs, if: nil, **attrs_with_types) ⇒ void
This method returns an undefined value.
Set multiple attributes at once
321 322 323 324 325 |
# File 'lib/alba/resource.rb', line 321 def attributes(*attrs, if: nil, **attrs_with_types) if_value = binding.local_variable_get(:if) assign_attributes(attrs, if_value) assign_attributes_with_types(attrs_with_types, if_value) end |
#collection_key(key) ⇒ Object
Sets key for collection serialization
471 472 473 |
# File 'lib/alba/resource.rb', line 471 def collection_key(key) @_collection_key = key.to_sym end |
#helper(mod = @_helper || Module.new, &block) ⇒ Object
Define helper methods
508 509 510 511 512 |
# File 'lib/alba/resource.rb', line 508 def helper(mod = @_helper || Module.new, &block) mod.module_eval(&block) if block extend mod @_helper = mod end |
#layout(file: nil, inline: nil) ⇒ Object
Set layout
445 446 447 |
# File 'lib/alba/resource.rb', line 445 def layout(file: nil, inline: nil) @_layout = Layout.new(file: file, inline: inline) end |
#meta(&block) ⇒ Object
Set metadata
437 438 439 |
# File 'lib/alba/resource.rb', line 437 def (&block) @_meta = block end |
#nested_attribute(name, **options, &block) ⇒ void Also known as: nested
This method returns an undefined value.
Set a nested attribute with the given block
402 403 404 405 406 407 408 |
# File 'lib/alba/resource.rb', line 402 def nested_attribute(name, **, &block) raise ArgumentError, 'No block given in attribute method' unless block key_transformation = @_key_transformation_cascade ? @_transform_type : :none attribute = NestedAttribute.new(key_transformation: key_transformation, &block) @_attributes[name.to_sym] = [:if] ? ConditionalAttribute.new(body: attribute, condition: [:if]) : attribute end |
#on_error(handler = nil, &block) ⇒ Object
Set error handler If this is set it’s used as a error handler overriding global one
480 481 482 483 484 485 |
# File 'lib/alba/resource.rb', line 480 def on_error(handler = nil, &block) raise ArgumentError, 'You cannot specify error handler with both Symbol and block' if handler && block raise ArgumentError, 'You must specify error handler with either Symbol or block' unless handler || block @_on_error = block || validated_error_handler(handler) end |
#on_nil(&block) ⇒ Object
Set nil handler
501 502 503 |
# File 'lib/alba/resource.rb', line 501 def on_nil(&block) @_on_nil = block end |
#prefer_object_method! ⇒ Object
DSL for alias, purely for readability
520 521 522 |
# File 'lib/alba/resource.rb', line 520 def prefer_object_method! alias_method :fetch_attribute_from_object_and_resource, :_fetch_attribute_from_object_first end |
#prefer_resource_method! ⇒ Object
DSL for alias, purely for readability
515 516 517 |
# File 'lib/alba/resource.rb', line 515 def prefer_resource_method! alias_method :fetch_attribute_from_object_and_resource, :_fetch_attribute_from_resource_first end |
#root_key(key, key_for_collection = nil) ⇒ Object
Set root key
416 417 418 419 |
# File 'lib/alba/resource.rb', line 416 def root_key(key, key_for_collection = nil) @_key = key.to_sym @_key_for_collection = key_for_collection&.to_sym end |
#root_key! ⇒ Object
Set root key to true
431 432 433 434 |
# File 'lib/alba/resource.rb', line 431 def root_key! @_key = true @_key_for_collection = true end |
#root_key_for_collection(key) ⇒ Object
Set root key for collection
425 426 427 428 |
# File 'lib/alba/resource.rb', line 425 def root_key_for_collection(key) @_key = true @_key_for_collection = key.to_sym end |
#transform_keys(type, root: true, cascade: true) ⇒ Object
Transform keys as specified type
456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/alba/resource.rb', line 456 def transform_keys(type, root: true, cascade: true) type = type.to_sym unless %i[none snake camel lower_camel dash].include?(type) # This should be `ArgumentError` but for backward compatibility it raises `Alba::Error` raise ::Alba::Error, "Unknown transform type: #{type}. Supported type are :camel, :lower_camel and :dash." end @_transform_type = type @_transforming_root_key = root @_key_transformation_cascade = cascade end |