Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service #2

Open
blackchestnut opened this issue Oct 26, 2016 · 0 comments
Open

Service #2

blackchestnut opened this issue Oct 26, 2016 · 0 comments

Comments

@blackchestnut
Copy link
Owner

Service

# lib/service.rb
module Service
  extend ActiveSupport::Concern

  included do
    def self.call(*args)
      new(*args).call
    end
  end
end

With Attributes

# Gemfile
gem 'virtus'

# lib/attributable_service.rb
module AttributableService
  extend ActiveSupport::Concern

  included do
    include Service
    include Virtus.model
  end
end

Using

module Users
  class Create
    include AttributableService

    attribute :params, Hash
    attribute :password, String

    def call
      user.password = password
      user.save

      user
    end

  private

    def user
      @user ||= User.new(params)
    end
end
Users::Create.call(params: params, password: 'asdf')
@blackchestnut blackchestnut self-assigned this Oct 26, 2016
Repository owner locked and limited conversation to collaborators Oct 26, 2016
@blackchestnut blackchestnut removed their assignment Oct 26, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant