Sha256: 2c600d0c08007059e7362ba46e2934838033a74b430883e00094110736fca261
Contents?: true
Size: 1.59 KB
Versions: 12
Compression:
Stored size: 1.59 KB
Contents
# -*- coding: UTF-8 -*- # A mixin that implements inheriting values from a parent module. The parent object can be set in # two ways. # # object.parent = parent # object.parent = lambda { some_function_returning_parent() } # # Set the inherited values with # # class MyClass # # include GetValuesFromParent # # inheritated_attr_accessor :myattr # inheritated_attr_accessor :myotherattr. # # end module MJ module Mixins module InheritedAttributes # Class methods module ClassMethods def inherited_attr_accessor( name ) class_eval <<-EOF attr_writer :#{name} def #{name} return @#{name} if @#{name} return parent.#{name} if @parent return nil end EOF end end # Instance methods module InstanceMethods def initialize( *args ) @parent = nil end # Set the parent object. attr_writer :parent # Get the parent object. def parent return nil if not @parent if @parent.is_a?( Proc ) @parent.call() else @parent end end end # Include def self.included( receiver ) receiver.extend ClassMethods receiver.send :include, InstanceMethods end end # module InheritedAttributes end end # module MJ::InheritedAttributes
Version data entries
12 entries across 12 versions & 1 rubygems