Class: Gdt::AbstractField

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

Direct Known Subclasses

GdtFields

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gdt_hash) ⇒ AbstractField

Returns a new instance of AbstractField.



74
75
76
77
78
# File 'lib/gdt/field_handling.rb', line 74

def initialize(gdt_hash)
  gdt_hash.each { |gdt_id, value|
    set_field(gdt_id, value)
  }
end

Class Method Details

.field(gdt_id, name, description, length, type = :alnum, rules = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gdt/field_handling.rb', line 44

def self.field(gdt_id, name, description, length, type = :alnum, rules = nil) 
  field = (fields[gdt_id] ||= GdtField.new)

  field.name = name
  field.type = type
  field.length = length

  define_method name do
    values[name]
  end
end

.fieldsObject



59
60
61
# File 'lib/gdt/field_handling.rb', line 59

def self.fields
  @gdt_fields ||= Hash.new
end

.lookup(field_id) ⇒ Object



56
57
58
# File 'lib/gdt/field_handling.rb', line 56

def self.lookup(field_id)
  @gdt_fields[field_id].name
end

Instance Method Details

#set_field(gdt_id, value) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
# File 'lib/gdt/field_handling.rb', line 67

def set_field(gdt_id, value)
  field = self.class.fields[gdt_id]
  raise ArgumentError, "undefined field '#{gdt_id}'" unless field

  values[field.name] = field.verify_and_convert(value)
end

#valuesObject



63
64
65
# File 'lib/gdt/field_handling.rb', line 63

def values
  @values ||= Hash.new
end