Class: Laborantin::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/laborantin/core/table.rb

Defined Under Namespace

Classes: Filler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, struct = nil, path = nil) {|_self| ... } ⇒ Table

Returns a new instance of Table.

Yields:

  • (_self)

Yield Parameters:



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

#commentObject

Returns the value of attribute comment.



4
5
6
# File 'lib/laborantin/core/table.rb', line 4

def comment
  @comment
end

#headerObject

Returns the value of attribute header.



4
5
6
# File 'lib/laborantin/core/table.rb', line 4

def header
  @header
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/laborantin/core/table.rb', line 3

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/laborantin/core/table.rb', line 3

def path
  @path
end

#separatorObject

Returns the value of attribute separator.



4
5
6
# File 'lib/laborantin/core/table.rb', line 4

def separator
  @separator
end

#structObject (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

#fieldsObject



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