class Noteman::NoteManager

Attributes

notes[RW]
result[RW]
tags[RW]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

by_keywords(keywords) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/noteman/note_manager.rb, line 72
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="") click to toggle source
# File lib/noteman/note_manager.rb, line 105
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
inbox() click to toggle source
# File lib/noteman/note_manager.rb, line 68
def inbox
  notes.select { |n| n.file == config['capture_to'] }.first
end
reset_filter() click to toggle source
# File lib/noteman/note_manager.rb, line 19
def reset_filter
  @result = []
  @result += @notes
  self
end
search_by_fomular(name) click to toggle source
# 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