All Projects → alehander92 → Matchete

alehander92 / Matchete

Licence: mit
A DSL for method overloading in Ruby based on pattern matching

Programming Languages

ruby
36898 projects - #4 most used programming language
dsl
153 projects

Projects that are alternatives of or similar to Matchete

Bh
Bootstrap Helpers for Ruby
Stars: ✭ 834 (+1473.58%)
Mutual labels:  gem
Ts Pattern
🎨 A complete Pattern Matching library for TypeScript, with smart type inference.
Stars: ✭ 854 (+1511.32%)
Mutual labels:  pattern-matching
Ruby Gem Downloads Badge
Clean and simple gem downloads count badge, courtesy of http://shields.io/. You can checkout the application directly at the following URL:
Stars: ✭ 29 (-45.28%)
Mutual labels:  gem
Pcre Ocaml
OCaml bindings to PCRE (Perl Compatibility Regular Expressions)
Stars: ✭ 23 (-56.6%)
Mutual labels:  pattern-matching
Moab Versioning
Gem to process digital object version content, metadata, and manifests
Stars: ✭ 9 (-83.02%)
Mutual labels:  gem
Lumberyard Cubism3 Gem
An Amazon Lumberyard Gem that adds in Live2D Cubism3 functionality to LyShine.
Stars: ✭ 13 (-75.47%)
Mutual labels:  gem
Exception notification Shoryuken
Exception Notifier Plugin for Rails with Shoryuken http://smartinez87.github.com/exception_notification
Stars: ✭ 5 (-90.57%)
Mutual labels:  gem
Espresso.jl
Expression transformation package
Stars: ✭ 46 (-13.21%)
Mutual labels:  pattern-matching
Itamae Plugin Resource Encrypted remote file
encrypt secret data (e.g. id_rsa), and forward decrypted file to remote.
Stars: ✭ 10 (-81.13%)
Mutual labels:  gem
Administrate Field Belongs to search
Plugin that adds search capabilities to belongs_to associations for Administrate
Stars: ✭ 29 (-45.28%)
Mutual labels:  gem
Datagrid
Gem to create tables grids with sortable columns and filters
Stars: ✭ 921 (+1637.74%)
Mutual labels:  gem
Puree
Metadata extraction from the Pure Research Information System.
Stars: ✭ 8 (-84.91%)
Mutual labels:  gem
Sixarm ruby magic number type
SixArm.com » Ruby » MagicNumberType infers a data type from the data's leading bytes
Stars: ✭ 13 (-75.47%)
Mutual labels:  gem
Green Button Data
Fast Ruby parser and API client for Green Button data
Stars: ✭ 18 (-66.04%)
Mutual labels:  gem
Render async
render_async lets you include pages asynchronously with AJAX
Stars: ✭ 974 (+1737.74%)
Mutual labels:  gem
Octicons
A scalable set of icons handcrafted with <3 by GitHub
Stars: ✭ 7,039 (+13181.13%)
Mutual labels:  gem
Modern Resume Theme
A modern static resume template and theme. Powered by Jekyll and GitHub pages.
Stars: ✭ 868 (+1537.74%)
Mutual labels:  gem
Pig Ci Rails
Monitor your Ruby Applications metrics (Memory, SQL Requests & Request Time) as part of your test suite.
Stars: ✭ 53 (+0%)
Mutual labels:  gem
Ingraph
Incremental view maintenance for openCypher graph queries.
Stars: ✭ 40 (-24.53%)
Mutual labels:  pattern-matching
Lita Line
A Line adapter for Lita
Stars: ✭ 15 (-71.7%)
Mutual labels:  gem

Matchete

Matchete provides a DSL for method overloading based on pattern matching for Ruby.

Build Status

It's just a quick hack inspired by weissbier and the use-return-values-of-method-definitions DSL technique used in harmonic

It supports only ruby 2.1+

Features

  • on [:value, Integer] matches an arg with the same internal structure
  • on '#method_name' matches args responding to method_name
  • on AClass matches instances of AClass
  • on a: 2, method:... matches keyword args
  • on :test? matches with user-defined predicate methods
  • on either('#count', Array) matches if any of the tests returns true for an arg
  • on full_match('#count', '#combine') matches if all of the tests return true for an arg
  • on exact(Integer) matches special values, used as shortcuts in other cases: classes, strings starting with '#', etc
  • on having('#count' => 2) matches objects with properties with certain values
  • default matches when no match has been found in on branches

Install

gem install matchete

Usage

class Translator
  include Matchete

  on Any, :string,
  def translate(value, to)
    value.to_s
  end

  on '#[email protected]', :negative,
  def translate(value, to)
    - value
  end

  on String, :integer,
  def translate(value, to)
    value.to_i
  end

  default def translate(value, to)
    0
  end
end

t = Translator.new
p t.translate 72, :negative # -72
p t.translate nil, :integer # 0
require 'matchete'

class FactorialStrikesAgain
  include Matchete

  on 1,
  def factorial(value)
    1
  end

  on -> x { x > 1 },
  def factorial(value)
    value * factorial(value - 1)
  end
end

FactorialStrikesAgain.new.factorial(4) #24
FactorialStrikesAgain.new.factorial(-2) #Matchete::NotResolvedError No matching factorial method for args [-2]
class Converter
  include Matchete

  on '#special_convert',
  def convert(value)
    value.special_convert
  end

  on Integer,
  def convert(value)
    [:integer, value]
  end

  on Hash,
  def convert(values)
    [:dict, values.map { |k, v| [convert(k), convert(v)] }]
  end

  on /reserved_/,
  def convert(value)
    [:reserved_symbol, value]
  end

  on String,
  def convert(value)
    [:string, value]
  end

  on ['deleted', [Integer, Any]],
  def convert(value)
    ['deleted', value[1]]
  end

  on :starts_with_cat?,
  def convert(value)
    [:fail, value]
  end

  on free: Integer, method:
  def convert(free:)
    [:rofl, free]
  end

  on either('#count', Array),
  def convert(value)
    value.count
  end

  on full_match('#count', '#lala'),
  def convert(value)
    value.count + value.lala
  end

  default def convert(value)
    [:z, value]
  end

  def starts_with_cat?(value)
    value.to_s.start_with?('cat')
  end
end

class Z
  def special_convert
    [:special_convert, nil]
  end
end

converter = Converter.new
p Converter.instance_methods
p converter.convert(2) #[:integer, 2]
p converter.convert(Z.new) #[:special_convert, nil]
p converter.convert([4, 4]) # 2
p converter.convert({2 => 4}) #[:dict, [[[:integer, 2], [:integer, 4]]]
p converter.convert('reserved_l') #[:reserved_symbol, 'reserved_l']
p converter.convert('zaza') #[:string, 'zaza']
p converter.convert(['deleted', [2, Array]]) #['deleted', [2, Array]]
p converter.convert(:cat_hehe) #[:fail, :cat_hehe]
p converter.convert(free: 2) #[:rofl, 2]
p converter.convert(2.2) #[:z, 2.2]

version

0.5.1

cbb

Todo

  • Clean up the specs, right now they're a mess.
  • Fix all kinds of edge cases

Copyright

Copyright (c) 2015 Alexander Ivanov. See LICENSE for further details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].