Sha256: cc824f638e6cfe2e3b9cadfd35e4a0be56efe02b9affc4f47981a0f48d3f6962

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/module/aliasing'

class Class
  # Purpose is to be able to define a default value for a class_attribute when one isn't initially set.
  def class_attribute_with_default(*attrs) # Actually, this IS being used currently by gems in lib/
    hash = attrs.last.is_a?(Hash) ? attrs.pop : {}
    my_default = hash.delete(:default)
    instance_writer = hash.blank? || hash[:instance_writer]

    attrs.each do |name| # By the way, FIXME: i think this should be broken because if i want to use a string as a default, that will not be reflected.
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def self.#{name}() #{my_default || 'nil'} end
        def self.#{name}?() !!#{name} end

        def self.#{name}=(val)
          singleton_class.class_eval do
            remove_possible_method(:#{name})
            define_method(:#{name}) { val }
          end
          val
        end

        def #{name}
          defined?(@#{name}) ? @#{name} : singleton_class.#{name}
        end

        def #{name}?
          !!#{name}
        end
      RUBY

      attr_writer name if instance_writer
    end
  end
  alias_method_chain :class_attribute, :default
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
core_utilities-0.1.3 lib/core_utilities/core_ext/class.rb