{"id":288916,"date":"2018-09-03T17:15:12","date_gmt":"2018-09-03T13:15:12","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=288916"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=288916","title":{"rendered":"\u041f\u043e\u043d\u0438\u0436\u0430\u0435\u043c \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e DI \u0434\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u0438 \u043a\u043e\u0434\u0430, \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438"},"content":{"rendered":"\n<div data-io-article-url=\"https:\/\/habr.com\/post\/422161\/\" class=\"post__text post__text-html js-mediator-article\">\n<p>\u0412 \u043d\u0430\u0447\u0430\u043b\u044c\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0445\u043e\u0447\u0443 \u0441\u0440\u0430\u0437\u0443 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044f \u043d\u0435 \u043f\u0440\u0435\u0442\u0435\u043d\u0434\u0443\u044e \u043d\u0430 \u043d\u043e\u0432\u0438\u0437\u043d\u0443, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f\/\u043d\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043a\u0430\u043a IoC DI.<\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u0447\u0442\u0438 \u043d\u0435\u0442 \u043e\u043f\u044b\u0442\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0435\u0439, \u044d\u0442\u043e \u043c\u043e\u044f \u043f\u0435\u0440\u0432\u0430\u044f. \u042f \u0441\u0442\u0430\u0440\u0430\u043b\u0441\u044f \u043a\u0430\u043a \u043c\u043e\u0433, \u0435\u0441\u043b\u0438 \u0447\u0442\u043e \u043d\u0435 \u0441\u0443\u0434\u0438\u0442\u0435 \u0441\u0442\u0440\u043e\u0433\u043e. <\/p>\n<p>  <\/p>\n<h2 id=\"o-chem-voobsche-rech\">\u041e \u0447\u0435\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0435\u0447\u044c<\/h2>\n<p>  <\/p>\n<p>\u0411\u043e\u043b\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 \u043d\u0430 Rails, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u044f \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0441\u044f, \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u043d\u0443 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443. \u041e\u043d\u0438 \u043b\u0438\u0431\u043e \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u0442\u0435\u0441\u0442\u043e\u0432 \u0432\u043e\u0432\u0441\u0435, \u043b\u0438\u0431\u043e \u0438\u0445 \u0442\u0435\u0441\u0442\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442 \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u043d\u0435\u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u0442\u0438\u0445 \u0442\u0435\u0441\u0442\u043e\u0432 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0436\u0435\u043b\u0430\u0442\u044c \u043b\u0443\u0447\u0448\u0435\u0433\u043e.<\/p>\n<p>  <\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u043f\u0440\u0438\u0447\u0438\u043d\u0430 \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u0442\u0430\u043a, \u0447\u0442\u043e \u0431\u044b \u0432 unit-\u0442\u0435\u0441\u0442\u0430\u0445 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0438\u043c \u043a\u043e\u0434, \u0430 \u043d\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0432 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u0438\u043d\u043e\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u043d\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435 \u0438\u043b\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430 \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043b\u043e\u0433\u0438\u0447\u043d\u0430\u044f \u0446\u0435\u043f\u043e\u0447\u043a\u0430, \u0430 \u0437\u0430\u0447\u0435\u043c \u043c\u043d\u0435 \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u044b\u043d\u043e\u0441\u0438\u0442\u044c \u043a\u043e\u0434 \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0438 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u043b\u043e\u0439, \u044f \u0436\u0435 \u0442\u0443\u0442 \u0432\u0441\u0435\u0433\u043e \u043f\u0430\u0440\u0443 \u0441\u0442\u0440\u043e\u043a \u0434\u043e\u0431\u0430\u0432\u043b\u044e \u0438 \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c \u0437\u0430\u043a\u0430\u0437\u0447\u0438\u043a\u0430.<\/p>\n<p>  <\/p>\n<p>\u0418 \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043b\u043e\u0445\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043c\u043e\u0434\u0443\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0435\u0440\u044f\u0435\u0442 \u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e\u0441\u0442\u044c \u043a \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433\u0443, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u043c\u0438 \u0432 \u0442\u0430\u043a\u043e\u043c \u043a\u043e\u0434\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0442\u044f\u0436\u0435\u043b\u043e. \u042d\u043d\u0442\u0440\u043e\u043f\u0438\u044f \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u0440\u0430\u0441\u0442\u0435\u0442. \u0410 \u0435\u0441\u043b\u0438 \u0432\u044b \u0443\u0436\u0435 \u0431\u043e\u0438\u0442\u0435\u0441\u044c \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u043a\u043e\u0434, \u0434\u0435\u043b\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u043b\u043e\u0445\u0438.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432 \u043c\u0438\u0440\u0435 Java \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u044f\u0434 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0438 \u043d\u0435\u0442 \u043e\u0441\u043e\u0431\u043e\u0433\u043e \u0441\u043c\u044b\u0441\u043b\u0430 \u0438\u0437\u043e\u0431\u0440\u0435\u0442\u0430\u0442\u044c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434, \u0445\u043e\u0442\u044f, \u043d\u0430\u0434\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432\u0435\u0441\u044c\u043c\u0430 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u0435 \u0438 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u0447\u0438\u043d\u0430 \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c. \u0420\u0443\u0431\u0438\u0441\u0442\u044b \u0432\u0438\u0434\u0438\u043c\u043e \u043a\u0430\u043a-\u0442\u043e \u0438\u043d\u0430\u0447\u0435 \u0440\u0435\u0448\u0430\u044e\u0442 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u043e \u044f, \u0447\u0435\u0441\u0442\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0442\u0430\u043a \u0438 \u043d\u0435 \u043f\u043e\u043d\u044f\u043b \u043a\u0430\u043a. \u041f\u043e \u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0435\u0448\u0438\u043b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044f.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2 id=\"obschaya-ideya-kak-reshat-eto-v-ruby-proektah\">\u041e\u0431\u0449\u0430\u044f \u0438\u0434\u0435\u044f \u043a\u0430\u043a \u0440\u0435\u0448\u0430\u0442\u044c \u044d\u0442\u043e \u0432 ruby \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445<\/h2>\n<p>  <\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438\u043c\u0435\u044e\u0449\u0438\u043c\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u043c\u0435\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u043c\u0438.<\/p>\n<p>  <\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">class UserService   def initialize()     @notification_service = NotificationService.new   end    def block_user(user)     user.block!     @notification_service.send(user, 'you have been blocked')   end end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 block_user \u043c\u044b \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u043c \u043d\u0430 \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u0432\u0435\u0434\u044c y \u043d\u0430\u0441 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 notify \u0438\u0437 NotificationService \u0438 \u043c\u044b \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u043d\u044b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434.<br \/>  \u0418\u043d\u0432\u0435\u0440\u0441\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0442\u0430\u043a\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u0435\u0441\u043b\u0438 \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c UserService, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">class UserService   def initialize(notification_service = NotificationService.new)     @notification_service = notification_service   end    def block_user(user)     user.block!     @notification_service.send(user, 'you have been blocked')   end end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043c\u044b \u043f\u043e\u0434\u0430\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 NotificationService mock \u043e\u0431\u044a\u0435\u043a\u0442, \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e block_user \u0434\u0435\u0440\u0433\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u044b notification_service \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u0441 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438.<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">RSpec.describe UserService, type: :service do   let (:notification_service) { instance_double(NotificationService) }   let (:service) { UserService.new(notification_service) }    describe &quot;.block_user&quot; do     let (:user) { instance_double(User) }      it &quot;should block user and send notification&quot; do       expect(user).to receive :block!       expect(notification_service).to receive(:send).with(user, &quot;you have been blocked&quot;)        service.block_user(user)     end   end end<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"konkretnyy-primer-dlya-rails\">\u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f rails<\/h2>\n<p>  <\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043c\u043d\u043e\u0433\u043e, \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u043c\u0443 \u0432\u0441\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0442\u0440\u0443\u0434\u043d\u043e, \u043a\u043e\u0434 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u043e\u0431\u0440\u0430\u0441\u0442\u0430\u0442\u044c \u043b\u0438\u0448\u043d\u0438\u043c\u0438 \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438 \u043a\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043d\u0438\u0436\u0430\u044e\u0442 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c.<\/p>\n<p>  <\/p>\n<p>\u0412 \u0441\u0432\u044f\u0437\u0438 \u0441 \u044d\u0442\u0438\u043c \u043f\u0440\u0438\u0448\u043b\u0430 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043c\u043e\u0434\u0443\u043b\u044c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044f\u043c\u0438.<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">module Services   module Injector     def self.included(base)       # TODO: check base, should be controller or service       base.extend ClassMethods     end      module ClassMethods       def inject_service(name)         service = Services::Helpers::Service.new(name)          attr_writer service.to_s          define_method service.to_s do           instance_variable_get(&quot;@#{service.to_s}&quot;).tap { |s| return s if s }           instance_variable_set(&quot;@#{service.to_s}&quot;, service.instance)         end       end     end   end    module Helpers     class Service       def initialize(name)         raise ArgumentError, 'name of service should be a Symbol' unless name.is_a? Symbol          @name = name.to_s.downcase         @class = &quot;#{@name.camelcase}Service&quot;.constantize          unless @class.respond_to? :instance           raise ArgumentError, &quot;#{@name.to_s} should be singleton (respond to instance method)&quot;         end       end        def to_s         &quot;#{@name}_service&quot;       end        def instance         if Rails.env.test?           if defined? RSpec::Mocks::ExampleMethods             extend RSpec::Mocks::ExampleMethods             instance_double @class           else             nil           end         else           @class.instance         end       end     end   end end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0443\u0442 \u0435\u0441\u0442\u044c \u043e\u0434\u0438\u043d \u043d\u044e\u0430\u043d\u0441, \u0441\u0435\u0440\u0432\u0438\u0441 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c Singleton, \u0442.\u0435. \u0438\u043c\u0435\u0442\u044c \u043c\u0435\u0442\u043e\u0434 instance. \u041f\u0440\u043e\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0432 <code>include Singleton<\/code> \u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435.<\/p>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432 ApplicationController \u0434\u043e\u0431\u0430\u0432\u0438\u043c<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">require 'services'  class ApplicationController &lt; ActionController::Base   include Services::Injector end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u0445 \u043c\u043e\u0436\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">class WelcomeController &lt; ApplicationController   inject_service :welcome    def index     render plain: welcome_service.text   end end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0441\u043f\u0435\u043a\u0435 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u043c\u044b \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u043c instance_double(WelcomeService) \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0441\u043e\u043c\u043e\u0441\u0442\u0438.<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">RSpec.describe WelcomeController, type: :controller do   describe &quot;index&quot; do     it &quot;should render text from test service&quot; do       allow(controller.welcome_service).to receive(:text).and_return &quot;OK&quot;        get :index        expect(response).to have_attributes body: &quot;OK&quot;       expect(response).to have_http_status 200     end   end end<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"chto-mozhno-uluchshit\">\u0427\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c<\/h2>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e \u0432 \u043d\u0430\u0448\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u043e\u0447\u044c\u044e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440, \u0430 \u0434\u043d\u0435\u043c \u0434\u0440\u0443\u0433\u043e\u0439. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u044b \u0438\u043c\u0435\u044e\u0442 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u0440\u0430\u0437\u043d\u044b\u0435 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438.<\/p>\n<p>  <\/p>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 NotificationService \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u0435\u043c \u0436\u0435, \u043d\u043e \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/p>\n<p>  <\/p>\n<pre><code>class NightlyNotificationService &lt; NotificationService end class DailyNotificationService &lt; NotificationService end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0439 \u043c\u0430\u043f\u043f\u0438\u043d\u0433 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432<\/p>\n<p>  <\/p>\n<pre><code class=\"ruby\">class NotificationServiceMapper   include Singleton    def take     now = Time.now      ((now.hour &gt;= 00) and (now.hour &lt;= 8)) ? NightlyNotificationService : DailyNotificationService   end end<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0431\u0435\u0440\u0435\u043c \u0438\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u044e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0432 Services::Helpers::Service.instance \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0435\u0441\u0442\u044c \u043b\u0438 *Mapper \u043e\u0431\u044a\u0435\u043a\u0442, \u0438 \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c, \u0442\u043e \u0432\u0437\u044f\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0443 \u043a\u043b\u0430\u0441\u0441\u0430 \u0447\u0435\u0440\u0435\u0437 take.<\/p>\n<\/div>\n<p>        <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/post\/422161\/\"> https:\/\/habr.com\/post\/422161\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div data-io-article-url=\"https:\/\/habr.com\/post\/422161\/\" class=\"post__text post__text-html js-mediator-article\">\n<p>\u0412 \u043d\u0430\u0447\u0430\u043b\u044c\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0445\u043e\u0447\u0443 \u0441\u0440\u0430\u0437\u0443 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044f \u043d\u0435 \u043f\u0440\u0435\u0442\u0435\u043d\u0434\u0443\u044e \u043d\u0430 \u043d\u043e\u0432\u0438\u0437\u043d\u0443, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f\/\u043d\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043e \u0442\u0430\u043a\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043a\u0430\u043a IoC DI.<\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u0447\u0442\u0438 \u043d\u0435\u0442 \u043e\u043f\u044b\u0442\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0435\u0439, \u044d\u0442\u043e \u043c\u043e\u044f \u043f\u0435\u0440\u0432\u0430\u044f. \u042f \u0441\u0442\u0430\u0440\u0430\u043b\u0441\u044f \u043a\u0430\u043a \u043c\u043e\u0433, \u0435\u0441\u043b\u0438 \u0447\u0442\u043e \u043d\u0435 \u0441\u0443\u0434\u0438\u0442\u0435 \u0441\u0442\u0440\u043e\u0433\u043e. <\/p>\n<p>  <\/p>\n<h2 id=\"o-chem-voobsche-rech\">\u041e \u0447\u0435\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0435\u0447\u044c<\/h2>\n<p>  <\/p>\n<p>\u0411\u043e\u043b\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 \u043d\u0430 Rails, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u044f \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0441\u044f, \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u043d\u0443 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443. \u041e\u043d\u0438 \u043b\u0438\u0431\u043e \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u0442\u0435\u0441\u0442\u043e\u0432 \u0432\u043e\u0432\u0441\u0435, \u043b\u0438\u0431\u043e \u0438\u0445 \u0442\u0435\u0441\u0442\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442 \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u043d\u0435\u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u0442\u0438\u0445 \u0442\u0435\u0441\u0442\u043e\u0432 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0436\u0435\u043b\u0430\u0442\u044c \u043b\u0443\u0447\u0448\u0435\u0433\u043e.<\/p>\n<p>  <\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u043f\u0440\u0438\u0447\u0438\u043d\u0430 \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u0442\u0430\u043a, \u0447\u0442\u043e \u0431\u044b \u0432 unit-\u0442\u0435\u0441\u0442\u0430\u0445 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0438\u043c \u043a\u043e\u0434, \u0430 \u043d\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0432 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u0438\u043d\u043e\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u043d\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435 \u0438\u043b\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0430 \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043b\u043e\u0433\u0438\u0447\u043d\u0430\u044f \u0446\u0435\u043f\u043e\u0447\u043a\u0430, \u0430 \u0437\u0430\u0447\u0435\u043c \u043c\u043d\u0435 \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u044b\u043d\u043e\u0441\u0438\u0442\u044c \u043a\u043e\u0434 \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0438 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u043b\u043e\u0439, \u044f \u0436\u0435 \u0442\u0443\u0442 \u0432\u0441\u0435\u0433\u043e \u043f\u0430\u0440\u0443 \u0441\u0442\u0440\u043e\u043a \u0434\u043e\u0431\u0430\u0432\u043b\u044e \u0438 \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c \u0437\u0430\u043a\u0430\u0437\u0447\u0438\u043a\u0430.<\/p>\n<p>  <\/p>\n<p>\u0418 \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043b\u043e\u0445\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043c\u043e\u0434\u0443\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0435\u0440\u044f\u0435\u0442 \u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e\u0441\u0442\u044c \u043a \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433\u0443, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u043c\u0438 \u0432 \u0442\u0430\u043a\u043e\u043c \u043a\u043e\u0434\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0442\u044f\u0436\u0435\u043b\u043e. \u042d\u043d\u0442\u0440\u043e\u043f\u0438\u044f \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u0440\u0430\u0441\u0442\u0435\u0442. \u0410 \u0435\u0441\u043b\u0438 \u0432\u044b \u0443\u0436\u0435 \u0431\u043e\u0438\u0442\u0435\u0441\u044c \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u043a\u043e\u0434, \u0434\u0435\u043b\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u043b\u043e\u0445\u0438.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432 \u043c\u0438\u0440\u0435 Java \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u044f\u0434 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0438 \u043d\u0435\u0442 \u043e\u0441\u043e\u0431\u043e\u0433\u043e \u0441\u043c\u044b\u0441\u043b\u0430 \u0438\u0437\u043e\u0431\u0440\u0435\u0442\u0430\u0442\u044c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434, \u0445\u043e\u0442\u044f, \u043d\u0430\u0434\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432\u0435\u0441\u044c\u043c\u0430 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u0435 \u0438 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u0447\u0438\u043d\u0430 \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c. \u0420\u0443\u0431\u0438\u0441\u0442\u044b \u0432\u0438\u0434\u0438\u043c\u043e \u043a\u0430\u043a-\u0442\u043e \u0438\u043d\u0430\u0447\u0435 \u0440\u0435\u0448\u0430\u044e\u0442 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u043e \u044f, \u0447\u0435\u0441\u0442\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0442\u0430\u043a \u0438 \u043d\u0435 \u043f\u043e\u043d\u044f\u043b \u043a\u0430\u043a. \u041f\u043e \u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0435\u0448\u0438\u043b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044f.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-288916","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/288916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=288916"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/288916\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=288916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=288916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=288916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}