Sha256: 167183e637c4757d24171449d1914be70f54cab92c6e1624d26cef7fca21f6a3

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

# ClassAttributes.define klass, :layout, 'default_value_optional'
# klass.layout -> get value
# klass.layout = value -> set value

# class A
#   ClassAttributes.define self, :layout, 'default'
# end
# class B < A
#   layout 'l B'
# end
# class C < B
# end
# puts A.layout # default
# puts B.layout # l B
# puts C.layout # l B

# class User
#   ClassAttributes.define_in_current_thread self, :current
# end

# User.current = User.first

module ClassAttributes
  extend self

  CA_DEFAULTS = {}

  # defines class variable
  def define klass, name, default=nil, &block
    raise ArgumentError, 'name must be symbol' unless name.class == Symbol

    default ||= block if block

    ::ClassAttributes::CA_DEFAULTS[name] = { 'Object'=>default }

    klass.define_singleton_method('%s=' % name) { |*args| send name, *args}
    klass.define_singleton_method(name) do |*args|
      root = ::ClassAttributes::CA_DEFAULTS[name]

      # set and return if argument defined
      return root[to_s] = args[0] if args.length > 0

      # find value and return
      ancestors.map(&:to_s).each do |el|
        value = root[el]
        if value || el == 'Object'
          value = instance_exec(&value) if value.is_a?(Proc)
          return value
        end
      end
    end
  end

  # defines class variable in current lux thread
  # User.current = @user
  # def current klass, name
  #   klass.class.send(:define_method, name) do |*args|
  #     Thread.current[:lux] ||= {}
  #     Thread.current[:lux]['%s-%s' % [klass, name]]
  #   end

  #   klass.class.send(:define_method, '%s=' % name) do |value|
  #     Thread.current[:lux] ||= {}
  #     Thread.current[:lux]['%s-%s' % [klass, name]] = value
  #   end
  # end
end

class Object
  def class_attribute name, default=nil, &block
    ClassAttributes.define self, name, default, &block
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lux-fw-0.2.3 ./lib/common/class_attributes.rb
lux-fw-0.2.1 ./lib/common/class_attributes.rb