Class: Checklist::List
- Inherits:
-
Object
- Object
- Checklist::List
- Extended by:
- Forwardable
- Defined in:
- lib/checklist/list.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
-
#each_checked(options = {}, &block) ⇒ Object
Example each_checked do |explain, checked| puts “#explain = #checked” end.
- #errors ⇒ Object
-
#filtered_items ⇒ Object
items that should be checked.
-
#initialize(klass, &block) ⇒ List
constructor
A new instance of List.
- #map_checked(options = {}, &block) ⇒ Object
- #update(&block) ⇒ Object
-
#valid? ⇒ Boolean
return true if all items are validated.
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
#context ⇒ Object
Returns the value of attribute context.
18 19 20 |
# File 'lib/checklist/list.rb', line 18 def context @context end |
#items ⇒ Object (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( = {}, &block) = () get_items().each do |item| block.call(item.explain, item.checked?) end end |
#errors ⇒ Object
59 60 61 |
# File 'lib/checklist/list.rb', line 59 def errors items.select { |item| !item.checked? } end |
#filtered_items ⇒ Object
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( = {}, &block) = () 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
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 |