0.01
No release in over 3 years
Low commit activity in last 3 years
Smart implementation of the most-used programming pattern - Service Object
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.2
~> 0.13
~> 13.0
~> 3.10
~> 0.21

Runtime

 Project Readme

SmartCore::Operation · Supported by Cado Labs · Gem Version

Smart implementation of the most-used programming pattern - Service Object. Powered by smart_injection, smart_container, smart_types and smart_initializer.

previous generation: https://github.com/0exp/smart_core


Supported by Cado Labs


Main features


Installation

gem 'smart_operation'
bundle install
# --- or ---
gem install smart_types
require 'smart_core/operation'

Synopsis

class CreateUser < SmartCore::Operation
  register_container(ReposContainer)

  import({ user_repo: 'business.users' }, access: :private)

  option :name, 'value.string'
  option :password, 'value.string'
  option :age, 'value.integer'

  def call
    user = user_repo.create({ name: name, password: password, age: age })
    Success(user: user) # or Callback { puts 'wow o.O' }
  end
end

CreateUser.call(name: 'Rustam', password: 'test123', age: 28) do |result|
  result.success?  { puts 'success!' }
  result.failure?  { puts 'failure!' }
  result.fatal?    { puts 'fatal!' }
  result.callback? { result.call } # or some_object.instance_eval(&result.callback)
end # NOTE: returns <result> object, but invokes the correspinding block

# COMING SOON:
CreateUser.exec(name: 'Rustam', password: 'test123', age: 28) do |result|
  # ...the same code as above...
end # NOTE: returns the result of the corresponding invoked block object

Pre-requisits:

class UserRepo
  def create(user_creds)
     # ... some code
  end
end

ReposContainer = SmartCore::Container.define do
  namespace(:business) do
    register(:users) { UserRepo.new }
  end
end

Roadmap

  • method memoization logic (with abilities to refresh memoized methods);
  • pattern matching for result objects (Success, Failure, Fatal, Callback);
  • migrate to Github Actions;
  • parametrized .call methods (ServiceObject.with(attrs_a).call(attrs_b) => ServiceObject.new(attrs_b).call(attrs_a) or something another);
  • think about default exception classes and interfaces to support a basic domain-specific exception class definitioning (common practice);
  • generators: an ability to invoke result block in repeatable yield-like style 🤔 (think about Fibers, js-like generators and similar) (for example: .generator(call_attrs) do |result| # repeatable result block invokation for each "yield" from an inside "generator" method);

Build

  • run tests:
bundle exec rspec
  • run code style checks:
bundle exec rubocop
  • run code style checks with auto-correction:
bundle exec rubocop -A

Contributing

  • Fork it ( https://github.com/smart-rb/smart_operation )
  • Create your feature branch (git checkout -b feature/my-new-feature)
  • Commit your changes (git commit -am '[feature_context] Add some feature')
  • Push to the branch (git push origin feature/my-new-feature)
  • Create new Pull Request

License

Released under MIT License.

Supporting

Supported by Cado Labs

Authors

Rustam Ibragimov