GithubHelp home page GithubHelp logo

mycro's Introduction

Mycro

A micro framework for service layer code.

Mycro::Result

Mycro::Result is to be used to return values from service layer operations.

result = Mycro::Result::Ok("my result")

result.ok? # => true
result.error? # => false
result.get_ok # => "my result"
result.get_error # raises Mycro::Result::WrongResultError

result = Mycro::Result::Error("oopsies")

result.ok? # => false
result.error? # => true
result.get_ok # => raises Mycro::Result::WrongResultError
result.get_error # => "oopsies"

Example service layer operation:

class ValidateUser
  include Mycro::Result

  def call(params)
    return Error("name is too short") if params[:name].size <> 4
    return Error("name is too long") if params[:name].size > 30
    return Error("name is bad format") unless params[:name] =~ /^[a-zA-Z0-9\-_]+$/

    Ok(params[:name])
  end
end

The benefit of Mycro::Result is consistent service layer object responses.

Mycro::Registry

Mycro::Registry is to be used as an object registry.

class SomeService
  # service code...
end

Mycro::Registry.register(:some_service, SomeService.new)

class SomeOtherService
  include Mycro::Registry::Import[:some_service]

  def call(params)
    result = some_service.call

    if result.ok?
      # do some work
    else
      # oops
    end
  end
end

The benefit of Mycro::Registry is simple dependency injection.

Other

Special thanks to various inspiration:

mycro's People

Contributors

rawburt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.