From f5bdd8f45fac69f150f48491a76faacbc21ff95f Mon Sep 17 00:00:00 2001 From: "Andrew Kozin (aka nepalez)" Date: Mon, 9 May 2016 13:08:10 +0300 Subject: [PATCH] Refactor arguments --- LICENSE.txt | 2 +- lib/dry/initializer.rb | 2 +- lib/dry/initializer/arguments.rb | 9 +++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/LICENSE.txt b/LICENSE.txt index 2e6bcec..f145bd8 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016 Andrew Kozin (aka nepalez) +Copyright (c) 2016 Andrew Kozin (nepalez), Vladimir Kochnev (marshall-lee) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/lib/dry/initializer.rb b/lib/dry/initializer.rb index a3a1c32..a948ec3 100644 --- a/lib/dry/initializer.rb +++ b/lib/dry/initializer.rb @@ -13,7 +13,7 @@ module Initializer def self.define(proc = nil, &block) Module.new do |container| - container.extend Dry::Initializer::Mixin + container.extend Mixin container.instance_exec(&(proc || block)) end end diff --git a/lib/dry/initializer/arguments.rb b/lib/dry/initializer/arguments.rb index 0f01361..7c58ff4 100644 --- a/lib/dry/initializer/arguments.rb +++ b/lib/dry/initializer/arguments.rb @@ -23,10 +23,7 @@ def declaration <<-RUBY attr_reader #{select(&:reader).map { |arg| ":#{arg.name}" }.join(", ")} define_method :initialize do |#{signature}| - #{assign_arguments} - #{take_declarations} - #{assign_defaults} - #{check_constraints} + #{operations.map(&method(:send)).compact.join("\n")} end RUBY end @@ -65,6 +62,10 @@ def signature (params + options).map(&:signature).join(", ") end + def operations + %w(assign_arguments take_declarations assign_defaults check_constraints) + end + def assign_arguments map(&:assignment).join("\n") end