Sha256: fe9fa5221938fa73a372fea1456bb036c9dbbcabb587ee469d6f4fbec4ad04af

Contents?: true

Size: 648 Bytes

Versions: 1

Compression:

Stored size: 648 Bytes

Contents

require "attr_setting/version"

module AttrSetting
  private

  def attr_setting(setting, default = nil)
    ivar = "@#{setting}"

    attr_writer setting

    define_method(setting) do |value = :_not_supplied|
      return instance_variable_set(ivar, value) unless value == :_not_supplied

      if instance_variable_defined?(ivar)
        instance_variable_get(ivar)
      else
        send("#{setting}!")
      end
    end

    define_method("#{setting}?") do
      !!send(setting)
    end

    define_method("#{setting}!") do
      default = instance_eval(&Proc.new) if block_given?
      instance_variable_set(ivar, default)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
attr_setting-1.0.0 lib/attr_setting.rb