Class: Weft::Attributes
- Inherits:
-
Object
- Object
- Weft::Attributes
- Defined in:
- lib/weft/attributes.rb
Overview
Value object representing a component's resolved wire attributes. Provides method-style access with a clear collision-resolution rule: declared attribute names win, then the underlying Hash API is available for any name not declared as an attribute.
Action callables receive a ready-made instance (the sole argument to a +performs+/+transfers+ block); you don't construct these yourself:
attrs.status # => "shipped" (declared attribute)
attrs.count # => 42 (declared attribute — wins over Hash#count)
attrs[:status] # => "shipped" (explicit hash access)
attrs.select { ... } # delegates to the underlying hash
attrs.to_h # => the underlying hash (explicit escape hatch)
Class Method Summary collapse
-
.extract_from(raw, using:) ⇒ Object
Build an Attributes instance by extracting declared keys from a raw attributes hash, applying defaults for missing keys.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(data) ⇒ Attributes
constructor
private
Constructed internally (see Attributes.extract_from and the Router's resolver).
- #key?(key) ⇒ Boolean
- #method_missing(name, *args, **kwargs, &block) ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(data) ⇒ Attributes
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.
Constructed internally (see extract_from and the Router's resolver).
35 36 37 |
# File 'lib/weft/attributes.rb', line 35 def initialize(data) @data = data end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **kwargs, &block) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/weft/attributes.rb', line 55 def method_missing(name, *args, **kwargs, &block) if @data.key?(name) && args.empty? && kwargs.empty? && !block @data[name] elsif @data.respond_to?(name) @data.public_send(name, *args, **kwargs, &block) else super end end |
Class Method Details
.extract_from(raw, using:) ⇒ Object
Build an Attributes instance by extracting declared keys from a raw attributes hash, applying defaults for missing keys. Does not mutate the raw hash.
schema = { status: { default: "pending" }, count: { default: 0 } }
raw = { status: "shipped", class: "big" }
Weft::Attributes.extract_from(raw, using: schema)
# => Attributes{ status: "shipped", count: 0 }
26 27 28 29 30 31 |
# File 'lib/weft/attributes.rb', line 26 def self.extract_from(raw, using:) data = using.to_h do |name, | [name, raw.fetch(name, [:default])] end new(data) end |
Instance Method Details
#[](key) ⇒ Object
39 40 41 |
# File 'lib/weft/attributes.rb', line 39 def [](key) @data[key] end |
#key?(key) ⇒ Boolean
43 44 45 |
# File 'lib/weft/attributes.rb', line 43 def key?(key) @data.key?(key) end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
51 52 53 |
# File 'lib/weft/attributes.rb', line 51 def respond_to_missing?(name, include_private = false) @data.key?(name) || @data.respond_to?(name, include_private) || super end |
#to_h ⇒ Object
47 48 49 |
# File 'lib/weft/attributes.rb', line 47 def to_h @data end |