Class: Record::Definition

Inherits:
Object show all
Defined in:
lib/flat/record.rb

Overview

Definition

Defines the behavior of a Record.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, attributes = {}, line_number = -1) ⇒ Definition

Create a new Record from a Hash of attributes.



71
72
73
74
75
76
77
# File 'lib/flat/record.rb', line 71

def initialize parent, attributes = {}, line_number = -1
  @parent, @attributes, @line_number = parent, attributes, line_number

  @attributes = parent.fields.inject({}) do |map, field|
    map.update(field.name => attributes[field.name])
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, params = nil) ⇒ Object

Catches method calls and returns field values or raises an Error.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/flat/record.rb', line 82

def method_missing method, params = nil
  if method.to_s =~ /^(.*)=$/
    if attributes.has_key?($1.to_sym)
      @attributes.store($1.to_sym, params)
    else
      raise Errors::FlatFileError, "Unknown method: #{method}"
    end
  else
    if attributes.has_key?(method)
      @attributes.fetch(method)
    else
      raise Errors::FlatFileError, "Unknown method: #{method}"
    end
  end
end

Instance Attribute Details

#attributesObject (readonly)

:nodoc:



66
67
68
# File 'lib/flat/record.rb', line 66

def attributes
  @attributes
end

#line_numberObject (readonly)

:nodoc:



66
67
68
# File 'lib/flat/record.rb', line 66

def line_number
  @line_number
end

#parentObject (readonly)

:nodoc:



66
67
68
# File 'lib/flat/record.rb', line 66

def parent
  @parent
end