Class: WLang::EncoderSet

Inherits:
Object
  • Object
show all
Defined in:
lib/wlang/encoder_set.rb,
lib/wlang/dialects/sql_dialect.rb,
lib/wlang/dialects/yaml_dialect.rb,
lib/wlang/dialects/ruby_dialect.rb,
lib/wlang/dialects/rdoc_dialect.rb,
lib/wlang/dialects/xhtml_dialect.rb,
lib/wlang/dialects/hosted_dialect.rb,
lib/wlang/dialects/coderay_dialect.rb,
lib/wlang/dialects/redcloth_dialect.rb,
lib/wlang/dialects/bluecloth_dialect.rb,
lib/wlang/dialects/plain_text_dialect.rb

Overview

This class allows grouping encoders together to build a given dialect. Encoders are always added with add_encoder, which also allows creating simple encoders on the fly (that is, without subclassing Encoder).

Examples:

# we will create a simple encoder set with two encoders, one for upcasing
# the other for downcasing.
encoders = EncoderSet.new
encoder.add_encoder 'upcaser' do |src,options|
  src.upcase
end 
encoder.add_encoder 'downcaser' do |src,options|
  src.downcase
end

Detailed API

Defined Under Namespace

Modules: BlueClothEncoders, CodeRayEncoderSet, Hosted, PlainText, RDocEncoders, RedClothEncoders, Ruby, SQL, XHtml, YAML

Instance Method Summary collapse

Constructor Details

#initializeEncoderSet

Creates an empty encoder set.



24
25
26
# File 'lib/wlang/encoder_set.rb', line 24

def initialize
  @encoders = {}
end

Instance Method Details

#add_encoder(name, encoder = nil, &block) ⇒ Object

Adds an encoder under a specific name. Supported signatures are as follows:

  • name is a symbol: encoder and block are ignored and encoder is interpreted as being a method of the String class whose name is the symbol.

  • name is a String and a block is provided: encoder is expected to be implemented by the block which takes |src,options| arguments.

  • name is a String and encoder is a Proc: same as if encoder was a given block.

Examples:

encoders = EncoderSet.new
# add an encoder by reusing String method
encoders.add_encoder(:upcase)

# add an encoder by providing a block
encoders.add_encoder("ucase") do |src,options|
  src.upcase
end

# add an encoder by providing a Proc
upcaser = Proc.new {|src,options| src.upcase}
encoders.add_encoder("upcase", upcaser)

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/wlang/encoder_set.rb', line 56

def add_encoder(name, encoder=nil, &block)
  # handle String method through symbol 
  if Symbol===name 
    encoder, block = nil, Proc.new {|src,options| src.send(name)}
    name = name.to_s
  elsif Proc===encoder
    encoder, block = nil, encoder
  end

  # check arguments
  if encoder.nil? 
    raise(ArgumentError,"Block required") if block.nil?
    encoder = Encoder.new(&block)
  end
  raise(ArgumentError, "Encoder expected") unless Encoder===encoder
    
  # save encoder
  @encoders[name] = encoder
end

#add_encoders(mod, pairs = nil) ⇒ Object

Adds reusable encoders defined in a Ruby module. mod must be a Module instance. If pairs is ommitted (nil), all encoders of the module are added, under their standard names (see DEFAULT_ENCODERS under WLang::EncoderSet::XHtml for example). Otherwise, pairs is expected to be a Hash providing a mapping between encoder names and mod methods (whose names are given by ruby symbols).

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
# File 'lib/wlang/encoder_set.rb', line 83

def add_encoders(mod, pairs=nil)
  raise(ArgumentError,"Module expected",caller) unless Module===mod
  pairs = mod::DEFAULT_ENCODERS if pairs.nil?
  pairs.each_pair do |name,method|
    meth = mod.method(method)
    raise(ArgumentError,"No such method: #{method} in #{mod}") if meth.nil?
    add_encoder(name, &meth.to_proc)
  end
end

#eachObject

Yields the block with name, encoder pairs



29
30
31
# File 'lib/wlang/encoder_set.rb', line 29

def each
  @encoders.each_pair{|name,encoder| yield(name, encoder)}
end

#encode(encoder, src, options = nil) ⇒ Object

Shortcut for get_encoder(encoder).encode(source,options)

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
# File 'lib/wlang/encoder_set.rb', line 106

def encode(encoder, src, options=nil)
  if String===encoder then 
    ename, encoder = encoder, get_encoder(encoder) 
    raise(ArgumentError,"No such encoder: #{ename}") if encoder.nil?
  end
  raise(ArgumentError,"Invalid encoder: #{encoder}") unless Encoder===encoder
  encoder.encode(src, options)
end

#get_encoder(name) ⇒ Object

Returns an encoder by its name, nil if no such encoder.



99
100
101
# File 'lib/wlang/encoder_set.rb', line 99

def get_encoder(name)
  @encoders[name]
end

#has_encoder?(name) ⇒ Boolean

Checks if an encoder is installed under _name.

Returns:

  • (Boolean)


94
95
96
# File 'lib/wlang/encoder_set.rb', line 94

def has_encoder?(name) 
  @encoders.has_key?(name)
end

#to_sObject

Returns a string with comma separated encoder names.



116
117
118
# File 'lib/wlang/encoder_set.rb', line 116

def to_s
  @encoders.keys.join(", ")
end