Class: ListTool::List
- Inherits:
-
Object
- Object
- ListTool::List
- Includes:
- Enumerable
- Defined in:
- lib/list_tool/list.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #add_item(text) ⇒ Object
- #change_item(index, new_text) ⇒ Object
- #clear! ⇒ Object
- #delete_item(num) ⇒ Object
- #each ⇒ Object
-
#initialize(arg = nil) ⇒ List
constructor
A new instance of List.
- #move_item(num, direction) ⇒ Object
- #rename(str) ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(arg = nil) ⇒ List
Returns a new instance of List.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/list_tool/list.rb', line 8 def initialize(arg=nil) if arg.respond_to?(:to_str) @name = arg @items = [] elsif arg.is_a?(Hash) prepare_data(arg) @name = arg['name'] @items = [] arg['items'].each do |item| add_item(item) end else raise(ArgumentError, "argument expected to be Hash or String, #{arg.class} given") end end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
6 7 8 |
# File 'lib/list_tool/list.rb', line 6 def items @items end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/list_tool/list.rb', line 6 def name @name end |
Instance Method Details
#add_item(text) ⇒ Object
50 51 52 |
# File 'lib/list_tool/list.rb', line 50 def add_item text @items << Item.new(text) end |
#change_item(index, new_text) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/list_tool/list.rb', line 58 def change_item index, new_text raise ArgumentError, 'index is not an integer' unless index.respond_to?(:to_int) raise ArgumentError, 'new text is not a string' unless new_text.respond_to?(:to_str) return nil if @items[index].nil? @items[index].text = new_text end |
#clear! ⇒ Object
26 27 28 |
# File 'lib/list_tool/list.rb', line 26 def clear! @items = [] end |
#delete_item(num) ⇒ Object
54 55 56 |
# File 'lib/list_tool/list.rb', line 54 def delete_item num @items.delete_at(num) end |
#each ⇒ Object
46 47 48 |
# File 'lib/list_tool/list.rb', line 46 def each @items.each { |item| yield(item) } end |
#move_item(num, direction) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/list_tool/list.rb', line 65 def move_item num, direction case direction when :up return unless (1...@items.length).include?(num) @items[num], @items[num-1] = @items[num-1], @items[num] when :down return unless (0...(@items.length-1)).include?(num) @items[num], @items[num+1] = @items[num+1], @items[num] end end |
#rename(str) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/list_tool/list.rb', line 30 def rename str raise ArgumentError, 'string expected' unless str.is_a?(String) old_name = @name @name = str old_name end |
#to_json ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/list_tool/list.rb', line 37 def to_json json = "{\"name\":\"#{@name}\",\"items\":[" @items.each do |item| json += item.to_json json += ',' unless item == @items.last end json += ']}' end |