Class: Coopy::Ndjson

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/coopy/ndjson.rb

Instance Method Summary collapse

Constructor Details

#initialize(tab) ⇒ Ndjson

Returns a new instance of Ndjson.



7
8
9
10
11
# File 'lib/lib/coopy/ndjson.rb', line 7

def initialize(tab)
  @tab = tab
  @view = tab.get_cell_view
  @header_row = 0
end

Instance Method Details

#add_header_row(r) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/lib/coopy/ndjson.rb', line 101

def add_header_row(r)
  names = ::Rb::RubyIterator.new(@columns.keys)
  _it = names
  while(_it.has_next) do
    n = _it._next
    @tab.set_cell(@columns[n],r,@view.to_datum(n))
  end
end

#add_row(r, txt) ⇒ Object



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/lib/coopy/ndjson.rb', line 64

def add_row(r,txt)
  json = ::Haxe::Format::JsonParser.new(txt).parse_rec
  @columns = {} if @columns == nil
  w = @tab.get_width
  h = @tab.get_height
  resize = false
  begin
    _g = 0
    _g1 = Reflect.fields(json)
    while(_g < _g1.length) 
      name = _g1[_g]
      _g+=1
      if !@columns.include?(name) 
        @columns[name] = w
        w+=1
        resize = true
      end
    end
  end
  if r >= h 
    h = r + 1
    resize = true
  end
  @tab.resize(w,h) if resize
  begin
    _g2 = 0
    _g11 = Reflect.fields(json)
    while(_g2 < _g11.length) 
      name1 = _g11[_g2]
      _g2+=1
      v = Reflect.field(json,name1)
      c = @columns[name1]
      @tab.set_cell(c,r,v)
    end
  end
end

#parse(txt) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/lib/coopy/ndjson.rb', line 110

def parse(txt)
  @columns = nil
  rows = txt.split("\n")
  h = rows.length
  if h == 0 
    @tab.clear
    return
  end
  h-=1 if rows[h - 1] == ""
  begin
    _g = 0
    while(_g < h) 
      i = _g
      _g+=1
      at = h - i - 1
      self.add_row(at + 1,rows[at])
    end
  end
  self.add_header_row(0)
end

#renderObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lib/coopy/ndjson.rb', line 44

def render 
  txt = ""
  offset = 0
  return txt if @tab.get_height == 0
  return txt if @tab.get_width == 0
  offset = 1 if @tab.get_cell(0,0) == "@:@"
  @header_row = offset
  begin
    _g1 = @header_row + 1
    _g = @tab.get_height
    while(_g1 < _g) 
      r = _g1
      _g1+=1
      txt += self.render_row(r)
      txt += "\n"
    end
  end
  txt
end

#render_row(r) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lib/coopy/ndjson.rb', line 22

def render_row(r)
  row = {}
  begin
    _g1 = 0
    _g = @tab.get_width
    while(_g1 < _g) 
      c = _g1
      _g1+=1
      key = @view.to_s(@tab.get_cell(c,@header_row))
      key = "@:@" if c == 0 && @header_row == 1
      begin
        value = @tab.get_cell(c,r)
        begin
          value1 = value
          row[key] = value1
        end
      end
    end
  end
  ::Haxe::Format::JsonPrinter._print(row,nil,nil)
end