Skip to content

Commit

Permalink
Todo list; move components and models into their own modules and folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Scott Lewis committed Jul 14, 2015
1 parent ae0a8ca commit 6d46e3a
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 228 deletions.
8 changes: 3 additions & 5 deletions .rubocop.yml
Expand Up @@ -2,11 +2,6 @@ Lint/UnusedMethodArgument:
Exclude:
- lib/cura/attributes/has_initialize.rb

Metrics/CyclomaticComplexity:
Exclude:
- lib/cura/attributes/has_side_attributes.rb
- lib/cura/event/dispatcher.rb

Style/CaseIndentation:
IndentOneStep: true

Expand All @@ -24,6 +19,9 @@ Style/SpaceAroundEqualsInParameterDefault:

# -=-=-=-=-=- TODO -=-=-=-=-=- #

Metrics/CyclomaticComplexity:
Enabled: false

# Offense count: 18
Metrics/AbcSize:
Max: 135
Expand Down
14 changes: 7 additions & 7 deletions examples/todo_list/lib/todo_list/application.rb
@@ -1,8 +1,8 @@
require "cura"

require "todo_list/header"
require "todo_list/lists"
require "todo_list/list_items"
require "todo_list/component/header"
require "todo_list/component/lists"
require "todo_list/component/list_items"

module TodoList

Expand Down Expand Up @@ -31,17 +31,17 @@ def initialize(attributes={})
main_pack = Cura::Component::Pack.new(width: window.width, height: window.height, fill: true)
window.add_child(main_pack)

header = Header.new
header = Component::Header.new
main_pack.add_child(header)

middle_pack = Cura::Component::Pack.new(height: window.height - 1, orientation: :horizontal, fill: true)
main_pack.add_child(middle_pack)

sidebar = Lists.new(width: 30, padding: 1)
sidebar = Component::Lists.new(width: 30, padding: 1)
middle_pack.add_child(sidebar)

@list_items = ListItems.new(width: 100, padding: 1)
middle_pack.add_child(@list_items, expand: true, fill: true)
@list_items = Component::ListItems.new(width: 100, padding: 1)
middle_pack.add_child(@list_items) # , expand: true, fill: true) # TODO

#-

Expand Down
26 changes: 26 additions & 0 deletions examples/todo_list/lib/todo_list/component/header.rb
@@ -0,0 +1,26 @@
module TodoList
module Component

class Header < Cura::Component::Pack

attr_reader :create_list_textbox

def initialize(attributes={})
attributes = { orientation: :horizontal }.merge(attributes)

super(attributes)

add_children(
Cura::Component::Label.new(text: "Todo", bold: true, margin: { right: 3 }),
Cura::Component::Label.new(text: "^-C to exit", margin: { right: 3 }),
Cura::Component::Label.new(text: "^-E to edit item", margin: { right: 3 }),
Cura::Component::Label.new(text: "^-D to delete item", margin: { right: 3 }),
Cura::Component::Label.new(text: "^-F to move focus forwards", margin: { right: 3 }),
Cura::Component::Label.new(text: "^-B to move focus backwards", margin: { right: 3 })
)
end

end

end
end
5 changes: 5 additions & 0 deletions examples/todo_list/lib/todo_list/component/list_item.rb
@@ -0,0 +1,5 @@
module TodoList
module Component

end
end
95 changes: 95 additions & 0 deletions examples/todo_list/lib/todo_list/component/list_items.rb
@@ -0,0 +1,95 @@
module TodoList
module Component

class ListItems < Cura::Component::Pack

attr_reader :create_list_item_textbox
attr_reader :listbox
attr_reader :list

def initialize(attributes={})
attributes = { fill: true, padding: { top: 1, bottom: 1 } }.merge(attributes)

super(attributes)

create_form_pack = Cura::Component::Pack.new(orientation: :horizontal)
add_child(create_form_pack)

@create_list_item_textbox = Cura::Component::Textbox.new(width: width - 21, padding: { left: 1, right: 1 }, margin: { right: 1 })
@create_list_item_textbox.on_event(:key_down, self) { |event, model_list| model_list.create_list_item if event.name == :enter }
create_form_pack.add_child(@create_list_item_textbox)

@create_list_item_button = Cura::Component::Button.new(text: "Create List Item", padding: { left: 1, right: 1 })
@create_list_item_button.on_event(:click, self) { |_event| model_list.create_list_item }
create_form_pack.add_child(@create_list_item_button)

@listbox_header_label = Cura::Component::Label.new(text: " " * width, bold: true, underline: true, margin: { top: 1 })
add_child(@listbox_header_label)

@listbox = Cura::Component::Listbox.new(width: @width)

@listbox.on_event(:key_down, self) do |event, model_list|
if event.target == self && event.control? && event.name == :D && !selected_object.nil?
selected_object.destroy

