class GmailBritta::Filter

This class specifies the behavior of a single filter definition. Create a filter object in the DSL via {FilterSet::Delegator#filter} or use {Filter#also}, {Filter#otherwise} or {Filter#archive_unless_directed} to make a new filter based on another one. @todo this probably needs some explanatory docs to make it understandable.

Public Class Methods

new(britta, options={}) click to toggle source

Create a new filter object @note Over the lifetime of {GmailBritta}, new {Filter}s usually get created only by the {FilterSet::Delegate}. @param [GmailBritta::Britta] britta the filterset object @option options :log [Logger] a logger for debug messages

# File lib/gmail-britta/filter.rb, line 185
def initialize(britta, options={})
  @britta = britta
  @log = options[:log]
  @from = []
  @to = []
  @has = []
  @has_not = []
end

Protected Class Methods

emit_filter_spec(filter, infix=' ', recursive=false) click to toggle source
# File lib/gmail-britta/filter.rb, line 229
def self.emit_filter_spec(filter, infix=' ', recursive=false)
  case filter
  when String
    filter
  when Hash
    str = ''
    filter.keys.each do |key|
      infix = ' '
      prefix = ''
      case key
      when :or
        infix = ' OR '
      when :and
        infix = ' AND '
      when :not
        prefix = '-'
        recursive = true
      end
      str << prefix + emit_filter_spec(filter[key], infix, recursive)
    end
    str
  when Array
    str_tmp = filter.map {|elt| emit_filter_spec(elt, ' ', true)}.join(infix)
    if recursive
      "(#{str_tmp})"
    else
      str_tmp
    end
  end
end

Public Instance Methods

also(&block) click to toggle source

Register and return a new filter that matches a message only if this filter's conditions and the previous filter's condition match. @yield The filter definition block @return [Filter] the new filter

# File lib/gmail-britta/filter.rb, line 151
def also(&block)
  chain(PositiveChainingFilter, &block)
end
archive_unless_directed(options={}) click to toggle source

Register (but don't return) a filter that archives the message unless it matches the `:to` email addresses. Optionally, mark the message as read if this filter matches.

@note This method returns the previous filter to make it easier

to construct filter chains with {#otherwise} and {#also}
with {#archive_unless_directed} in the middle.

@option options [true, false] :mark_read If true, mark the message as read @option options [Array<String>] :to a list of addresses that the message may be addressed to in order to prevent this filter from matching. Defaults to the value given to :me on {GmailBritta.filterset}. @return [Filter] `self` (not the newly-constructed filter)

# File lib/gmail-britta/filter.rb, line 166
def archive_unless_directed(options={})
  mark_as_read=options[:mark_read]
  tos=Array(options[:to] || me)
  filter = PositiveChainingFilter.new(self).perform do
    has_not [{:or => tos.map {|to| "to:#{to}"}}]
    archive
    if mark_as_read
      mark_read
    end
  end
  filter.log_definition
  self
end
chain(type, &block) click to toggle source

@!group Filter chaining

# File lib/gmail-britta/filter.rb, line 131
def chain(type, &block)
  filter = type.new(self).perform(&block)
  filter.log_definition
  filter
end
generate_xml() click to toggle source

Return the filter's value as XML text. @return [String] the Atom XML representation of this filter

# File lib/gmail-britta/filter.rb, line 196
    def generate_xml
      generate_xml_properties
      engine = Haml::Engine.new("
%entry
  %category{:term => 'filter'}
  %title Mail Filter
  %content
#{generate_haml_properties 1}
", :attr_wrapper => '"')
      engine.render(self)
    end
generate_xml_properties() click to toggle source
# File lib/gmail-britta/filter.rb, line 208
def generate_xml_properties
  engine = Haml::Engine.new(generate_haml_properties, :attr_wrapper => '"')
  engine.render(self)
end
otherwise(&block) click to toggle source

Register and return a new filter that matches only if this Filter's conditions (those that are not duplicated on the new Filter's {#has} clause) *do not* match. @yield The filter definition block @return [Filter] the new filter

# File lib/gmail-britta/filter.rb, line 142
def otherwise(&block)
  chain(NegatedChainingFilter, &block)
end
perform(&block) click to toggle source

Evaluate block as a filter definition block and register `self` as a filter on the containing {FilterSet} @note this method gets called by {Delegate#filter} to create and register a new filter object @yield The filter definition. `self` in the block is the new filter object. @api private @return [Filter] the filter that

# File lib/gmail-britta/filter.rb, line 218
def perform(&block)
  instance_eval(&block)
  @britta.filters << self
  self
end

Protected Instance Methods

filterset() click to toggle source
# File lib/gmail-britta/filter.rb, line 225
def filterset; @britta; end
log_definition() click to toggle source

Note a filter definition on the logger. @note for debugging only.

# File lib/gmail-britta/filter.rb, line 262
def log_definition
  return unless @log.debug?
  @log.debug  "Filter: #{self}"
  Filter.single_write_accessors.keys.each do |name, gmail_name|
    val = send(:"get_#{name}")
    @log.debug "  #{name}: #{val}" if val
  end
  self
end
logger() click to toggle source
# File lib/gmail-britta/filter.rb, line 227
def logger; @log ; end
me() click to toggle source

Return the list of emails that the filterset has configured as “me”.

# File lib/gmail-britta/filter.rb, line 273
def me
  @britta.me
end

Private Instance Methods

generate_haml_properties(indent=0) click to toggle source
# File lib/gmail-britta/filter.rb, line 279
    def generate_haml_properties(indent=0)
      properties =
"- self.class.single_write_accessors.keys.each do |name|
  - gmail_name = self.class.single_write_accessors[name]
  - if value = self.send(\"output_\#{name}\".intern)
    %apps:property{:name => gmail_name, :value => value.to_s}"
      if (indent)
        indent_sp = ' '*indent*2
        properties = indent_sp + properties.split("\n").join("\n" + indent_sp)
      end
      properties
    end