No commit activity in last 3 years
No release in over 3 years
Let inherited class var to authorize inheritance
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

InheritedClassVar Build StatusCode ClimateDependency StatusCoverage StatusGem Version

Implement class variables that inherit from their ancestors. Such as a Hash:

require 'inherited_class_var'

class Bird
  include InheritedClassVar
  inherited_class_hash :attributes #, shallow: false, reverse: false (default aoptions)
  
  def self.attribute(attribute_name, options={})
    attributes_object.merge(attribute_name.to_sym => options)
  end
  attribute :name, upcase: true
end

class Duck < Bird
  attribute :flying, default: false
end

Bird.attributes # => { name: upcase: true }
Duck.attributes # => { name: upcase: true, flying: false }

Installation

Add this line to your application's Gemfile:

gem 'inherited_class_var'

And then execute:

$ bundle

Or install it yourself as:

$ gem install inherited_class_var

Usage

You can also define your own variable types. This is the source for Hash:

module InheritedClassVar
  class Hash < Variable
    alias_method :merge, :change

    def default_value
      {}
    end

    def _change(hash1, hash2)
      method = options[:shallow] ? :merge! : :deep_merge!
      block = options[:reverse] ? Proc.new {|key,left,right| left }  : Proc.new {|key,left,right| right }
      hash1.public_send(method, hash2, &block)
    end
  end
end

module InheritedClassVar
    def inherited_class_hash(variable_name, options={})
      inherited_class_var variable_name, InheritedClassVar::Hash, options
    end
end

License

The gem is available as open source under the terms of the MIT License.