Class: Demos::Spreadsheet::App

Inherits:
Hokusai::Block show all
Defined in:
ui/examples/spreadsheet.rb

Instance Attribute Summary collapse

Attributes inherited from Hokusai::Block

#node, #provides, #publisher

Instance Method Summary collapse

Methods inherited from Hokusai::Block

#children, #children?, compile, computed, computed!, #draw, #draw_with, #dump, #emit, inject, inject!, #method_missing, mount, #on_resize, provide, provides, #render, style, styles_get, template, template_from_file, template_get, #update, use, uses

Constructor Details

#initialize(**args) ⇒ App

Returns a new instance of App.



63
64
65
66
# File 'ui/examples/spreadsheet.rb', line 63

def initialize(**args)
  super
  @status = :unchanged
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hokusai::Block

Instance Attribute Details

#spreadsheetObject (readonly)

Returns the value of attribute spreadsheet.



61
62
63
# File 'ui/examples/spreadsheet.rb', line 61

def spreadsheet
  @spreadsheet
end

Instance Method Details

#handle_keypress(event) ⇒ Object



72
73
74
75
76
77
78
79
# File 'ui/examples/spreadsheet.rb', line 72

def handle_keypress(event)
  case event
  when proc(&:ctrl), proc(&:super)
    if event.char == "s"
      save
    end
  end
end

#has_spreadsheetObject



99
100
101
# File 'ui/examples/spreadsheet.rb', line 99

def has_spreadsheet
  !@spreadsheet.nil?
end

#on_mountedObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'ui/examples/spreadsheet.rb', line 103

def on_mounted
  if File.exist?("somecsv.csv")
    @spreadsheet = ::CSV.read("somecsv.csv", headers: false)
  else
    @spreadsheet = ::CSV.parse(<<~EOF, headers: false)
    Name,Department,Salary
    Bob,Engineering,1000
    Jane,Sales,2000
    John,Management,5000
    EOF
  end
end

#saveObject



81
82
83
84
85
86
# File 'ui/examples/spreadsheet.rb', line 81

def save
  File.open("somecsv.csv", "wb") do |io|
    io << spreadsheet.to_a.map(&:to_csv).join("")
  end
  @status = :saved
end

#set_statusObject



68
69
70
# File 'ui/examples/spreadsheet.rb', line 68

def set_status
  @status = :changed
end

#statusObject



88
89
90
91
92
93
94
95
96
97
# File 'ui/examples/spreadsheet.rb', line 88

def status
  case @status
  when :changed
    "(Modified)"
  when :saved
    "(Saved)"
  else
    "(Not Modified)"
  end
end