Module: GmailBritta::SingleWriteAccessors::ClassMethods

Included in:
GmailBritta::SingleWriteAccessors
Defined in:
lib/gmail-britta/single_write_accessors.rb

Instance Method Summary collapse

Instance Method Details

#single_write_accessor(name, gmail_name, &block) ⇒ Object

Defines a string-typed filter accessor DSL method. Generates the [name], get_[name] and output_[name] methods.

Parameters:

  • name (Symbol)

    the name of the accessor method

  • gmail_name (String)

    the name of the attribute in the gmail Atom export


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gmail-britta/single_write_accessors.rb', line 23

def single_write_accessor(name, gmail_name, &block)
  direct_single_write_accessors[name] = gmail_name
  ivar = ivar_name(name)
  define_method(name) do |words|
    if instance_variable_get(ivar) and instance_variable_get(ivar) != []
      raise "Only one use of #{name} is permitted per filter"
    end
    instance_variable_set(ivar, words)
  end
  get(name, ivar)
  if block_given?
    define_method("output_#{name}") do
      instance_variable_get(ivar) && block.call(instance_variable_get(ivar)) unless instance_variable_get(ivar) == []
    end
  else
    output(name, ivar)
  end
end

#single_write_accessorsArray<Symbol>

Returns the single write accessors defined on this class and every superclass.

Returns:

  • (Array<Symbol>)

    the single write accessors defined on this class and every superclass.


10
11
12
13
14
15
16
# File 'lib/gmail-britta/single_write_accessors.rb', line 10

def single_write_accessors
  super_accessors = {}
  if self.superclass.respond_to?(:single_write_accessors)
    super_accessors = self.superclass.single_write_accessors
  end
  super_accessors.merge(direct_single_write_accessors)
end

#single_write_boolean_accessor(name, gmail_name) ⇒ Object

Note:

There is no way to turn these boolean values back off in Gmail's export XML.

Defines a boolean-typed filter accessor DSL method. If the method gets called in the filter definition block, that causes the value to switch to true.

Parameters:

  • name (Symbol)

    the name of the accessor method

  • gmail_name (String)

    the name of the attribute in the gmail Atom export


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gmail-britta/single_write_accessors.rb', line 50

def single_write_boolean_accessor(name, gmail_name)
  direct_single_write_accessors[name] = gmail_name
  ivar = ivar_name(name)
  define_method(name) do |*args|
    value = args.length > 0 ? args[0] : true
    if instance_variable_get(ivar)
      raise "Only one use of #{name} is permitted per filter"
    end
    instance_variable_set(ivar, value)
  end
  get(name, ivar)
  output(name, ivar)
end