Sha256: d07c6fc9a764b47db463a68d9032a90257cbac21cbc783b21705ff03689d835b

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module Arli
  module Helpers
    module Inherited

      module ClassMethods
        def set_or_get(var_name, val = nil, set_nil = false)
          var = "@#{var_name}".to_sym
          self.instance_variable_set(var, val) if val
          self.instance_variable_set(var, nil) if set_nil
          self.instance_variable_get(var)
        end

        def short_name
          name.gsub(/.*::/, '').underscore.to_sym
        end

        def attr_assignable(*attrs)
          self.class.instance_eval do
            attrs.each do |attribute|
              send(:define_method, attribute) do |val = nil, **opts|
                set_or_get(attribute, val, opts && opts[:nil])
              end
            end
          end
        end
      end

      module Subclassing
        def included(klass)
          klass.instance_eval do
            class << self
              include ::Arli::Helpers::Inherited
            end
          end
        end
      end

      def self.included(base)
        # This works for both classes and modules
        base.instance_eval do
          class << self
            include(::Arli::Helpers::Inherited::ClassMethods)
          end
        end
        base.extend(Subclassing) if base.is_a?(Class)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arli-1.4.0 lib/arli/helpers/inherited.rb
arli-1.3.0 lib/arli/helpers/inherited.rb