Class: CsvHuman::Doc

Inherits:
Object
  • Object
show all
Includes:
DocHelper
Defined in:
lib/csvhuman/doc/schema.rb

Overview

tags and attributes (schema) reader / converter (txt to csv)

Constant Summary

Constants included from DocHelper

CsvHuman::DocHelper::ATTRIBUTE_LINE_RX, CsvHuman::DocHelper::HASHTAG_LINE_RX, CsvHuman::DocHelper::HEADING_LINE_RX, CsvHuman::DocHelper::SINCE_HXL_RX, CsvHuman::DocHelper::TYPE_RX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DocHelper

#match_attribute, #match_hashtag, #match_heading, #match_since_hxl, #match_type, #split_descr

Constructor Details

#initialize(str_or_readable) ⇒ Doc

Returns a new instance of Doc.



38
39
40
41
# File 'lib/csvhuman/doc/schema.rb', line 38

def initialize( str_or_readable )
  # note: must (only) support/respond_to read_line
  @input = str_or_readable
end

Class Method Details

.open(path, mode = nil, &block) ⇒ Object

rename path to filename or name - why? why not?



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/csvhuman/doc/schema.rb', line 17

def self.open( path, mode=nil, &block )   ## rename path to filename or name - why? why not?

   ## note: default mode (if nil/not passed in) to 'r:bom|utf-8'
   f  = File.open( path, mode ? mode : 'r:bom|utf-8' )
   doc = self.new( f )

   # handle blocks like Ruby's open(), not like the (old old) CSV library
   if block_given?
     begin
       block.call( doc )
     ensure
       f.close
     end
   else
     doc    ## note: caller responsible for closing (todo/fix: add close,closed? to doc!!!)
   end
end

.read_attributes(path) ⇒ Object



9
10
11
# File 'lib/csvhuman/doc/schema.rb', line 9

def self.read_attributes( path )
  self.open( path ) { |doc| doc.parse_attributes }
end

.read_tags(path) ⇒ Object



13
14
15
# File 'lib/csvhuman/doc/schema.rb', line 13

def self.read_tags( path )
  self.open( path ) { |doc| doc.parse_tags }
end

Instance Method Details

#parse_attributesObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/csvhuman/doc/schema.rb', line 44

def parse_attributes

  attrib   = nil
  category = nil
  descr    = nil
  version  = nil
  tags     = []

  next_line   =  nil   ## e.g. set to :descr

  attribs = []

  @input.each_line do |line|
    line = line.chomp( '' )

    line = line.strip   ## remove leading and trailing spaces


    next if line.empty? || line.start_with?( '%' )   ## skip blank lines and comment lines

    if next_line == :descr
      ## auto-capture next line (if descr reset to nil)
      descr, version = split_descr( line )
      puts "descr >#{descr}<, version >#{version}<"

      next_line = nil
    elsif (m=match_heading( line ))
      ## note: new category ends attribute definition
      if attrib
        attribs << [attrib, version, category, tags.join( ' ' ), descr]
        attrib = nil
      end

      category = "(#{m[:level2]}) #{m[:title]}"
    elsif (m=match_attribute( line ))
      if attrib
        attribs << [attrib, version, category, tags.join( ' ' ), descr]
      end

      attrib    = m[:name]
      tags      = []
      descr     = nil
      version   = nil

      next_line = :descr  ## reset descr to nil - will auto-capture next line
    elsif (m=match_hashtag( line ))
      tags << "##{m[:name]}"
    end
  end

  if attrib
    attribs << [attrib, version, category, tags.join( ' ' ), descr]
  end

  attribs
end

#parse_tagsObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/csvhuman/doc/schema.rb', line 103

def parse_tags

  tag     = nil
  type    = nil
  category = nil
  descr    = nil
  version  = nil
  attribs  = []

  next_line   =  nil   ## e.g. set to :descr


  tags = []

  @input.each_line do |line|
    line = line.chomp( '' )

    line = line.strip   ## remove leading and trailing spaces


    next if line.empty? || line.start_with?( '%' )   ## skip blank lines and comment lines

    if next_line == :descr
      ## auto-capture next line (if descr reset to nil)
      descr, version = split_descr( line )

      ## descr = "(2) People and households"   if descr == "(2) Surveys and assessments"

      puts "descr >#{descr}<, version >#{version}<"

      next_line = nil
    elsif (m=match_heading( line ))
      ## note: new category ends tag definition
      if tag
        tags << [tag, type, version, category, attribs.join( ' ' ), descr]
        tag = nil
      end

      category = "(#{m[:level2]}) #{m[:title]}"
    elsif (m=match_type( line ))
      type = m[:type]
    elsif (m=match_hashtag( line ))
      if tag
        tags << [tag, type, version, category, attribs.join( ' ' ), descr]
      end

      tag      = m[:name]
      attribs  = []
      type     = nil
      descr    = nil
      version  = nil

      next_line = :descr  ## reset descr to nil - will auto-capture next line
    elsif (m=match_attribute( line ))
      attribs << "+#{m[:name]}"
    end
  end

  if tag
    tags << [tag, type, version, category, attribs.join( ' ' ), descr]
  end

  tags
end