Sha256: bce8acdc259de69474cbce3ce3da5999dfff277555a46daa4135001d1534833e
Contents?: true
Size: 1.73 KB
Versions: 1
Compression:
Stored size: 1.73 KB
Contents
# frozen_string_literal: true require "micro/attributes/version" require "micro/attributes/attributes_utils" require "micro/attributes/macros" require "micro/attributes/to_initialize" module Micro module Attributes def self.included(base) base.extend(::Micro::Attributes.const_get(:Macros)) base.class_eval do private_class_method :__attribute private_class_method :__attributes private_class_method :__attribute_data private_class_method :__attribute_data! private_class_method :__attributes_data end def base.inherited(subclass) self.attributes_data({}).each do |name, value| subclass.attribute(value.nil? ? name : {name => value}) end subclass.extend ::Micro::Attributes.const_get('Macros::ForSubclasses') end end def self.to_initialize @to_initialize ||= ::Micro::Attributes.const_get(:ToInitialize) end def attributes=(arg) self.class.attributes_data(AttributesUtils.hash_argument!(arg)).each do |name, value| instance_variable_set("@#{name}", value) if attribute?(name) end end protected :attributes= def attributes state = self.class.attributes.each_with_object({}) do |name, memo| memo[name] = public_send(name) if respond_to?(name) end self.class.attributes_data(state) end def attribute?(name) self.class.attribute?(name) end def attribute(name) return unless attribute?(name) value = public_send(name) block_given? ? yield(value) : value end def attribute!(name, &block) attribute(name) { |name| return block ? block[name] : name } raise NameError, "undefined attribute `#{name}" end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
u-attributes-0.8.0 | lib/micro/attributes.rb |