module AutoForme::OptsAttributes

  1. lib/autoforme/opts_attributes.rb

Methods

Public Instance

  1. opts_attribute

Public Instance methods

opts_attribute (*meths)

Setup methods for each given argument such that if the method is called with an argument or block, it sets the value of the related option to that argument or block. If called without an argument or block, it returns the stored option value.

[show source]
# File lib/autoforme/opts_attributes.rb, line 7
def opts_attribute(*meths)
  meths.each do |meth|
    define_method(meth) do |*args, &block|
      if block
        if args.empty?
          opts[meth] = block
        else
          raise ArgumentError, "No arguments allowed if passing a block"
        end
      end

      case args.length
      when 0
        opts[meth]
      when 1
        opts[meth] = args.first
      else
        raise ArgumentError, "Only 0-1 arguments allowed"
      end
    end
  end
end