previous_selected_index = @selected_index
model_list.fill_listbox
self.selected_index = [previous_selected_index, count - 1].min
end

if event.target == self && event.control? && event.name == :E
selected_child.focusable = true
selected_child.focus
end
end

add_child(@listbox)


end

def create_list_item
text = @create_list_item_textbox.text

Model::ListItem.create(list_id: @list.id, text: text)

fill_listbox

@create_list_item_textbox.clear
@create_list_item_textbox.focus
end

def fill_listbox
@listbox.delete_children

return nil if @list.nil?

@list.list_items.each do |list_item|
list_item_textbox = Cura::Component::Textbox.new(text: list_item.text, width: @listbox.width, background: :inherit, foreground: Cura::Color.white, focusable: false)
list_item_textbox.on_event(:key_down, @listbox) do |event, listbox|
if event.name == :enter
list_item.text = text

list_item.save

self.focusable = false
listbox.focus
end
end

@listbox.add_child(list_item_textbox, list_item)
end
end

def list=(list)
@list = list
@listbox_header_label.text = @list.name
@listbox_header_label.text << " " * (width - @list.name.length) unless @list.name.length >= width

fill_listbox
end

end

end
end
85 changes: 85 additions & 0 deletions examples/todo_list/lib/todo_list/component/lists.rb
@@ -0,0 +1,85 @@
module TodoList
module Component

class Lists < Cura::Component::Pack

attr_reader :create_list_textbox
attr_reader :listbox


def initialize(attributes={})
attributes = { fill: true, padding: { top: 1, bottom: 1 } }.merge(attributes)

super(attributes)

create_form_pack = Cura::Component::Pack.new(orientation: :horizontal)
add_child(create_form_pack)

@create_list_textbox = Cura::Component::Textbox.new(width: width - 16, padding: { left: 1, right: 1 }, margin: { right: 1 })
@create_list_textbox.on_event(:key_down, self) { |event, model_list| model_list.create_list if event.name == :enter }
create_form_pack.add_child(@create_list_textbox)

@create_list_button = Cura::Component::Button.new(text: "Create List", padding: { left: 1, right: 1 })
@create_list_button.on_event(:click, self) { |_event, model_list| model_list.create_list }
create_form_pack.add_child(@create_list_button)

@listbox_header_label = Cura::Component::Label.new(text: "Lists" + " " * (width - 5), bold: true, underline: true, margin: { top: 1 })
add_child(@listbox_header_label)

@listbox = Cura::Component::Listbox.new(width: @width)
@listbox.on_event(:selected) { |_event| application.list_items.list = selected_object unless selected_object.nil? }
@listbox.on_event(:key_down, self) do |event, model_list|
if event.target == self && event.control? && event.name == :D && !selected_object.nil?
selected_object.destroy

previous_selected_index = @selected_index
model_list.fill_listbox
self.selected_index = [previous_selected_index, count - 1].min
end

if event.target == self && event.control? && event.name == :E
selected_child.focusable = true
selected_child.focus
end
end

add_child(@listbox)

fill_listbox
end

def create_list
name = @create_list_textbox.text

Model::List.create(name: name)

fill_listbox

@create_list_textbox.clear
@create_list_textbox.focus
end

def fill_listbox
@listbox.delete_children

Model::List.all.each do |list|
list_textbox = Cura::Component::Textbox.new(text: list.name, width: @listbox.width, background: :inherit, foreground: Cura::Color.white, focusable: false)
list_textbox.on_event(:key_down, @listbox) do |event, listbox|
if event.name == :enter
list.name = text

list.save

self.focusable = false
listbox.focus
end
end

@listbox.add_child(list_textbox, list)
end
end

end

end
end
7 changes: 5 additions & 2 deletions examples/todo_list/lib/todo_list/database.rb
Expand Up @@ -4,6 +4,7 @@

module TodoList
module Database

class << self

def path
Expand All @@ -16,8 +17,8 @@ def setup

create_tables unless db_existed

require "todo_list/list"
require "todo_list/list_item"
require "todo_list/model/list"
require "todo_list/model/list_item"
end

protected
Expand All @@ -38,10 +39,12 @@ def create_list_items_table
@connection.create_table(:list_items) do
primary_key :id
String :text
TrueClass :completed, default: false
foreign_key :list_id, :lists
end unless @connection.table_exists?(:list_items)
end

end

end
end
24 changes: 0 additions & 24 deletions examples/todo_list/lib/todo_list/header.rb

This file was deleted.

7 changes: 0 additions & 7 deletions examples/todo_list/lib/todo_list/list.rb

This file was deleted.

6 changes: 0 additions & 6 deletions examples/todo_list/lib/todo_list/list_item.rb

This file was deleted.

0 comments on commit 6d46e3a

Please sign in to comment.