class EcoRake module Options # Gathers all the option presets (`EcoRake::Options::Set`) defined. # It works at class level and does lazy loading. class Library extend RakeCommander::Base::ClassHelpers extend RakeCommander::Base::ClassAutoLoader class_resolver :item_class, EcoRake::Options::Set autoloads_children_of :item_class autoload_namespace_ignore "EcoRake::Options" class << self include Enumerable # Iterator def each(&block) return to_enum(:each) unless block_given? sets.values.each(&block) end # Retrieve an options set # @return [EcoRake::Options::Set] the class with that `name` def [](value) sets[to_name(value)] end # @return [Boolean] def set?(name) raise "Expected String or Symbol. Given: #{name.class}" unless name.respond_to?(:to_sym) sets.key?(name.to_sym) end # @return [Array] names list of all available option sets. def names setes.keys end # Add a new options set. # @note # 1. If it receives an `item_class` (`Class`) it just adds it in # 2. If it receives an object instance, it adds its class # @return [EcoRake::Options::Set] the class def add(options_set) klass = options_set.is_a?(Class)? options_set : options_set.class raise "Expecting #{item_class} child or instance. Given: #{klass}" unless klass <= item_class puts "Warning: redefining options set '#{set.name}'" if self[options_set.name] sets[options_set.name] = klass end private def autoload_children super(self) do |instance| add(instance) end end # It loads pending children of `item_class` every time it's invoked. # @note if sets is used as unique access point, this should ensure all children are known/loaded. # @return [Hash] where `key` is option_set name and value is the `item_class` (i.e. EcoRake::Options::Set) def sets autoload_children @sets ||= {} end # To-name converter # @return [Symbol, NilClass] def to_name(value) case value when String, Symbol value.to_sym when item_class value.name end end end end end end