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-0.1.70 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.69 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.68 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.67 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.66 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.65 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.64 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.63 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.62 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.61 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.60 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.59 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.58 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.57 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.56 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.55 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.54 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.53 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.52 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb
harbr-0.1.50 vendor/bundle/ruby/3.2.0/gems/sucker_punch-3.1.0/lib/sucker_punch/core_ext.rb