Sha256: 773ac5b43e142a97f36feed71107f1c65631e11945070ba2cccd3cdf9761b344

Contents?: true

Size: 996 Bytes

Versions: 2

Compression:

Stored size: 996 Bytes

Contents

module WrapIt
  #
  # Adds minimal support to retrieve derived class variables
  #
  # @author Alexey Ovchinnikov <alexiss@cybernetlab.ru>
  #
  module DerivedAttributes
    def self.included(base)
      base.extend ClassMethods
    end

    #
    # Class methods to include
    #
    module ClassMethods
      def get_derived(name)
        return instance_variable_get(name) if instance_variable_defined?(name)
        ancestors.each do |ancestor|
          break if ancestor == Base
          next unless ancestor.instance_variable_defined?(name)
          return ancestor.instance_variable_get(name)
        end
        nil
      end

      def collect_derived(name, initial = [], method = :concat)
        result = initial
        ancestors.each do |ancestor|
          break if ancestor == Base
          next unless ancestor.instance_variable_defined?(name)
          result = result.send(method, ancestor.instance_variable_get(name))
        end
        result
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wrap_it-0.1.1 lib/wrap_it/derived_attributes.rb
wrap_it-0.1.0 lib/wrap_it/derived_attributes.rb