Module: Alba::Resource::ClassMethods
- Included in:
- Alba::Resource
- Defined in:
- lib/alba/resource.rb,
sig/alba/resource.rbs
Overview
Class methods
Instance Method Summary collapse
- #_resource_methods ⇒ Array[Symbol]
- #alias_method ⇒ Symbol
- #assign_attributes(attrs, if_value) ⇒ Object
- #assign_attributes_with_types(attrs_with_types, if_value, &block) ⇒ void
-
#association(name, condition = nil, resource: nil, serializer: nil, source: nil, key: nil, with_traits: nil, params: EMPTY_HASH, **options, &block) ⇒ void
(also: #one, #many, #has_one, #has_many)
Set association.
-
#attribute(name = nil, if: nil, **name_with_type, &block) ⇒ void
Set an attribute with the given block.
-
#attributes(*attrs, if: nil, **attrs_with_types) ⇒ void
Set multiple attributes at once.
- #class_eval ⇒ void
-
#collection_key(key) ⇒ void
Sets key for collection serialization.
- #dup ⇒ Object
- #extend ⇒ Object
-
#helper(mod = @_helper || Module.new, &block) ⇒ void
Define helper methods.
- #include ⇒ Object
- #inherited(subclass) ⇒ Object private
- #instance_method ⇒ UnboundMethod
-
#layout(file: nil, inline: nil) ⇒ void
Set layout.
-
#meta(key = :meta, &block) ⇒ void
Set metadata.
-
#method_added(method_name) ⇒ Object
This
method_addedis used for defining "resource methods". - #module_eval ⇒ void
- #name ⇒ Object
-
#nested_attribute(name, **options, &block) ⇒ void
(also: #nested)
Set a nested attribute with the given block.
- #nesting ⇒ String?
- #new ⇒ Object
-
#on_error(handler = nil, &block) ⇒ void
Set error handler If this is set it's used as a error handler overriding global one.
-
#on_nil(&block) ⇒ void
Set nil handler.
-
#prefer_object_method! ⇒ void
DSL for alias, purely for readability.
-
#prefer_resource_method! ⇒ void
DSL for alias, purely for readability.
- #private ⇒ Object
-
#root_key(key, key_for_collection = nil) ⇒ void
Set root key.
-
#root_key! ⇒ void
Set root key to true.
-
#root_key_for_collection(key) ⇒ void
Set root key for collection.
-
#trait(name, &block) ⇒ void
Set a trait.
-
#transform_keys(type, root: true, cascade: true) ⇒ void
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.
- #validated_error_handler(handler) ⇒ Symbol
Instance Method Details
#_resource_methods ⇒ Array[Symbol]
108 |
# File 'sig/alba/resource.rbs', line 108
def _resource_methods: () -> Array[Symbol]
|
#alias_method ⇒ Symbol
98 |
# File 'sig/alba/resource.rbs', line 98
def alias_method: (Symbol, Symbol) -> Symbol
|
#assign_attributes(attrs, if_value) ⇒ Object
397 398 399 400 401 402 403 |
# File 'lib/alba/resource.rb', line 397 def assign_attributes(attrs, if_value) attrs.each do |attr_name| attr_name = attr_name.to_sym attr = if_value ? ConditionalAttribute.new(body: attr_name, condition: if_value) : attr_name @_attributes[attr_name] = attr end end |
#assign_attributes_with_types(attrs_with_types, if_value, &block) ⇒ void
This method returns an undefined value.
406 407 408 409 410 411 412 413 414 |
# File 'lib/alba/resource.rb', line 406 def assign_attributes_with_types(attrs_with_types, if_value, &block) attrs_with_types.each do |attr_name, type_and_converter| attr_name = attr_name.to_sym type, type_converter = type_and_converter typed_attr = TypedAttribute.new(name: attr_name, type: type, converter: type_converter, &block) attr = if_value ? ConditionalAttribute.new(body: typed_attr, condition: if_value) : typed_attr @_attributes[attr_name] = attr end end |
#association(name, condition = nil, resource: nil, serializer: nil, source: nil, key: nil, with_traits: nil, params: EMPTY_HASH, **options, &block) ⇒ void Also known as: one, many, has_one, has_many
This method returns an undefined value.
Set association
454 455 456 457 458 459 460 461 462 463 |
# File 'lib/alba/resource.rb', line 454 def association(name, condition = nil, resource: nil, serializer: nil, source: nil, key: nil, with_traits: nil, params: EMPTY_HASH, **, &block) resource ||= serializer transformation = @_key_transformation_cascade ? @_transform_type : :none assoc = Association.new( name: name, condition: condition, resource: resource, source: source, with_traits: with_traits, 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 = nil, if: nil, **name_with_type, &block) ⇒ void
This method returns an undefined value.
Set an attribute with the given block
424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/alba/resource.rb', line 424 def attribute(name = nil, if: nil, **name_with_type, &block) if_value = binding.local_variable_get(:if) raise ArgumentError, 'No block given in attribute method' unless block raise ArgumentError, 'You must specify either name or name with type' if name.nil? && name_with_type.empty? if name.nil? assign_attributes_with_types(name_with_type, if_value, &block) else # Symbol attr = if_value ? ConditionalAttribute.new(body: block, condition: if_value) : block @_attributes[name.to_sym] = attr end end |
#attributes(*attrs, if: nil, **attrs_with_types) ⇒ void
This method returns an undefined value.
Set multiple attributes at once
391 392 393 394 395 |
# File 'lib/alba/resource.rb', line 391 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 |
#class_eval ⇒ void
This method returns an undefined value.
103 |
# File 'sig/alba/resource.rbs', line 103
def class_eval: [A] () { () [self: untyped] -> A } -> A
|
#collection_key(key) ⇒ void
This method returns an undefined value.
Sets key for collection serialization
593 594 595 |
# File 'lib/alba/resource.rb', line 593 def collection_key(key) @_collection_key = key.to_sym end |
#dup ⇒ Object
106 |
# File 'sig/alba/resource.rbs', line 106
def dup: () -> untyped
|
#extend ⇒ Object
102 |
# File 'sig/alba/resource.rbs', line 102
def extend: (*Module) -> untyped
|
#helper(mod = @_helper || Module.new, &block) ⇒ void
This method returns an undefined value.
Define helper methods
633 634 635 636 637 638 639 |
# File 'lib/alba/resource.rb', line 633 def helper(mod = @_helper || Module.new, &block) mod.module_eval(&block) if block include(mod) extend(mod) @_helper = mod end |
#include ⇒ Object
101 |
# File 'sig/alba/resource.rbs', line 101
def include: (*Module) -> untyped
|
#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.
377 378 379 380 |
# File 'lib/alba/resource.rb', line 377 def inherited(subclass) super INTERNAL_VARIABLES.each_key { |name| subclass.instance_variable_set(:"@#{name}", instance_variable_get(:"@#{name}").clone) } end |
#instance_method ⇒ UnboundMethod
100 |
# File 'sig/alba/resource.rbs', line 100
def instance_method: (Symbol) -> UnboundMethod
|
#layout(file: nil, inline: nil) ⇒ void
This method returns an undefined value.
Set layout
547 548 549 |
# File 'lib/alba/resource.rb', line 547 def layout(file: nil, inline: nil) @_layout = Layout.new(file: file, inline: inline) end |
#meta(key = :meta, &block) ⇒ void
This method returns an undefined value.
Set metadata
538 539 540 |
# File 'lib/alba/resource.rb', line 538 def (key = :meta, &block) @_meta = [key, block] end |
#method_added(method_name) ⇒ Object
This method_added is used for defining "resource methods"
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/alba/resource.rb', line 356 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 |
#module_eval ⇒ void
This method returns an undefined value.
104 |
# File 'sig/alba/resource.rbs', line 104
def module_eval: [A] () { () [self: untyped] -> A } -> A
|
#name ⇒ Object
105 |
# File 'sig/alba/resource.rbs', line 105
def name: () -> untyped
|
#nested_attribute(name, **options, &block) ⇒ void Also known as: nested
This method returns an undefined value.
Set a nested attribute with the given block
486 487 488 489 490 491 492 |
# File 'lib/alba/resource.rb', line 486 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(klass: self, key_transformation: key_transformation, &block) @_attributes[name.to_sym] = [:if] ? ConditionalAttribute.new(body: attribute, condition: [:if]) : attribute end |
#nesting ⇒ String?
469 470 471 472 473 474 475 |
# File 'lib/alba/resource.rb', line 469 def nesting if name.nil? nil else name.rpartition('::').first.then { |n| n.empty? ? nil : n } end end |
#new ⇒ Object
107 |
# File 'sig/alba/resource.rbs', line 107
def new: (*untyped, **untyped) -> untyped
|
#on_error(handler = nil, &block) ⇒ void
This method returns an undefined value.
Set error handler If this is set it's used as a error handler overriding global one
603 604 605 606 607 608 |
# File 'lib/alba/resource.rb', line 603 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) ⇒ void
This method returns an undefined value.
Set nil handler
625 626 627 |
# File 'lib/alba/resource.rb', line 625 def on_nil(&block) @_on_nil = block end |
#prefer_object_method! ⇒ void
This method returns an undefined value.
DSL for alias, purely for readability
649 650 651 |
# File 'lib/alba/resource.rb', line 649 def prefer_object_method! alias_method :fetch_attribute_from_object_and_resource, :_fetch_attribute_from_object_first end |
#prefer_resource_method! ⇒ void
This method returns an undefined value.
DSL for alias, purely for readability
643 644 645 |
# File 'lib/alba/resource.rb', line 643 def prefer_resource_method! alias_method :fetch_attribute_from_object_and_resource, :_fetch_attribute_from_resource_first end |
#private ⇒ Object
99 |
# File 'sig/alba/resource.rbs', line 99
def private: (*Symbol) -> untyped
|
#root_key(key, key_for_collection = nil) ⇒ void
This method returns an undefined value.
Set root key
514 515 516 517 |
# File 'lib/alba/resource.rb', line 514 def root_key(key, key_for_collection = nil) @_key = key.to_sym @_key_for_collection = key_for_collection&.to_sym end |
#root_key! ⇒ void
This method returns an undefined value.
Set root key to true
531 532 533 534 |
# File 'lib/alba/resource.rb', line 531 def root_key! @_key = true @_key_for_collection = true end |
#root_key_for_collection(key) ⇒ void
This method returns an undefined value.
Set root key for collection
524 525 526 527 |
# File 'lib/alba/resource.rb', line 524 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
501 502 503 504 505 506 |
# File 'lib/alba/resource.rb', line 501 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) ⇒ void
This method returns an undefined value.
Transform keys as specified type
559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/alba/resource.rb', line 559 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
575 576 577 578 579 580 581 582 583 584 585 586 587 |
# File 'lib/alba/resource.rb', line 575 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 |
#validated_error_handler(handler) ⇒ Symbol
610 611 612 613 614 615 616 617 618 |
# File 'lib/alba/resource.rb', line 610 def validated_error_handler(handler) unless %i[raise ignore nullify].include?(handler) # For backward compatibility # TODO: Change this to ArgumentError raise Alba::Error, "Unknown error handler: #{handler}. It must be one of `:raise`, `:ignore` or `:nullify`." end handler end |