Class: Athena::Record

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/athena/record.rb

Overview

#

A component of athena, the database file converter. #

#

Copyright © 2007-2009 University of Cologne, #

Albertus-Magnus-Platz,                              #
50932 Cologne, Germany                              #
                                                    #

Authors: #

Jens Wille <[email protected]>                                    #
                                                                        #

athena is free software; you can redistribute it and/or modify it under the # terms of the GNU General Public License as published by the Free Software # Foundation; either version 3 of the License, or (at your option) any later # version. #

#

athena is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. #

#

You should have received a copy of the GNU General Public License along # with athena. If not, see <www.gnu.org/licenses/>. #

#

++

Defined Under Namespace

Classes: NoRecordError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#verbose

Constructor Details

#initialize(block, id = object_id.abs) ⇒ Record

Returns a new instance of Record.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/athena/record.rb', line 53

def initialize(block, id = object_id.abs)
  @struct = {}
  @block  = block
  @id     = id

  add_record unless block

  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



51
52
53
# File 'lib/athena/record.rb', line 51

def block
  @block
end

#idObject (readonly)

Returns the value of attribute id.



51
52
53
# File 'lib/athena/record.rb', line 51

def id
  @id
end

#structObject (readonly)

Returns the value of attribute struct.



51
52
53
# File 'lib/athena/record.rb', line 51

def struct
  @struct
end

Class Method Details

.[](field = nil, config = nil) ⇒ Object

Raises:



41
42
43
44
45
46
47
# File 'lib/athena/record.rb', line 41

def [](field = nil, config = nil)
  record = records.last
  raise NoRecordError unless record

  record.fill(field, config) if field && config
  record
end

.recordsObject



37
38
39
# File 'lib/athena/record.rb', line 37

def records
  @records
end

Instance Method Details

#closeObject



90
91
92
# File 'lib/athena/record.rb', line 90

def close
  block ? block[self] : self
end

#fill(field, config) ⇒ Object



69
70
71
# File 'lib/athena/record.rb', line 69

def fill(field, config)
  struct[field] ||= config.merge({ :values => Hash.new { |h, k| h[k] = [] } })
end

#to(format) ⇒ Object



94
95
96
# File 'lib/athena/record.rb', line 94

def to(format)
  Athena::Formats[:out, format].convert(self)
end

#update(element, data, field_config = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/athena/record.rb', line 73

def update(element, data, field_config = nil)
  if field_config
    field_config.each { |field, config|
      fill(field, config)
    }
  end

  struct.each_key { |field|
    verbose(:data) do
      value = data.strip
      spit "#{field.to_s.upcase}[#{element}] << #{value}" unless value.empty?
    end

    struct[field][:values][element] << data
  }
end