Class: EsriShapefile::ByteModel::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/esri_shapefile/byte_model/field.rb

Constant Summary collapse

TYPES =
{
  integer: { bytesize: 4,  unpack: { big: 'l>', little: 'l<' } },
  double:  { bytesize: 8,  unpack: { big: 'G',  little: 'E'  } },
  point:   { bytesize: 16, unpack: { big: 'G',  little: 'E', multiplier: 2 } },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, position:, type:, byte_order:, number: nil) ⇒ Field

Returns a new instance of Field.



12
13
14
15
16
17
18
# File 'lib/esri_shapefile/byte_model/field.rb', line 12

def initialize(name, position:, type:, byte_order:, number: nil)
  @name = name
  @position = position
  @type = type
  @number = number
  @byte_order = byte_order
end

Instance Attribute Details

#byte_orderObject (readonly)

Returns the value of attribute byte_order.



4
5
6
# File 'lib/esri_shapefile/byte_model/field.rb', line 4

def byte_order
  @byte_order
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/esri_shapefile/byte_model/field.rb', line 4

def name
  @name
end

#numberObject (readonly)

Returns the value of attribute number.



4
5
6
# File 'lib/esri_shapefile/byte_model/field.rb', line 4

def number
  @number
end

#positionObject (readonly)

Returns the value of attribute position.



4
5
6
# File 'lib/esri_shapefile/byte_model/field.rb', line 4

def position
  @position
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/esri_shapefile/byte_model/field.rb', line 4

def type
  @type
end

Instance Method Details

#bytesize(values = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/esri_shapefile/byte_model/field.rb', line 36

def bytesize(values = {})
  size = TYPES[@type][:bytesize]

  if number_depends_on_field?
    multiplier = TYPES[@type][:unpack][:multiplier] || 1
    number_of_consecutive_values = number_depends_on_field? ? values[number] : number
    number_of_consecutive_values * multiplier * size
  else
    size
  end
end

#list?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/esri_shapefile/byte_model/field.rb', line 20

def list?
  !@number.nil?
end

#number_depends_on_field?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/esri_shapefile/byte_model/field.rb', line 28

def number_depends_on_field?
  @number.is_a?(Symbol)
end

#unpack_format(values = {}) ⇒ Object



48
49
50
51
52
53
# File 'lib/esri_shapefile/byte_model/field.rb', line 48

def unpack_format(values = {})
  number_of_consecutive_values = number_depends_on_field? ? values[number] : number
  multiplier = TYPES[@type][:unpack][:multiplier] || 1
  unpack_format = TYPES[@type][:unpack][@byte_order]
  "#{unpack_format}#{number_of_consecutive_values * multiplier}"
end

#unused?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/esri_shapefile/byte_model/field.rb', line 32

def unused?
  name == :unused
end