Class: WMLAction::Tag

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/wml_action/tag.rb

Defined Under Namespace

Classes: Action, Attribute, Filter, Macro

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

#level=, #log

Constructor Details

#initialize(values = {}) ⇒ Tag

Returns a new instance of Tag.



34
35
36
37
38
39
40
41
42
# File 'lib/wml_action/tag.rb', line 34

def initialize(values={})
  @name=values[:name]||""
  @subs=values[:subs]||Array.new
  @attrs=values[:attrs]||Hash.new
  @macros=values[:macros]||Set.new
  @filter=values[:filter]||Array.new
  @actions=values[:actions]||Array.new
  load_content( values[:content] ) if values.key? :content
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



8
9
10
# File 'lib/wml_action/tag.rb', line 8

def actions
  @actions
end

#attrsObject

Returns the value of attribute attrs.



8
9
10
# File 'lib/wml_action/tag.rb', line 8

def attrs
  @attrs
end

#filterObject

Returns the value of attribute filter.



8
9
10
# File 'lib/wml_action/tag.rb', line 8

def filter
  @filter
end

#macrosObject

Returns the value of attribute macros.



8
9
10
# File 'lib/wml_action/tag.rb', line 8

def macros
  @macros
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/wml_action/tag.rb', line 8

def name
  @name
end

#subsObject

Returns the value of attribute subs.



8
9
10
# File 'lib/wml_action/tag.rb', line 8

def subs
  @subs
end

Instance Method Details

#<<(content) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wml_action/tag.rb', line 44

def <<(content)
  case content
  when Action then @actions << content
  when Attribute then @attrs.merge!( Hash[*content] )
  when Macro then @macros.add( content.value )
  when Filter then @filter << content.value
  when Tag then @subs << content
  else raise TypeError.new("Can not add #{content.class}: #{content} to a Tag")
  end
  return self
end

#action?(action) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/wml_action/tag.rb', line 138

def action?(action)
  @actions.include?(action)
end

#attr?(attr) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/wml_action/tag.rb', line 122

def attr?( attr )
  @attrs.key?(attr.name)
end

#attr_value?(attr) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/wml_action/tag.rb', line 118

def attr_value?( attr )
  @attrs[attr.name] == attr.value
end

#delete(content) ⇒ Object



100
101
102
103
104
105
# File 'lib/wml_action/tag.rb', line 100

def delete(content)
  case content
  when Tag then @subs.delete_if { |s| content.name == s.name && s.match?( content.filter ) }
  when Macro then @macros.delete(content.value)
  end
end

#filter?(filter) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/wml_action/tag.rb', line 134

def filter?(filter)
  @filter.include?(filter)
end

#load_content(contents) ⇒ Object



56
57
58
# File 'lib/wml_action/tag.rb', line 56

def load_content(contents)
  contents.each { |c| self<<c }
end

#macro?(macro) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/wml_action/tag.rb', line 126

def macro?( macro )
  @macros.include?(macro.value)
end

#match?(filter) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
# File 'lib/wml_action/tag.rb', line 107

def match?( filter )
  return true if filter.empty?
  filter.each do |f|
    case f
    when Attribute then return false unless attr_value?( f )
    when Macro then return false unless macro?( f )
    end
  end
  return true
end

#merge(other) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/wml_action/tag.rb', line 75

def merge(other)
  return self unless @name == other.name
  return self unless match?( other.filter )
  log.info "Merging [#{@name}] section with [#{other.name}] with filter: #{other.filter}"
  other.attrs.each_pair do |key,value|
    log.debug "Processing key: #{key}=#{value}"
    @attrs.store(key,value)
  end
  other.macros.each do |macro|
    log.debug "Adding macro: #{macro}"
    @macros<<(macro)
  end
  other.subs.each do |other_sub|
    @subs.map { |sub| sub.merge(other_sub) }
  end
  other.actions.each do |a|
    case a.action
    when '+' then self << a.object
    when '-' then delete( a.object )
    else raise NoMethodError.new("Don't know what to do with #{a.action} action")
    end
  end
  return self
end

#sub?(sub) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/wml_action/tag.rb', line 130

def sub?( sub )
  ( @subs.keep_if { |s| s.name == sub.name && s.match?(sub.filter) } ).empty?
end

#to_s(indent = 0, indent_first_line = 1) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/wml_action/tag.rb', line 60

def to_s(indent=0,indent_first_line=1)
  i=indent
  t="\t"
  ifl=indent_first_line
  return "    |\#{t*i*ifl}[\#{@name}]\n    |\#{(@filter.map { |f| \"\#{t*(i+1)}/ \#{f}\" }).join(\"\\n\")}\n    |\#{(@actions.map { |a| \"\#{t*(i+1)}\#{a.to_s(i+1)}\" }).join(\"\\n\")}\n    |\#{(@attrs.map   { |k,v| \"\#{t*(i+1)}\#{k}=\#{v}\" }).join(\"\\n\")}\n    |\#{(@macros.map { |m| \"\#{t*(i+1)}\#{m}\" }).join(\"\\n\")}\n    |\#{(@subs.map   { |s| \"\#{s.to_s(i+1)}\" }).join(\"\\n\")}\n    |\#{t*i}[/\#{@name}]\n  EOS\nend\n".gsub(/^\s+\|/, '').gsub(/^$\n/,'')