Class: Nl::Protocols::Raw::AttributeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/nl/protocols/raw.rb

Defined Under Namespace

Classes: Attribute

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ AttributeSet

Returns a new instance of AttributeSet.



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/nl/protocols/raw.rb', line 126

def initialize(attributes)
  attr_class = self.class::Attribute

  attributes.each do |attr|
    unless attr.kind_of?(attr_class)
      raise TypeError, "attribute must be an instance of #{attr_class}"
    end
  end

  @attributes = Array(attributes)
end

Class Method Details

.build_attributes(**params) ⇒ Object



200
201
202
203
204
205
206
# File 'lib/nl/protocols/raw.rb', line 200

def build_attributes(**params)
  attrs = params.map do |name, value|
    attr_class = self::BY_NAME[name] or raise "Unknown attribute #{name}"
    attr_class.new(value)
  end
  new(attrs)
end

.decode(decoder) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/nl/protocols/raw.rb', line 191

def decode(decoder)
  attrs = []
  while decoder.available?
    attr = decode1(decoder)
    attrs << attr
  end
  new(attrs.compact)
end

Instance Method Details

#<<(attr) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/nl/protocols/raw.rb', line 152

def <<(attr)
  attr_class = self.class::Attribute
  unless attr.kind_of?(attr_class)
    raise TypeError, "attribute must be an instance of #{attr_class}"
  end

  @attributes << attr
end

#[](type) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/nl/protocols/raw.rb', line 138

def [](type)
  case type
  when Symbol
    attr_class = self.class.by_name(type)
  when Integer
    attr_class = self.class.by_type(type)
  else
    raise TypeError, "attribute type must be a Symbol or an Integer"
  end

  # TODO: multi-attr
  @attributes.find { it.kind_of?(attr_class) } rescue binding.irb
end

#encode(encoder) ⇒ Object



170
171
172
173
174
# File 'lib/nl/protocols/raw.rb', line 170

def encode(encoder)
  @attributes.each do |attr|
    encode1(encoder, attr)
  end
end