Class: Noteman::NoteManager

Inherits:
Object
  • Object
show all
Includes:
Config
Defined in:
lib/noteman/note_manager.rb

Constant Summary

Constants included from Config

Config::CONFIG_NAME

Instance Attribute Summary collapse

Attributes included from Config

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config

#home_config, #read_from_file

Constructor Details

#initializeNoteManager

Returns a new instance of NoteManager.



6
7
8
9
10
11
12
# File 'lib/noteman/note_manager.rb', line 6

def initialize
  @notes = []
  Dir.chdir(File.expand_path(config['notes_in']))
  Dir.glob("*.#{config['ends_with']}").each do |file|
    @notes << Note.new(file)
  end
end

Instance Attribute Details

#notesObject

Returns the value of attribute notes.



4
5
6
# File 'lib/noteman/note_manager.rb', line 4

def notes
  @notes
end

#resultObject

Returns the value of attribute result.



4
5
6
# File 'lib/noteman/note_manager.rb', line 4

def result
  @result
end

#tagsObject

Returns the value of attribute tags.



4
5
6
# File 'lib/noteman/note_manager.rb', line 4

def tags
  @tags
end

Class Method Details

.searchObject



14
15
16
17
# File 'lib/noteman/note_manager.rb', line 14

def self.search
  @@manager = NoteManager.new unless defined? @@manager
  @@manager.reset_filter
end

Instance Method Details

#by_keywords(keywords) ⇒ Object



32
33
34
35
36
37
# File 'lib/noteman/note_manager.rb', line 32

def by_keywords(keywords)
  if keywords && keywords.length > 0
    @result.delete_if { |note| not note.contains? keywords }
  end
  self
end

#by_tags(tags) ⇒ Object



25
26
27
28
29
30
# File 'lib/noteman/note_manager.rb', line 25

def by_tags(tags)
  if tags && tags.length > 0
    @result.delete_if { |note| not note.with_tags? tags }
  end
  self
end

#capture(input, link = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/noteman/note_manager.rb', line 57

def capture(input, link=nil)
  if link
    content = "[#{input}](#{link})"
  else
    content = input
  end
  open(config['capture_to'], 'a') do |f|
    f << "#{content}\n"
  end
end

#choose(notes) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/noteman/note_manager.rb', line 68

def choose(notes)
  if notes.length > 1
    notes.each_with_index do |note, i|
      puts "% 3d: %s" % [i, note.file]
    end
    print "> "
    num = STDIN.gets
    return false if num =~ /^[a-z ]*$/i
    chosen = notes[num.to_i]
  elsif notes.length == 1
    chosen = notes[0]
  else
    chosen = false
  end
  chosen
end

#fork_editor(input = "") ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/noteman/note_manager.rb', line 101

def fork_editor(input="")
  tmpfile = Tempfile.new('note')

  File.open(tmpfile.path,'w+') do |f|
    f.puts input
  end

  pid = Process.fork { system("$EDITOR #{tmpfile.path}") }

  trap("INT") {
    Process.kill(9, pid) rescue Errno::ESRCH
    tmpfile.unlink
    tmpfile.close!
    exit 0
  }

  Process.wait(pid)

  begin
    if $?.exitstatus == 0
      input = IO.read(tmpfile.path)
    else
      raise "Cancelled"
    end
  ensure
    tmpfile.close
    tmpfile.unlink
  end

  input
end

#reset_filterObject



19
20
21
22
23
# File 'lib/noteman/note_manager.rb', line 19

def reset_filter
  @result = []
  @result += @notes
  self
end

#search_by_fomular(name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/noteman/note_manager.rb', line 39

def search_by_fomular(name)
  fomular = config['fomular'][name]
  if fomular
    search
    tags = fomular['tags'].split(' ')
    keywords = fomular['keywords'].split(' ')
    if tags
      by_tags tags
    end
    if keywords
      by_keywords keywords
    end
  else
    puts "No such fomular defined #{name}."
  end
  self
end