From e47b366d6a96502303acecebd5e6d402b764b416 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Fri, 1 Sep 2017 14:28:06 +0200 Subject: [PATCH] Refactoring: Replaced custom implementation with Ruby core Delegator classes. --- lib/mixin/instance_wrapper.rb | 43 -------------------- lib/sequencer/units.rb | 14 +++---- lib/sequencer/units/attribute.rb | 2 +- lib/sequencer/units/attributes.rb | 66 ++++++++++++++----------------- 4 files changed, 36 insertions(+), 89 deletions(-) delete mode 100644 lib/mixin/instance_wrapper.rb diff --git a/lib/mixin/instance_wrapper.rb b/lib/mixin/instance_wrapper.rb deleted file mode 100644 index a7d8d2903..000000000 --- a/lib/mixin/instance_wrapper.rb +++ /dev/null @@ -1,43 +0,0 @@ -module Mixin - # This modules enables to redirect all calls to methods that are - # not defined to the declared instance variable. This comes handy - # when you wan't extend a Ruby core class like Hash. - # To inherit directly from such classes is a bad idea and should be avoided. - # This way allows it indirectly. - module InstanceWrapper - module ClassMethods - # Creates the class macro `wrap` that activates - # the wrapping for the given instance variable name. - # - # @param [Symbol] variable the name of the instance variable to wrap around - # - # @example - # wrap :@some_hash - # - # @return [nil] - def wrap(variable) - define_method(:instance) do - instance_variable_get(variable) - end - end - end - - def self.included(base) - base.extend(ClassMethods) - end - - private - - def method_missing(method, *args, &block) - if instance.respond_to?(method) - instance.send(method, *args, &block) - else - super - end - end - - def respond_to_missing?(method_sym, include_all) - instance.respond_to?(method_sym, include_all) - end - end -end diff --git a/lib/sequencer/units.rb b/lib/sequencer/units.rb index e926ead87..f1adb1bf3 100644 --- a/lib/sequencer/units.rb +++ b/lib/sequencer/units.rb @@ -1,11 +1,9 @@ -require 'mixin/instance_wrapper' +require 'sequencer/units/attribute' +require 'sequencer/units/attributes' class Sequencer - class Units + class Units < SimpleDelegator include ::Enumerable - include ::Mixin::InstanceWrapper - - wrap :@units # Initializes a new Sequencer::Units instance with the given Units Array. # @@ -14,7 +12,7 @@ class Sequencer # @example # Sequencer::Units.new('Example::Unit', 'Sequencer::Unit::Second::Unit') def initialize(*units) - @units = units + super(units) end # Required #each implementation for ::Enumerable functionality. Constantizes @@ -27,7 +25,7 @@ class Sequencer # # @return [nil] def each - @units.each do |unit| + __getobj__.each do |unit| yield constantize(unit) end end @@ -58,7 +56,7 @@ class Sequencer # # @return [Object] the Unit class for the requested index def [](index) - constantize(@units[index]) + constantize(__getobj__[index]) end private diff --git a/lib/sequencer/units/attribute.rb b/lib/sequencer/units/attribute.rb index 3e716bb2b..b56dcf544 100644 --- a/lib/sequencer/units/attribute.rb +++ b/lib/sequencer/units/attribute.rb @@ -1,5 +1,5 @@ class Sequencer - class Units + class Units < SimpleDelegator class Attribute attr_accessor :from, :to diff --git a/lib/sequencer/units/attributes.rb b/lib/sequencer/units/attributes.rb index f9d47aa29..9c5a72136 100644 --- a/lib/sequencer/units/attributes.rb +++ b/lib/sequencer/units/attributes.rb @@ -1,28 +1,15 @@ -require 'mixin/instance_wrapper' - class Sequencer - class Units - class Attributes - include ::Mixin::InstanceWrapper + class Units < SimpleDelegator - wrap :@attributes - - # Initializes the lifespan store for the attributes - # of the given Units declarations. - # - # @param [Array Array<:Symbol>}>] declarations the list of Unit declarations. - # - # @example - # declarations = [{uses: [:attribute1, ...], provides: [:result], ...}] - # attributes = Sequencer::Units::Attributes(declarations) - # - # @return [nil] - def initialize(declarations) - @declarations = declarations - - initialize_attributes - initialize_lifespan - end + # Initializes the lifespan store for the attributes + # of the given Units declarations. + # + # @param [Array Array<:Symbol>}>] declarations the list of Unit declarations. + # + # @example + # declarations = [{uses: [:attribute1, ...], provides: [:result], ...}] + # attributes = Sequencer::Units::Attributes(declarations) + class Attributes < Delegator # Lists all `provides` declarations of the Units the instance was initialized with. # @@ -61,24 +48,29 @@ class Sequencer key?(attribute) end - private - - def initialize_attributes - @attributes = Hash.new do |hash, key| - hash[key] = Sequencer::Units::Attribute.new - end + def __getobj__ + @attributes end - def initialize_lifespan - @declarations.each_with_index do |unit, index| - - unit[:uses].try(:each) do |attribute| - self[attribute].to = index + def __setobj__(declarations) + @attributes ||= begin + attributes = Hash.new do |hash, key| + hash[key] = Sequencer::Units::Attribute.new end - unit[:provides].try(:each) do |attribute| - next if self[attribute].will_be_provided? - self[attribute].from = index + attributes.tap do |result| + + declarations.each_with_index do |unit, index| + + unit[:uses].try(:each) do |attribute| + result[attribute].to = index + end + + unit[:provides].try(:each) do |attribute| + next if result[attribute].will_be_provided? + result[attribute].from = index + end + end end end end