Class: Laborantin::Table
- Inherits:
-
Object
- Object
- Laborantin::Table
- Defined in:
- lib/laborantin/core/table.rb
Defined Under Namespace
Classes: Filler
Instance Attribute Summary collapse
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#header ⇒ Object
Returns the value of attribute header.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#separator ⇒ Object
Returns the value of attribute separator.
-
#struct ⇒ Object
readonly
Returns the value of attribute struct.
Instance Method Summary collapse
- #dump(obj, check = true) ⇒ Object
- #fields ⇒ Object
- #fill(&blk) ⇒ Object
-
#initialize(name, struct = nil, path = nil) {|_self| ... } ⇒ Table
constructor
A new instance of Table.
- #read(hash = {}) ⇒ Object
Constructor Details
#initialize(name, struct = nil, path = nil) {|_self| ... } ⇒ Table
Returns a new instance of Table.
5 6 7 8 9 10 11 12 13 |
# File 'lib/laborantin/core/table.rb', line 5 def initialize(name, struct=nil, path=nil) @name = name @struct = struct @path = path @separator = ' ' @comment = '#' @heaer = nil yield self if block_given? end |
Instance Attribute Details
#comment ⇒ Object
Returns the value of attribute comment.
4 5 6 |
# File 'lib/laborantin/core/table.rb', line 4 def comment @comment end |
#header ⇒ Object
Returns the value of attribute header.
4 5 6 |
# File 'lib/laborantin/core/table.rb', line 4 def header @header end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/laborantin/core/table.rb', line 3 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/laborantin/core/table.rb', line 3 def path @path end |
#separator ⇒ Object
Returns the value of attribute separator.
4 5 6 |
# File 'lib/laborantin/core/table.rb', line 4 def separator @separator end |
#struct ⇒ Object (readonly)
Returns the value of attribute struct.
3 4 5 |
# File 'lib/laborantin/core/table.rb', line 3 def struct @struct end |
Instance Method Details
#dump(obj, check = true) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/laborantin/core/table.rb', line 37 def dump(obj, check=true) strings = obj.to_a.map(&:to_s) if check if strings.find{|s| s.include?(separator)} raise ArgumentError, "cannot unambiguously dump a value with the separator" end end line = strings.join(separator) if check if line.start_with?(comment) and (not comment.empty?) raise ArgumentError, "line starting with comment\n#{line}" end expected = struct.members.size got = line.split(separator).size if got != expected raise ArgumentError, "ambiguous line: #{got} fields instead of #{expected} in \n#{line}" end end line end |
#fields ⇒ Object
15 16 17 |
# File 'lib/laborantin/core/table.rb', line 15 def fields struct.members.map(&:to_s) end |
#fill(&blk) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/laborantin/core/table.rb', line 27 def fill(&blk) File.open(path, 'w') do |f| f.puts header if struct filler = Filler.new do |val| f.puts dump(val) end blk.call(filler) end end |
#read(hash = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/laborantin/core/table.rb', line 58 def read(hash={}) if block_given? File.open(path) do |f| f.each_line do |l| next if l.start_with?(comment) strings = l.chomp.split(separator) pairs = [struct.members, strings].transpose atoms = pairs.map do |sym, val| int = hash[sym] if int val.send(int) else val end end yield struct.new(*atoms) end end else Enumerator.new(self, :read, hash) end end |