Class: Takagi::Core::AttributeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/takagi/core/attribute_set.rb,
sig/takagi/core/attribute_set.rbs

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 =

Returns:

  • ({ "text/plain" => 0, "application/link-format" => 40, "application/xml" => 41, "application/octet-stream" => 42, "application/exi" => 47, "application/json" => 50, "application/cbor" => 60 })
{
  '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 =

Returns:

  • (Object)
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata) ⇒ AttributeSet

Returns a new instance of AttributeSet.

Parameters:

  • metadata (Object)


23
24
25
26
# File 'lib/takagi/core/attribute_set.rb', line 23

def initialize()
  @metadata = 
  @overrides = {}
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.

Returns:

  • (Object)


21
22
23
# File 'lib/takagi/core/attribute_set.rb', line 21

def 
  @metadata
end

Instance Method Details

#apply!nil, untyped

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.

Returns:

  • (nil, untyped)


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

#assign_list(key, values) ⇒ nil, untyped

Parameters:

  • key (Object)
  • values (Object)

Returns:

  • (nil, untyped)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/takagi/core/attribute_set.rb', line 91

def assign_list(key, values)
  flattened = values.flatten.compact
  return if flattened.empty?

  overrides = Array(@overrides[key])
  overrides = [] if overrides.empty?

  flattened.each do |value|
    str = value.to_s
    overrides << str unless overrides.include?(str)
  end

  @overrides[key] = overrides
end

#attribute(name, value) ⇒ Object

Parameters:

  • name (Object)
  • value (Object)

Returns:

  • (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) ⇒ void

This method returns an undefined value.

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

Parameters:

  • value (Object)

Returns:

  • (Object)


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_

Parameters:

  • values (Object)

Returns:

  • (Object)


56
57
58
# File 'lib/takagi/core/attribute_set.rb', line 56

def interface(*values)
  assign_list(:if, values)
end

#metadata_override(key, value) ⇒ Object

Parameters:

  • key (Object)
  • value (Object)

Returns:

  • (Object)


106
107
108
# File 'lib/takagi/core/attribute_set.rb', line 106

def (key, value)
  @overrides[key] = value
end

#normalize_content_format(value) ⇒ Object

Parameters:

  • value (Object)

Returns:

  • (Object)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/takagi/core/attribute_set.rb', line 110

def normalize_content_format(value)
  case value
  when Integer
    value
  when String
    normalized = value.strip
    return normalized.to_i if normalized.match?(/\A\d+\z/)

    mapped = CONTENT_FORMATS[normalized.downcase]
    return mapped if mapped

    raise ArgumentError, "Unknown content format: #{value}"
  else
    raise ArgumentError, "Unsupported content-format value: #{value.inspect}"
  end
end

#obs(value = true) ⇒ Object Also known as: observable

Parameters:

  • value (Boolean) (defaults to: true)

Returns:

  • (Object)


47
48
49
# File 'lib/takagi/core/attribute_set.rb', line 47

def obs(value = true)
  (:obs, value ? true : REMOVE)
end

#rt(*values) ⇒ Object

Parameters:

  • values (Object)

Returns:

  • (Object)


52
53
54
# File 'lib/takagi/core/attribute_set.rb', line 52

def rt(*values)
  assign_list(:rt, values)
end

#sz(value) ⇒ Object

Parameters:

  • value (Object)

Returns:

  • (Object)


39
40
41
# File 'lib/takagi/core/attribute_set.rb', line 39

def sz(value)
  (:sz, value.to_i)
end

#title(value) ⇒ Object

Parameters:

  • value (Object)

Returns:

  • (Object)


43
44
45
# File 'lib/takagi/core/attribute_set.rb', line 43

def title(value)
  (:title, value.to_s)
end