class Evertils::Action::Search

Constants

Formatting

Public Class Methods

new(args) click to toggle source

@since 2.2.2

# File lib/evertils/actions/search.rb, line 10
def initialize(args)
  @note_helper = Evertils::Helper::Note.instance
  @args = args
  @note = @note_helper.find_note_by_grammar(grammar.to_s)
  @api_helper = Evertils::Helper::ApiEnmlHandler.new(@config)

  execute
end

Private Instance Methods

execute() click to toggle source

@since 2.2.2

# File lib/evertils/actions/search.rb, line 23
def execute
  return Notify.error('Note not found') if @note.entity.nil?

  search_for(@args.term)
end
grammar() click to toggle source

@since 2.2.2

# File lib/evertils/actions/search.rb, line 31
def grammar
  terms = Grammar.new
  terms.tags = {
    day: Date.today.yday,
    week: Date.today.cweek
  }
  terms.notebook = @args.notebook
  terms.created = Date.new(Date.today.year, 1, 1).strftime('%Y%m%d')
  terms
end
grep_results_for(text) click to toggle source

@since 2.2.2

# File lib/evertils/actions/search.rb, line 70
def grep_results_for(text)
  return search_nodes.select { |line| line.scan(text) } if text.is_a? Regexp

  search_nodes.select { |line| line.include? text }
end
search_for(text) click to toggle source

@since 2.2.2

# File lib/evertils/actions/search.rb, line 44
def search_for(text)
  results = grep_results_for(text)

  return Notify.error("No rows matched search query {#{text}}") if results.empty?

  Notify.success("#{results.size} rows matched query {#{text}}")
  results.each { |res| Notify.info(Formatting.clean(res)) }
end
search_nodes() click to toggle source

@since 2.2.2

# File lib/evertils/actions/search.rb, line 55
def search_nodes
  xml = @api_helper.from_str(@note.entity.content)
  target = xml.search('en-note').first
  nodes = []

  target.children.each do |child|
    node = child.children.first.to_s
    nodes.push(Formatting.clean(node)) unless node.empty? || node == '<br/>'
  end

  nodes
end