Class: Hyperion::FieldSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperion.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ FieldSpec

Returns a new instance of FieldSpec.



259
260
261
262
263
264
265
# File 'lib/hyperion.rb', line 259

def initialize(name, opts={})
  @name = name
  @default = opts[:default]
  @type = opts[:type]
  @packer = opts[:packer]
  @unpacker = opts[:unpacker]
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



257
258
259
# File 'lib/hyperion.rb', line 257

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



257
258
259
# File 'lib/hyperion.rb', line 257

def name
  @name
end

Instance Method Details

#pack(value) ⇒ Object



267
268
269
270
271
272
273
274
275
276
# File 'lib/hyperion.rb', line 267

def pack(value)
  if @packer && @packer.respond_to?(:call)
    @packer.call(value)
  elsif @type
    type_packer = Hyperion.packer_for(@type)
    type_packer ? type_packer.call(value) : value
  else
    value
  end
end

#unpack(value) ⇒ Object



278
279
280
281
282
283
284
285
286
287
# File 'lib/hyperion.rb', line 278

def unpack(value)
  if @unpacker && @unpacker.respond_to?(:call)
    @unpacker.call(value)
  elsif @type
    type_packer = Hyperion.unpacker_for(@type)
    type_packer ? type_packer.call(value) : value
  else
    value
  end
end