Class: Tagformula::Parser::Group

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

Instance Method Summary collapse

Constructor Details

#initializeGroup

Returns a new instance of Group.



6
7
8
# File 'lib/tagformula/parser.rb', line 6

def initialize()
	@contents = []
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/tagformula/parser.rb', line 9

def empty?
	@contents.empty?
end

#last_entryObject



12
13
14
# File 'lib/tagformula/parser.rb', line 12

def last_entry
	@contents.last
end

#matches?(taglist) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tagformula/parser.rb', line 37

def matches?(taglist)
	condition, op, i = nil, nil, 0
	while @contents[i]
		case @contents[i]
		when Symbol
			op = @contents[i]
		when Group, Tag
			res = @contents[i].matches?(taglist)
			raise "Epic fail - res is neither true nor false" unless [true,false].include?(res)
			case op
			when :and
				condition &&= res
			when :or
				condition ||= res
			when nil
				if i.zero?
					condition = res
				else
					raise "Epic fail - apparently no operator between tags.  How does this even happen?"
				end
			end
		else
			raise "Epic fail - invalid group entry" # epic fail
		end
		i += 1
	end
	condition
end

#push_condition(what) ⇒ Object



15
16
17
18
19
# File 'lib/tagformula/parser.rb', line 15

def push_condition(what)
	if [:and,:or].include?(what) && (! @contents.last.is_a?(Symbol))
		@contents << what
	end
end

#push_entry(entry) ⇒ Object



20
21
22
23
24
25
# File 'lib/tagformula/parser.rb', line 20

def push_entry(entry)
	case entry
	when Tag, Group
		@contents << entry
	end
end

#required_tagsObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tagformula/parser.rb', line 26

def required_tags
	@contents.map do |content|
		if content.is_a?(Tag) && content.needed
			content.tagname
		elsif content.is_a?(Group)
			content.required_tags
		else
			nil
		end
	end.flatten.compact
end