Class: Endymion::FieldSpec

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FieldSpec.



8
9
10
11
12
13
14
15
16
# File 'lib/endymion/field_spec.rb', line 8

def initialize(name, opts={})
  @name = name
  @default = opts[:default]
  @type = opts[:type]
  @packer = opts[:packer]
  @unpacker = opts[:unpacker]
  @db_name = opts[:db_name] ? Format.format_field(opts[:db_name]) : name
  @api = opts[:api]
end

Instance Attribute Details

#db_nameObject (readonly)

Returns the value of attribute db_name.



6
7
8
# File 'lib/endymion/field_spec.rb', line 6

def db_name
  @db_name
end

#defaultObject (readonly)

Returns the value of attribute default.



6
7
8
# File 'lib/endymion/field_spec.rb', line 6

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/endymion/field_spec.rb', line 6

def name
  @name
end

Instance Method Details

#pack(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/endymion/field_spec.rb', line 18

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

#unpack(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/endymion/field_spec.rb', line 29

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