Class: Checklist::List

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/checklist/list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, &block) ⇒ List

Returns a new instance of List.



20
21
22
23
# File 'lib/checklist/list.rb', line 20

def initialize(klass, &block)
  @klass, @items = klass, []
  update(&block)
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



18
19
20
# File 'lib/checklist/list.rb', line 18

def context
  @context
end

#itemsObject (readonly)

Returns the value of attribute items.



18
19
20
# File 'lib/checklist/list.rb', line 18

def items
  @items
end

Instance Method Details

#each_checked(options = {}, &block) ⇒ Object

Example

each_checked do |explain, checked|

puts "#{explain} = #{checked}"

end



46
47
48
49
50
51
# File 'lib/checklist/list.rb', line 46

def each_checked(options = {}, &block)
  options = parse_options(options)
  get_items(options).each do |item|
    block.call(item.explain, item.checked?)
  end
end

#errorsObject



59
60
61
# File 'lib/checklist/list.rb', line 59

def errors
  items.select { |item| !item.checked? }
end

#filtered_itemsObject

items that should be checked



64
65
66
# File 'lib/checklist/list.rb', line 64

def filtered_items
  items.select { |item| item.keep? }
end

#map_checked(options = {}, &block) ⇒ Object



53
54
55
56
57
# File 'lib/checklist/list.rb', line 53

def map_checked(options = {}, &block)
  options = parse_options(options)
  block ||= lambda { |msg, checked| [msg, checked] }
  items.map {|item| block.call(item.explain, item.checked?) }
end

#update(&block) ⇒ Object



25
26
27
28
# File 'lib/checklist/list.rb', line 25

def update(&block)
  instance_eval(&block) if block_given?
  self
end

#valid?Boolean

return true if all items are validated

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/checklist/list.rb', line 31

def valid?
  items.each do |item|
    return false unless item.checked?
  end
  return true
end