Refactoring: Replaced custom implementation with Ruby core Delegator classes.
This commit is contained in:
parent
b23f0d21d5
commit
e47b366d6a
4 changed files with 36 additions and 89 deletions
|
@ -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
|
|
|
@ -1,11 +1,9 @@
|
||||||
require 'mixin/instance_wrapper'
|
require 'sequencer/units/attribute'
|
||||||
|
require 'sequencer/units/attributes'
|
||||||
|
|
||||||
class Sequencer
|
class Sequencer
|
||||||
class Units
|
class Units < SimpleDelegator
|
||||||
include ::Enumerable
|
include ::Enumerable
|
||||||
include ::Mixin::InstanceWrapper
|
|
||||||
|
|
||||||
wrap :@units
|
|
||||||
|
|
||||||
# Initializes a new Sequencer::Units instance with the given Units Array.
|
# Initializes a new Sequencer::Units instance with the given Units Array.
|
||||||
#
|
#
|
||||||
|
@ -14,7 +12,7 @@ class Sequencer
|
||||||
# @example
|
# @example
|
||||||
# Sequencer::Units.new('Example::Unit', 'Sequencer::Unit::Second::Unit')
|
# Sequencer::Units.new('Example::Unit', 'Sequencer::Unit::Second::Unit')
|
||||||
def initialize(*units)
|
def initialize(*units)
|
||||||
@units = units
|
super(units)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Required #each implementation for ::Enumerable functionality. Constantizes
|
# Required #each implementation for ::Enumerable functionality. Constantizes
|
||||||
|
@ -27,7 +25,7 @@ class Sequencer
|
||||||
#
|
#
|
||||||
# @return [nil]
|
# @return [nil]
|
||||||
def each
|
def each
|
||||||
@units.each do |unit|
|
__getobj__.each do |unit|
|
||||||
yield constantize(unit)
|
yield constantize(unit)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -58,7 +56,7 @@ class Sequencer
|
||||||
#
|
#
|
||||||
# @return [Object] the Unit class for the requested index
|
# @return [Object] the Unit class for the requested index
|
||||||
def [](index)
|
def [](index)
|
||||||
constantize(@units[index])
|
constantize(__getobj__[index])
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
class Sequencer
|
class Sequencer
|
||||||
class Units
|
class Units < SimpleDelegator
|
||||||
class Attribute
|
class Attribute
|
||||||
|
|
||||||
attr_accessor :from, :to
|
attr_accessor :from, :to
|
||||||
|
|
|
@ -1,11 +1,5 @@
|
||||||
require 'mixin/instance_wrapper'
|
|
||||||
|
|
||||||
class Sequencer
|
class Sequencer
|
||||||
class Units
|
class Units < SimpleDelegator
|
||||||
class Attributes
|
|
||||||
include ::Mixin::InstanceWrapper
|
|
||||||
|
|
||||||
wrap :@attributes
|
|
||||||
|
|
||||||
# Initializes the lifespan store for the attributes
|
# Initializes the lifespan store for the attributes
|
||||||
# of the given Units declarations.
|
# of the given Units declarations.
|
||||||
|
@ -15,14 +9,7 @@ class Sequencer
|
||||||
# @example
|
# @example
|
||||||
# declarations = [{uses: [:attribute1, ...], provides: [:result], ...}]
|
# declarations = [{uses: [:attribute1, ...], provides: [:result], ...}]
|
||||||
# attributes = Sequencer::Units::Attributes(declarations)
|
# attributes = Sequencer::Units::Attributes(declarations)
|
||||||
#
|
class Attributes < Delegator
|
||||||
# @return [nil]
|
|
||||||
def initialize(declarations)
|
|
||||||
@declarations = declarations
|
|
||||||
|
|
||||||
initialize_attributes
|
|
||||||
initialize_lifespan
|
|
||||||
end
|
|
||||||
|
|
||||||
# Lists all `provides` declarations of the Units the instance was initialized with.
|
# Lists all `provides` declarations of the Units the instance was initialized with.
|
||||||
#
|
#
|
||||||
|
@ -61,24 +48,29 @@ class Sequencer
|
||||||
key?(attribute)
|
key?(attribute)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def __getobj__
|
||||||
|
@attributes
|
||||||
|
end
|
||||||
|
|
||||||
def initialize_attributes
|
def __setobj__(declarations)
|
||||||
@attributes = Hash.new do |hash, key|
|
@attributes ||= begin
|
||||||
|
attributes = Hash.new do |hash, key|
|
||||||
hash[key] = Sequencer::Units::Attribute.new
|
hash[key] = Sequencer::Units::Attribute.new
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def initialize_lifespan
|
attributes.tap do |result|
|
||||||
@declarations.each_with_index do |unit, index|
|
|
||||||
|
declarations.each_with_index do |unit, index|
|
||||||
|
|
||||||
unit[:uses].try(:each) do |attribute|
|
unit[:uses].try(:each) do |attribute|
|
||||||
self[attribute].to = index
|
result[attribute].to = index
|
||||||
end
|
end
|
||||||
|
|
||||||
unit[:provides].try(:each) do |attribute|
|
unit[:provides].try(:each) do |attribute|
|
||||||
next if self[attribute].will_be_provided?
|
next if result[attribute].will_be_provided?
|
||||||
self[attribute].from = index
|
result[attribute].from = index
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue