Sha256: de2635f9f11894feb0479a13eab503ae06855d371a706641f1c79215d1861ba4

Contents?: true

Size: 1.07 KB

Versions: 85

Compression:

Stored size: 1.07 KB

Contents

begin
  require 'active_support/core_ext/class/attribute'
rescue LoadError

  # A dumbed down version of ActiveSupport's
  # Class#class_attribute helper.
  class Class
    def class_attribute(*attrs)
      instance_writer = true

      attrs.each do |name|
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def self.#{name}() nil end
          def self.#{name}?() !!#{name} end

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

            if singleton_class?
              class_eval do
                def #{name}
                  defined?(@#{name}) ? @#{name} : singleton_class.#{name}
                end
              end
            end
            val
          end

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

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

        attr_writer name if instance_writer
      end
    end

    private
    def singleton_class?
      ancestors.first != self
    end
  end
end


Version data entries

85 entries across 85 versions & 2 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb