Sha256: 78bd37aedbcb3844940284729ed7cf86d98e1c07db813caedf179fd927e4e488

Contents?: true

Size: 1.46 KB

Versions: 14

Compression:

Stored size: 1.46 KB

Contents

module Vedeu

  # Converts an options Hash into a class containing methods for each of the
  # keys, which when called returns the value associated. When the value is
  # either true or false, an additional predicate method is created.
  class Options

    # @!attribute [r] _defined
    # @return [Array<Symbol>]
    attr_reader :_defined

    # @param options [Hash]
    # @return [Vedeu::Options]
    def self.build(options = {})
      new(options)._build
    end

    # @param options [Hash]
    # @return [Vedeu::Options]
    def initialize(options = {})
      @_options = options
      @_defined = []
    end

    # @return [Vedeu::Options]
    def _build
      _options.each do |name, value|
        _create_method(name) { value }

        if value.is_a?(TrueClass) || value.is_a?(FalseClass)
          _create_alias("#{name}?", name)
        end
      end

      self
    end

    protected

    # @!attribute [r] _options
    # @return [Hash]
    attr_reader :_options

    private

    # @param alias_name [String|Symbol]
    # @param method_name [String|Symbol]
    # @return [void]
    def _create_alias(alias_name, method_name)
      @_defined << alias_name.to_sym

      define_singleton_method(alias_name, method(method_name))
    end

    # @param name [String|Symbol]
    # @param block [Proc]
    # @return [void]
    def _create_method(name, &block)
      @_defined << name.to_sym

      self.class.send(:define_method, name, &block)
    end

  end # Option

end # Vedeu

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
vedeu-0.4.32 lib/vedeu/support/options.rb
vedeu-0.4.31 lib/vedeu/support/options.rb
vedeu-0.4.30 lib/vedeu/support/options.rb
vedeu-0.4.29 lib/vedeu/support/options.rb
vedeu-0.4.28 lib/vedeu/support/options.rb
vedeu-0.4.27 lib/vedeu/support/options.rb
vedeu-0.4.26 lib/vedeu/support/options.rb
vedeu-0.4.25 lib/vedeu/support/options.rb
vedeu-0.4.24 lib/vedeu/support/options.rb
vedeu-0.4.23 lib/vedeu/support/options.rb
vedeu-0.4.22 lib/vedeu/support/options.rb
vedeu-0.4.21 lib/vedeu/support/options.rb
vedeu-0.4.20 lib/vedeu/support/options.rb
vedeu-0.4.19 lib/vedeu/support/options.rb