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, serializer: 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.
- #inherited(subclass) ⇒ Object private
-
#layout(file: nil, inline: nil) ⇒ Object
Set layout.
-
#meta(key = :meta, &block) ⇒ Object
Set metadata.
-
#method_added(method_name) ⇒ Object
This ‘method_added` is used for defining “resource methods”.
-
#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.
-
#trait(name, &block) ⇒ void
Set a trait.
-
#transform_keys(type, root: true, cascade: true) ⇒ Object
Transform keys as specified type.
-
#transform_keys!(type) ⇒ Object
Transform keys as specified type AFTER the class is defined Note that this is an experimental API and may be removed/changed.
Instance Method Details
#association(name, condition = nil, resource: nil, serializer: nil, key: nil, params: {}, **options, &block) ⇒ void Also known as: one, many, has_one, has_many
This method returns an undefined value.
Set association
441 442 443 444 445 446 447 448 |
# File 'lib/alba/resource.rb', line 441 def association(name, condition = nil, resource: nil, serializer: nil, key: nil, params: {}, **, &block) resource ||= serializer 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
422 423 424 425 426 |
# File 'lib/alba/resource.rb', line 422 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
388 389 390 391 392 |
# File 'lib/alba/resource.rb', line 388 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
571 572 573 |
# File 'lib/alba/resource.rb', line 571 def collection_key(key) @_collection_key = key.to_sym end |
#helper(mod = @_helper || Module.new, &block) ⇒ Object
Define helper methods
608 609 610 611 612 |
# File 'lib/alba/resource.rb', line 608 def helper(mod = @_helper || Module.new, &block) mod.module_eval(&block) if block extend mod @_helper = mod end |
#inherited(subclass) ⇒ Object
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.
374 375 376 377 |
# File 'lib/alba/resource.rb', line 374 def inherited(subclass) super INTERNAL_VARIABLES.each_key { |name| subclass.instance_variable_set(:"@#{name}", instance_variable_get(:"@#{name}").clone) } end |
#layout(file: nil, inline: nil) ⇒ Object
Set layout
527 528 529 |
# File 'lib/alba/resource.rb', line 527 def layout(file: nil, inline: nil) @_layout = Layout.new(file: file, inline: inline) end |
#meta(key = :meta, &block) ⇒ Object
Set metadata
519 520 521 |
# File 'lib/alba/resource.rb', line 519 def (key = :meta, &block) @_meta = [key, block] end |
#method_added(method_name) ⇒ Object
This ‘method_added` is used for defining “resource methods”
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/alba/resource.rb', line 353 def method_added(method_name) # rubocop:disable Metrics/MethodLength case method_name when :collection_converter, :converter warn "Defining ##{method_name} methods is deprecated", category: :deprecated, uplevel: 1 alias_method :serializable_hash_for_collection, :deprecated_serializable_hash_for_collection private(:serializable_hash_for_collection) alias_method :serializable_hash, :deprecated_serializable_hash alias_method :to_h, :deprecated_serializable_hash when :attributes warn 'Overriding `attributes` is deprecated, use `select` instead.', category: :deprecated, uplevel: 1 when :select @_select_arity = instance_method(:select).arity when :_setup # noop else _resource_methods << method_name.to_sym end super 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
471 472 473 474 475 476 477 |
# File 'lib/alba/resource.rb', line 471 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
580 581 582 583 584 585 |
# File 'lib/alba/resource.rb', line 580 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
601 602 603 |
# File 'lib/alba/resource.rb', line 601 def on_nil(&block) @_on_nil = block end |
#prefer_object_method! ⇒ Object
DSL for alias, purely for readability
620 621 622 |
# File 'lib/alba/resource.rb', line 620 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
615 616 617 |
# File 'lib/alba/resource.rb', line 615 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
498 499 500 501 |
# File 'lib/alba/resource.rb', line 498 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
513 514 515 516 |
# File 'lib/alba/resource.rb', line 513 def root_key! @_key = true @_key_for_collection = true end |
#root_key_for_collection(key) ⇒ Object
Set root key for collection
507 508 509 510 |
# File 'lib/alba/resource.rb', line 507 def root_key_for_collection(key) @_key = true @_key_for_collection = key.to_sym end |
#trait(name, &block) ⇒ void
This method returns an undefined value.
Set a trait
486 487 488 489 490 491 |
# File 'lib/alba/resource.rb', line 486 def trait(name, &block) raise ArgumentError, 'No block given in trait method' unless block name = name.to_sym @_traits[name] = block end |
#transform_keys(type, root: true, cascade: true) ⇒ Object
Transform keys as specified type
538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/alba/resource.rb', line 538 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 |
#transform_keys!(type) ⇒ Object
Transform keys as specified type AFTER the class is defined Note that this is an experimental API and may be removed/changed
554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/alba/resource.rb', line 554 def transform_keys!(type) dup.class_eval do transform_keys(type, root: @_transforming_root_key, cascade: @_key_transformation_cascade) if @_key_transformation_cascade # We need to update key transformation of associations and nested attributes @_attributes.each_value do |attr| attr.key_transformation = type if attr.is_a?(Association) || attr.is_a?(NestedAttribute) end end self # Return the new class end end |