Giter Site home page Giter Site logo

serradura / kind Goto Github PK

View Code? Open in Web Editor NEW
37.0 3.0 5.0 435 KB

A development toolkit for Ruby with several small/cohesive abstractions to empower your development workflow - It's totally free of dependencies.

Home Page: http://rubygems.org/gems/kind

License: MIT License

Ruby 99.07% Shell 0.93%
ruby rubygem type-system type-checking activemodel-validations maybe-monad

kind's Issues

require "kind/of/stdlib"

Implement type checkers for the data types available in the stdlib like: Date, StringIO, Set, OpenStruct

Kind.of.<Type>.as_optional()

Scope:

Concept:

person_name method

data = {first_name: 'Rodrigo', last_name: 'Rodrigues'}

def person_name1(data)
  "#{data[:first_name]} #{data[:last_name]}"
end

def person_name2(data)
  data = Kind.of.Hash(data)

  "#{data[:first_name]} #{data[:last_name]}"
end

def person_name3(data)
  Kind::Optional[data].
    then { |data| "#{data[:first_name]} #{data[:last_name]}" }.
    value_or { 'John Doe' }
end

def person_name4(arg)
  hash = Kind.of.Hash.or_nil(arg)

  Kind::Optional[hash].
    then { |data| "#{data[:first_name]} #{data[:last_name]}" }.
    value_or { 'John Doe' }
end

def person_name5(arg)
  return 'John Doe' unless arg.is_a?(Hash)

  "#{data[:first_name]} #{data[:last_name]}"
end

Expectation:

def person_name5(arg)
  Kind::Of::Hash.as_optional(arg)
                .map { |data| "#{data[:first_name]} #{data[:last_name]}" }
                .value_or { 'John Doe' }
end

PersonIntroduction module

data = {first_name: 'Rodrigo', last_name: 'Rodrigues'}

module PersonIntroduction1
  extend self

  def call(data)
    "Hi my name is #{full_name(data)}, I'm #{age(data)}"
  end

  private

    def full_name(data)
      case data
      when Hash then "#{data[:first_name]} #{data[:last_name]}"
      else 'John Doe'
      end
    end

    def age(data)
      case data
      when Hash then data.fetch(:age, 0)
      else 0
      end
    end
end

module PersonIntroduction2
  extend self

  def call(data)
    "Hi my name is #{full_name(data)}, I'm #{age(data)}"
  end

  private

    def full_name(data)
      return 'John Doe' unless data.is_a?(Hash)

      "#{data[:first_name]} #{data[:last_name]}"
    end

    def age(data)
      data.is_a?(Hash) ? data.fetch(:age, 0) : 0
    end
end

module PersonIntroduction3
  extend self

  def call(arg)
    data = Kind::Optional[Kind.of.Hash.or_nil(arg)]

    "Hi my name is #{full_name(data)}, I'm #{age(data)}"
  end

  private

    def full_name(optional)
      optional.map { |data| "#{data[:first_name]} #{data[:last_name]}" }.
              value_or { 'John Doe' }
    end

    def age(optional)
      optional.map { |data| data[:age] }.
              value_or(0)
    end
end

Expectation:

module PersonIntroduction3
  extend self

  def call(arg)
    data = Kind::Of::Hash.as_optional(arg)

    "Hi my name is #{full_name(data)}, I'm #{age(data)}"
  end

  private

    def full_name(optional)
      optional.map { |data| "#{data[:first_name]} #{data[:last_name]}" }
              .value_or { 'John Doe' }
    end

    def age(optional)
      optional.map { |data| data[:age] }
              .value_or(0)
    end
end

Add Kind::Some, Kind::None and Kind.of.Numeric.instance?(*args) (Kind.of.<Type>?)

New Features

Add = -> params do
  a, b = Kind.of.Hash(params, or: Empty::HASH).values_at(:a, :b)

  return Kind::None unless Kind.of.Numeric.instance?(a, b)

  Kind::Some(a + b)
end

# --

Kind::Optional.new(a: 1, b: 2).then(&Add).value_or(0) # 3

# --

Kind::Optional.new([]).then(&Add).value_or(0) # 0
Kind::Optional.new({}).then(&Add).value_or(0) # 0
Kind::Optional.new(nil).then(&Add).value_or(0) # 0

Improve Kind::Maybe example

Add = -> params do
  a, b = Kind.of.Hash(params, or: Empty::HASH).values_at(:a, :b)

  a + b if Kind.of.Numeric?(a, b)  
end

# --

Kind::Optional.new(a: 1, b: 2).then(&Add).value_or(0) # 3

# --

Kind::Optional.new([]).then(&Add).value_or(0) # 0
Kind::Optional.new({}).then(&Add).value_or(0) # 0
Kind::Optional.new(nil).then(&Add).value_or(0) # 0

Implement Kind::Array::Of(<Type>)

  1. Kind::Array::Of(Type) will create a callable that will verify if a given value is an array containing items with the expected type.
  2. Kind::Array.of(String, value) will raise Kind::Error when the given value wasn't an array containing items with the expected type.
  3. Kind::Array.of?(String, value) will return true when the given value be an array containing items with the expected type.

Kind::Result (implement the Either monad)

Kind::Success() or Kind::Failure()

module FindUser
  CreateByIdFinder = -> (repository:) do
    lambda do |id:|
      user = repository.find_by(id: id)
  
      return Kind::Success(user: user) if user
  
      Kind::Failure(:user_not_found)
    end 
  end

  ById = CreateByIdFinder.(repository: User)
end

Kind::Result#{on_success,on_failure} hooks

FindUser::ById.(user_id: 1)
  .on_success { |result| p result }
  .on_failure { |reason| p reason }

Kind::Result#{then,map} will perform only if the result was a success

FindUser::ById.(user_id: 1)
  .then { |result| p result }

# or

FindUser::ById.(user_id: 1)
  .map { |result| p result }

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.