Sha256: 7fb48956591d07a9a2b1a44eefb985eb49ebac5ba1e79007b1534735e7d07cb8

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 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)
        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

6 entries across 6 versions & 1 rubygems

Version Path
arli-1.2.1 lib/arli/helpers/inherited.rb
arli-1.1.0 lib/arli/helpers/inherited.rb
arli-1.0.2 lib/arli/helpers/inherited.rb
arli-1.0.1 lib/arli/helpers/inherited.rb
arli-1.0.0 lib/arli/helpers/inherited.rb
arli-0.9.0 lib/arli/helpers/inherited.rb