Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

inheritable_instance

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

inheritable_instance

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

InheritableInstance

Allow descending classes to inherit instance variables. This is useful for persisting class-level configuration.

Installation

Add this line to your application's Gemfile:

gem 'inheritable_instance'

Usage

Insert the InheritableInstance module into the ancestry chain of your class:

class FakeClass
  extend InheritableInstance
end

Then use inheritable_instance to set any instance variables that you want to be inherited by subclasses:

class FakeClass
  inheritable_instance :@thing_one, 1
  inheritable_instance :@thing_two, 2
end

These variables will be automatically set on inheritance:

class DoubleFake < FakeClass; end
DoubleFake.instance_variable_get(:@thing_one) == 1
DoubleFake.instance_variable_get(:@thing_two) == 2

It is convenient to pair this with the attr_* methods:

class << FakeClass
  attr_accessor :thing_two
end
DoubleFake.thing_two = "two"
FakeClass.thing_two == 2
DoubleFake.thing_two == "two"

Note that the values are duplicated (when possible):

class OneTwoThree
  extend InheritableInstance
  inheritable_instance :@list, []
  class << self; attr_reader :list; end
end
OneTwoThree.list << 1 << 2 << 3
Abc = Class.new(OneTwoThree)
Abc.list << 'a' << 'b' << 'c'
Abc.list == [1,2,3,'a','b','c']
OneTwoThree.list == [1,2,3]

Additional Details

If for some reason it is inconvenient to extend the module, there is an Includible helper that you can use instead:

class SameAsFakeClass
  include InheritableInstance::Includible
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/fledman/inheritable_instance.

License

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

FAQs

Package last updated on 31 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc