checkthelist 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,9 +8,10 @@
8
8
 
9
9
  module Checklist
10
10
  class Item
11
- attr_reader :explain, :key, :list
11
+ attr_reader :explain, :key, :list, :options
12
12
 
13
- def initialize(explain, list = nil, &block)
13
+ def initialize(explain, list = nil, options = {}, &block)
14
+ @options = options
14
15
  @list = list
15
16
  @block = block
16
17
  @explain = explain
@@ -18,7 +19,44 @@ module Checklist
18
19
 
19
20
  def checked?
20
21
  raise Checklist::InstanceMissingError unless list.context
21
- list.context.instance_eval(&@block) == true
22
+ context.instance_eval(&@block) == true
23
+ end
24
+
25
+ # return true if the item should be checked
26
+ def keep?
27
+ keep = only_applies?
28
+ keep = except_applies? if keep.nil?
29
+ keep
30
+ end
31
+
32
+ private
33
+
34
+ def context
35
+ list.context
36
+ end
37
+
38
+ # based on options[:except], the method ...
39
+ # return false if item should not be checked
40
+ # return true if the item should be checked
41
+ # return nil if option was not supplied
42
+ def except_applies?
43
+ options[:except].nil? ? nil : !exec(options[:except])
44
+ end
45
+
46
+ # based on options[:only], the method ...
47
+ # return false if item should not be checked
48
+ # return true if the item should be checked
49
+ # return nil if option was not supplied
50
+ def only_applies?
51
+ options[:only].nil? ? nil : exec(options[:only])
52
+ end
53
+
54
+ def exec(sym_or_proc)
55
+ if sym_or_proc.is_a?(Symbol)
56
+ context.send(sym_or_proc)
57
+ elsif sym_or_proc.is_a?(Proc)
58
+ context.instance_eval(&sym_or_proc)
59
+ end
22
60
  end
23
61
 
24
62
  end
@@ -43,28 +43,44 @@ module Checklist
43
43
  # each_checked do |explain, checked|
44
44
  # puts "#{explain} = #{checked}"
45
45
  # end
46
- def each_checked(&block)
47
- items.each do |item|
46
+ def each_checked(options = {}, &block)
47
+ options = parse_options(options)
48
+ get_items(options).each do |item|
48
49
  block.call(item.explain, item.checked?)
49
50
  end
50
51
  end
51
52
 
52
- def map_checked(&block)
53
- if block_given?
54
- items.map {|item| block.call(item.explain, item.checked?) }
55
- else
56
- items.map {|item| [item.explain, item.checked?] }
57
- end
53
+ def map_checked(options = {}, &block)
54
+ options = parse_options(options)
55
+ block ||= lambda { |msg, checked| [msg, checked] }
56
+ items.map {|item| block.call(item.explain, item.checked?) }
58
57
  end
59
58
 
60
59
  def errors
61
60
  items.select { |item| !item.checked? }
62
61
  end
63
62
 
63
+ # items that should be checked
64
+ def filtered_items
65
+ items.select { |item| item.keep? }
66
+ end
67
+
64
68
  private
65
69
 
66
- def check(explain, &block)
67
- @items << Item.new(explain, self, &block)
70
+ def check(explain, item_options = {}, &block)
71
+ @items << Item.new(explain, self, item_options, &block)
72
+ end
73
+
74
+ def get_items(options)
75
+ options[:filtered] ? filtered_items : items
76
+ end
77
+
78
+ def default_options
79
+ { filtered: true }
80
+ end
81
+
82
+ def parse_options(options = {})
83
+ default_options.merge(options)
68
84
  end
69
85
 
70
86
  end
@@ -7,26 +7,7 @@
7
7
  # THE SOFTWARE.
8
8
 
9
9
  require_relative '../lib/checklist'
10
-
11
- class TestingChecklist
12
- include Checklist
13
-
14
- attr_accessor :world
15
-
16
- def initialize
17
- @world = "World"
18
- @true = true
19
- end
20
-
21
- checklist do
22
- check "World should contains the world string" do
23
- world == "World"
24
- end
25
- check "the truth" do
26
- @true == true
27
- end
28
- end
29
- end
10
+ require_relative 'testing_checklist'
30
11
 
31
12
  # verify if there is no collapsing in the rules
32
13
  class TestingChecklist2
@@ -63,6 +44,34 @@ describe Checklist do
63
44
  end
64
45
  end
65
46
 
47
+ describe 'filtered_items' do
48
+ context 'when only is set to true' do
49
+ it 'should return the item' do
50
+ instance.only = true
51
+ instance.checklist.filtered_items.map{|i| i.explain}.should include("the truth")
52
+ end
53
+ end
54
+
55
+ context 'when only is set to false' do
56
+ it 'should not return the item' do
57
+ instance.only = false
58
+ instance.checklist.filtered_items.map{|i| i.explain}.should_not include("the truth")
59
+ end
60
+ end
61
+ context 'when except is set to true' do
62
+ it 'should not return the item' do
63
+ instance.except = true
64
+ instance.checklist.filtered_items.map{|i| i.explain}.should_not include("World should contains the world string")
65
+ end
66
+ end
67
+ context 'when except is set to false' do
68
+ it 'should return the item' do
69
+ instance.except = false
70
+ instance.checklist.filtered_items.map{|i| i.explain}.should include("World should contains the world string")
71
+ end
72
+ end
73
+ end
74
+
66
75
  describe 'valid?' do
67
76
  context 'when calling from instance' do
68
77
  it 'should return true if all conditions return true' do
@@ -0,0 +1,23 @@
1
+ require_relative '../lib/checklist'
2
+
3
+ class TestingChecklist
4
+ include Checklist
5
+
6
+ attr_accessor :world, :only, :except
7
+
8
+ def initialize
9
+ @world = "World"
10
+ @true = true
11
+ @only = false
12
+ @except = false
13
+ end
14
+
15
+ checklist do
16
+ check "World should contains the world string", except: :except do
17
+ world == "World"
18
+ end
19
+ check "the truth", only: :only do
20
+ @true == true
21
+ end
22
+ end
23
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: checkthelist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -18,13 +18,14 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
- - lib/checklist.rb
22
21
  - lib/checklist/checklist.rb
23
22
  - lib/checklist/exceptions.rb
24
23
  - lib/checklist/item.rb
25
24
  - lib/checklist/list.rb
25
+ - lib/checklist.rb
26
26
  - spec/checklist_spec.rb
27
- homepage: http://rubygems.org/gems/checkmylist
27
+ - spec/testing_checklist.rb
28
+ homepage: http://rubygems.org/gems/checkthelist
28
29
  licenses:
29
30
  - MIT
30
31
  post_install_message: