Class: Taggata::Parser::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/taggata/parser/tag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Tag

Returns a new instance of Tag.



7
8
9
# File 'lib/taggata/parser/tag.rb', line 7

def initialize(db)
  @db = db
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



5
6
7
# File 'lib/taggata/parser/tag.rb', line 5

def db
  @db
end

Instance Method Details

#parse(query) ⇒ Hash

Parses give tagging string

Parameters:

  • query

    String tagging string

Returns:

  • (Hash)


15
16
17
18
19
20
21
22
23
# File 'lib/taggata/parser/tag.rb', line 15

def parse(query)
  result = { :add => [], :del => [] }
  hash = query.split.reduce(result) do |acc, tag|
    handle_tag(tag, acc)
  end
  dels = hash[:del].empty? ? [] : ::Taggata::Persistent::Tag.find(db, :name => hash[:del])
  adds = hash[:add].empty? ? [] : find_tags(hash[:add])
  { :add => adds, :del => dels }
end