# Author:: Nicolas Desprès . # Copyright:: Copyright (c) 2005 Uttk Team. All rights reserved. # License:: LGPL # $Id: /w/fey/uttk/trunk/lib/uttk/filters.rb 22057 2006-02-20T21:40:27.185401Z pouillar $ module Uttk module Filters def self.[] ( *yaml_desc ) joined_yaml_desc = yaml_desc.join(', ') joined_yaml_desc.sub!(/^\s+/, '') if joined_yaml_desc[0,1] != '{' and joined_yaml_desc[0,1] != '[' full_doc = "--- {#{joined_yaml_desc}}" else full_doc = "--- #{joined_yaml_desc}" end create_backend(YAML.load(full_doc)).flatten end def Symbol.yaml_load ( val ) val.to_sym end def Integer.yaml_load ( val ) val.to_i end class FilterOption attr_reader :option def initialize ( anObject ) @option = anObject end def to_hash { self.class.name.demodulize.underscore.to_sym => option } end def self.yaml_load ( anObject ) new(superclass.name.demodulize.sub(/Option$/, '').constantize.yaml_load(anObject)) end end class SymbolOption < FilterOption end class RegexpOption < FilterOption end # class RegexpOption class IntegerOption < FilterOption end # class IntegerOption class BooleanOption < FilterOption def self.yaml_load ( anObject ) new(anObject) end end # class BooleanOption class Action < SymbolOption have YamlExtension end class Keep < RegexpOption have YamlExtension end # class Keep class Skip < RegexpOption have YamlExtension end # class Skip class Width < IntegerOption have YamlExtension end # class Width class Prune < BooleanOption have YamlExtension end # class Prune class << self def create_backend ( desc ) case desc when Array desc.map { |elt| create_backend(elt) } when String [create_backend_leaf(desc, STDOUT)] when Hash desc.map do |k, v| create_backend_leaf(k, *create_backend(v).flatten) end when FilterOption [desc] when nil [STDOUT] else raise OptionParser::InvalidArgument, "need a string or a hash not #{desc.inspect}" end end private :create_backend def create_backend_leaf ( aConst, *args ) unless aConst.is_a? String raise OptionParser::InvalidArgument, "need a string not #{aConst.inspect}" end if aConst =~ ConstRegexp::RB_CONST if Dumpers.const_defined? aConst return Dumpers.const_get(aConst).new(*args) else if Filters.const_defined? aConst mod = Filters elsif PathFilters.const_defined? aConst mod = PathFilters else return aConst.to_path.open('w') end options, observers = args.partition { |arg| arg.is_a? FilterOption } options = options.inject({}) { |accu, opt| accu.merge! opt.to_hash } return mod.const_get(aConst).new(observers, options) end end aConst.to_path.open('w') end private :create_backend_leaf end end # module Filters end # module Uttk