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) ⇒ List
constructor
A new instance of List.
- #inspect ⇒ Object
- #move_item(num, direction) ⇒ Object
- #rename(str) ⇒ Object
- #to_json(options = nil) ⇒ Object
Constructor Details
#initialize(arg) ⇒ 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) 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 53 |
# File 'lib/list_tool/list.rb', line 50 def add_item text @items << Item.new(text) @items[-1] end |
#change_item(index, new_text) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/list_tool/list.rb', line 59 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
30 31 32 |
# File 'lib/list_tool/list.rb', line 30 def clear! @items = [] end |
#delete_item(num) ⇒ Object
55 56 57 |
# File 'lib/list_tool/list.rb', line 55 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 |
#inspect ⇒ Object
26 27 28 |
# File 'lib/list_tool/list.rb', line 26 def inspect "#<#{self.class}:0x#{self.__id__.to_s(16)}, name: #{name}, items: [#{items.map(&:text).join(', ')}]>" end |
#move_item(num, direction) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/list_tool/list.rb', line 66 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
34 35 36 37 38 39 |
# File 'lib/list_tool/list.rb', line 34 def rename str raise ArgumentError, 'string expected' unless str.is_a?(String) old_name = @name @name = str old_name end |
#to_json(options = nil) ⇒ Object
41 42 43 44 |
# File 'lib/list_tool/list.rb', line 41 def to_json =nil hash = {name: @name, items: @items} hash.to_json end |