Sha256: d75e53348f5919c6cb28f6c54fb595bbc4f5b255b9ee8de8416ecee6ec38b3e4

Contents?: true

Size: 1.93 KB

Versions: 5

Compression:

Stored size: 1.93 KB

Contents

module DBF
  # An instance of DBF::Record represents a row in the DBF file 
  class Record
    # Initialize a new DBF::Record
    # 
    # @param [DBF::Table] table
    def initialize(data, columns, version, memo)
      @data, @columns, @version, @memo = StringIO.new(data), columns, version, memo
      define_accessors
    end
    
    # Equality
    #
    # @param [DBF::Record] other
    # @return [Boolean]
    def ==(other)
      other.respond_to?(:attributes) && other.attributes == attributes
    end
    
    # Maps a row to an array of values
    # 
    # @return [Array]
    def to_a
      @columns.map { |column| attributes[Util.underscore(column.name)] }
    end
    
    # Do all search parameters match?
    #
    # @param [Hash] options
    # @return [Boolean]
    def match?(options)
      options.all? {|key, value| attributes[Util.underscore(key.to_s)] == value}
    end
    
    def attributes
      return @attributes if @attributes
      
      @attributes = Attributes.new
      @columns.each do |column|
        @attributes[column.name] = init_attribute(column)
      end
      @attributes
    end
    
    private
    
    def define_accessors #nodoc
      @columns.each do |column|
        unless self.class.method_defined? column.underscored_name
          self.class.class_eval <<-END
            def #{column.underscored_name}
              @#{column.underscored_name} ||= attributes['#{column.underscored_name}']
            end
          END
        end
      end
    end
    
    def init_attribute(column) #nodoc
      column.memo? ? @memo.get(get_memo_start_block(column)) : column.type_cast(unpack_data(column))
    end
   
    def get_memo_start_block(column) #nodoc
      if %w(30 31).include?(@version)
        @data.read(column.length).unpack('V').first
      else
        unpack_data(column).to_i
      end
    end

    def unpack_data(column) #nodoc
      @data.read(column.length).unpack("a#{column.length}").first
    end
    
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dbf-1.6.5 lib/dbf/record.rb
dbf-1.6.3 lib/dbf/record.rb
dbf-1.6.2 lib/dbf/record.rb
dbf-1.6.1 lib/dbf/record.rb
dbf-1.6.0 lib/dbf/record.rb