Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/doing/helpers.rb

Instance Method Summary collapse

Instance Method Details

#has_tags?(tags, bool = :and) ⇒ Boolean



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/doing/helpers.rb', line 5

def has_tags?(tags, bool = :and)
  tags = tags.split(/ *, */) if tags.is_a? String
  bool = bool.normalize_bool if bool.is_a? String
  item = self
  tags.map! {|t| t.strip.sub(/^@/, '')}
  case bool
  when :and
    result = true
    tags.each do |tag|
      unless item['title'] =~ /@#{tag}/
        result = false
        break
      end
    end
    result
  when :not
    result = true
    tags.each do |tag|
      if item['title'] =~ /@#{tag}/
        result = false
        break
      end
    end
    result
  else
    result = false
    tags.each do |tag|
      if item['title'] =~ /@#{tag}/
        result = true
        break
      end
    end
    result
  end
end

#matches_search?(search) ⇒ Boolean



41
42
43
44
45
46
47
48
49
50
# File 'lib/doing/helpers.rb', line 41

def matches_search?(search)
  item = self
  text = item['note'] ? item['title'] + item['note'].join(' ') : item['title']
  pattern = if search.strip =~ %r{^/.*?/$}
              search.sub(%r{/(.*?)/}, '\1')
            else
              search.split('').join('.{0,3}')
            end
  text =~ /#{pattern}/i ? true : false
end