Module: Alba::Resource::ClassMethods
- Includes:
- _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
-
#association(name, modifier = 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 ⇒ self
-
#helper(mod = @_helper || Module.new, &block) ⇒ void
Define helper methods.
- #include ⇒ self
- #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 ⇒ String?
-
#nested_attribute(name, **options, &block) ⇒ void
(also: #nested)
Set a nested attribute with the given block.
- #new ⇒ Resource::_InstanceMethods
-
#on_error(handler = nil, &block) ⇒ void
Set error handler Defaults to raising errors when no handler is set.
-
#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 ⇒ void
-
#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.
Instance Method Details
#_resource_methods ⇒ Array[Symbol]
142 |
# File 'sig/alba/resource.rbs', line 142
def _resource_methods: () -> Array[Symbol]
|
#alias_method ⇒ Symbol
132 |
# File 'sig/alba/resource.rbs', line 132
def alias_method: (Symbol, Symbol) -> Symbol
|
#association(name, modifier = 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
407 408 409 410 411 412 413 414 415 416 |
# File 'lib/alba/resource.rb', line 407 def association(name, modifier = 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, modifier: modifier, 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
377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/alba/resource.rb', line 377 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
344 345 346 347 348 |
# File 'lib/alba/resource.rb', line 344 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.
137 |
# File 'sig/alba/resource.rbs', line 137
def class_eval: [A] () { () [self: untyped] -> A } -> A
|
#collection_key(key) ⇒ void
This method returns an undefined value.
Sets key for collection serialization
545 546 547 |
# File 'lib/alba/resource.rb', line 545 def collection_key(key) @_collection_key = key.to_sym end |
#dup ⇒ Object
140 |
# File 'sig/alba/resource.rbs', line 140
def dup: () -> untyped
|
#extend ⇒ self
136 |
# File 'sig/alba/resource.rbs', line 136
def extend: (*Module) -> self
|
#helper(mod = @_helper || Module.new, &block) ⇒ void
This method returns an undefined value.
Define helper methods
585 586 587 588 589 590 591 |
# File 'lib/alba/resource.rb', line 585 def helper(mod = @_helper || Module.new, &block) mod.module_eval(&block) if block include(mod) extend(mod) @_helper = mod end |
#include ⇒ self
135 |
# File 'sig/alba/resource.rbs', line 135
def include: (*Module) -> self
|
#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.
330 331 332 333 |
# File 'lib/alba/resource.rb', line 330 def inherited(subclass) super INTERNAL_VARIABLES.each_key { |name| subclass.instance_variable_set(:"@#{name}", instance_variable_get(:"@#{name}").clone) } end |
#instance_method ⇒ UnboundMethod
134 |
# File 'sig/alba/resource.rbs', line 134
def instance_method: (Symbol) -> UnboundMethod
|
#layout(file: nil, inline: nil) ⇒ void
This method returns an undefined value.
Set layout
499 500 501 |
# File 'lib/alba/resource.rb', line 499 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
490 491 492 |
# File 'lib/alba/resource.rb', line 490 def (key = :meta, &block) @_meta = [key, block] end |
#method_added(method_name) ⇒ Object
This method_added is used for defining "resource methods"
317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/alba/resource.rb', line 317 def method_added(method_name) case method_name 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.
138 |
# File 'sig/alba/resource.rbs', line 138
def module_eval: [A] () { () [self: untyped] -> A } -> A
|
#name ⇒ String?
139 |
# File 'sig/alba/resource.rbs', line 139
def name: () -> String?
|
#nested_attribute(name, **options, &block) ⇒ void Also known as: nested
This method returns an undefined value.
Set a nested attribute with the given block
438 439 440 441 442 443 444 |
# File 'lib/alba/resource.rb', line 438 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 |
#new ⇒ Resource::_InstanceMethods
141 |
# File 'sig/alba/resource.rbs', line 141
def new: (*untyped, **untyped) -> Resource::_InstanceMethods
|
#on_error(handler = nil, &block) ⇒ void
This method returns an undefined value.
Set error handler Defaults to raising errors when no handler is set.
555 556 557 558 559 560 |
# File 'lib/alba/resource.rb', line 555 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
577 578 579 |
# File 'lib/alba/resource.rb', line 577 def on_nil(&block) @_on_nil = block end |
#prefer_object_method! ⇒ void
This method returns an undefined value.
DSL for alias, purely for readability
601 602 603 |
# File 'lib/alba/resource.rb', line 601 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
595 596 597 |
# File 'lib/alba/resource.rb', line 595 def prefer_resource_method! alias_method :fetch_attribute_from_object_and_resource, :_fetch_attribute_from_resource_first end |
#private ⇒ void
This method returns an undefined value.
133 |
# File 'sig/alba/resource.rbs', line 133
def private: (*Symbol) -> void
|
#root_key(key, key_for_collection = nil) ⇒ void
This method returns an undefined value.
Set root key
466 467 468 469 |
# File 'lib/alba/resource.rb', line 466 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
483 484 485 486 |
# File 'lib/alba/resource.rb', line 483 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
476 477 478 479 |
# File 'lib/alba/resource.rb', line 476 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
453 454 455 456 457 458 |
# File 'lib/alba/resource.rb', line 453 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
511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/alba/resource.rb', line 511 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
527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/alba/resource.rb', line 527 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 |