Class: Bismas::Schema

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/bismas/schema.rb

Constant Summary collapse

FIELD_RE =
%r{\Afeld\s+=\s+(\d+)}i
CATEGORY_RE =
lambda { |category_length|
%r{\A(.{#{category_length}})"(.+?)"} }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



55
56
57
# File 'lib/bismas/schema.rb', line 55

def initialize
  @title, @name, @category_length, @categories = nil, nil, nil, {}
end

Instance Attribute Details

#category_lengthObject

Returns the value of attribute category_length.



59
60
61
# File 'lib/bismas/schema.rb', line 59

def category_length
  @category_length
end

#nameObject

Returns the value of attribute name.



59
60
61
# File 'lib/bismas/schema.rb', line 59

def name
  @name
end

#titleObject

Returns the value of attribute title.



59
60
61
# File 'lib/bismas/schema.rb', line 59

def title
  @title
end

Class Method Details

.parse(*args) ⇒ Object



44
45
46
# File 'lib/bismas/schema.rb', line 44

def parse(*args)
  new.parse(*args)
end

.parse_file(file, options = {}) ⇒ Object



48
49
50
51
# File 'lib/bismas/schema.rb', line 48

def parse_file(file, options = {})
  Bismas.amend_encoding(options)
  File.open(file, 'rb', options, &method(:parse))
end

Instance Method Details

#parse(io) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bismas/schema.rb', line 65

def parse(io)
  category_re = nil

  io.each { |line|
    case line
      when category_re
        self[$1.strip] = $2.strip
      when FIELD_RE
        category_re = CATEGORY_RE[
          self.category_length = $1.to_i]
      when String
        title ? name ? nil :
          self.name  = line.strip :
          self.title = line.strip
    end
  }

  self
end