Sha256: d8768a83c25a9754745e457b5e870d1852fd8d687885b74a03b1180d54437f0b
Contents?: true
Size: 1 KB
Versions: 1
Compression:
Stored size: 1 KB
Contents
module ClassAttribute def self.included(base) base.extend ClassMethods end module ClassMethods # Based on `class_attribute` from Rails, without the options we don't use. # https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/class/attribute.rb def tiny_class_attribute(name, default: nil) class_methods, methods = [], [] methods << <<~RUBY def #{name} defined?(@#{name}) ? @#{name} : self.class.#{name} end RUBY class_methods << <<~RUBY def #{name}=(value) define_method(:#{name}) { value } if singleton_class? define_singleton_method(:#{name}) { value } value end RUBY methods << <<~RUBY attr_writer :#{name} RUBY location = caller_locations(1, 1).first class_eval(["class << self", *class_methods, "end", *methods].join(";").tr("\n", ";"), location.path, location.lineno) public_send(:"#{name}=", default) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
tiny_state-0.2.0 | lib/class_attribute.rb |