-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstructs.rb
56 lines (43 loc) · 1.36 KB
/
structs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
## SPDX-License-Identifier: public domain
class Todos < Contract
struct :Todo, text: String,
completed: Bool
# An array of 'Todo' structs
storage todos: array( Todo )
sig [String]
def create( text: )
## 3 ways to initialize a struct
## - calling it like a function
@todos.push( Todo.new( text, false ))
## key value mapping
# @todos.push( Todo.new( text: text, completed: false ))
## initialize an empty struct and then update it
todo = Todo.new
todo.text = text
# todo.completed initialized to false
@todos.push( todo )
end
## Rubysol automatically created a getter for 'todos' so
## you don't actually need this function.
# sig :get, [UInt], :view, returns: [String, Bool]
# def get( index: )
# todo = @todos[ index ]
# [todo.text, todo.completed]
# end
sig [UInt], :view, returns: Todo
def get( index: )
@todos[ index ]
end
# update text
sig [UInt, String]
def updateText( index:, text: )
todo = @todos[ index]
todo.text = text
end
# update completed
sig [UInt]
def toggleCompleted( index: )
todo = @todos[ index ]
todo.completed = !todo.completed
end
end