Class: Takagi::Core::AttributeSet
- Inherits:
-
Object
- Object
- Takagi::Core::AttributeSet
- Defined in:
- lib/takagi/core/attribute_set.rb
Overview
Encapsulates CoRE Link Format attribute handling for a single route. The DSL is shared between request-time handlers and boot-time helpers, so changes applied here are idempotent and safe to call repeatedly.
Constant Summary collapse
- CONTENT_FORMATS =
{ 'text/plain' => 0, 'application/link-format' => 40, 'application/xml' => 41, 'application/octet-stream' => 42, 'application/exi' => 47, 'application/json' => 50, 'application/cbor' => 60 }.freeze
- REMOVE =
Object.new
Instance Attribute Summary collapse
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
-
#apply! ⇒ Object
Persists staged attribute overrides back onto the route metadata.
- #attribute(name, value) ⇒ Object
-
#core(&block) ⇒ Object
Evaluates a block that configures any mix of CoRE attributes.
- #ct(value) ⇒ Object (also: #content_format)
-
#initialize(metadata) ⇒ AttributeSet
constructor
A new instance of AttributeSet.
- #interface(*values) ⇒ Object (also: #if_)
- #obs(value = true) ⇒ Object (also: #observable)
- #rt(*values) ⇒ Object
- #sz(value) ⇒ Object
- #title(value) ⇒ Object
Constructor Details
#initialize(metadata) ⇒ AttributeSet
Returns a new instance of AttributeSet.
23 24 25 26 |
# File 'lib/takagi/core/attribute_set.rb', line 23 def initialize() @metadata = @overrides = {} end |
Instance Attribute Details
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
21 22 23 |
# File 'lib/takagi/core/attribute_set.rb', line 21 def @metadata end |
Instance Method Details
#apply! ⇒ Object
Persists staged attribute overrides back onto the route metadata. Invoked automatically after request handling, but also exposed so callers (e.g. Takagi::Router#configure_core) can persist updates.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/takagi/core/attribute_set.rb', line 68 def apply! return if @overrides.empty? @overrides.each do |key, value| if value.equal?(REMOVE) .delete(key) next end coerced = if value.is_a?(Array) normalized = value.map(&:to_s) normalized.length == 1 ? normalized.first : normalized else value end [key] = coerced end @overrides.clear end |
#attribute(name, value) ⇒ Object
61 62 63 |
# File 'lib/takagi/core/attribute_set.rb', line 61 def attribute(name, value) (name.to_sym, value.nil? ? REMOVE : value) end |
#core(&block) ⇒ Object
Evaluates a block that configures any mix of CoRE attributes. Accepts the same DSL as the runtime helpers exposed in RouteContext.
30 31 32 |
# File 'lib/takagi/core/attribute_set.rb', line 30 def core(&block) instance_exec(&block) if block end |
#ct(value) ⇒ Object Also known as: content_format
34 35 36 |
# File 'lib/takagi/core/attribute_set.rb', line 34 def ct(value) (:ct, normalize_content_format(value)) end |
#interface(*values) ⇒ Object Also known as: if_
56 57 58 |
# File 'lib/takagi/core/attribute_set.rb', line 56 def interface(*values) assign_list(:if, values) end |
#obs(value = true) ⇒ Object Also known as: observable
47 48 49 |
# File 'lib/takagi/core/attribute_set.rb', line 47 def obs(value = true) (:obs, value ? true : REMOVE) end |
#rt(*values) ⇒ Object
52 53 54 |
# File 'lib/takagi/core/attribute_set.rb', line 52 def rt(*values) assign_list(:rt, values) end |
#sz(value) ⇒ Object
39 40 41 |
# File 'lib/takagi/core/attribute_set.rb', line 39 def sz(value) (:sz, value.to_i) end |
#title(value) ⇒ Object
43 44 45 |
# File 'lib/takagi/core/attribute_set.rb', line 43 def title(value) (:title, value.to_s) end |