CI Coverage Status Gem Version Gem Downloads

Some Tools

... which tools?

Let us speculate about that:

Context: MyStruct

All the goodies of OpenStruct without the badies:

  • Immutable
  • Deconstructable (→ Patternmatchable)
  • Hashy Interface (fetch, merge, slice...)

Given an instance of MyStruct constructed from a Hash

  require 'ex_aequo/my_struct'
  let(:from_hash) { MyStruct.new(a: 1, b: 2) }

And an instance constructed by a reducer

    let(:incremented) { MyStruct.from_reduce(%i[a b c d e], 0) { _1.succ } }

Then they can be accessed like a Hash or OpenStruct

    expect(from_hash[:a]).to eq(1)
    expect(from_hash.fetch(:b)).to eq(2)
    expect(incremented[:e]).to eq(5)

And they are immutable

  expect { from_hash[:c] = 3 }
    .to raise_error(MyStruct::ImmutableError, 'cannot change values with []= in immutable instance of MyStruct')

And mutable operations just create new objects

  expect(from_hash.merge(c: 3)).to eq(MyStruct.new(a: 1, b: 2, c: 3))
  expect(from_hash.to_h).to eq(a: 1, b: 2)

And the hashy methods observer the same interface

    expect { from_hash.fetch(:c) }
      .to raise_error(KeyError, 'key :c not found in #<MyStruct a=1, b=2> and no default given to #fetch')

    expect(from_hash.fetch(:c, 42)).to eq(42)
    expect(from_hash.fetch(:c) { 43 }).to eq(43)

    expect(incremented.slice(:a, :c, :e)).to eq(MyStruct.new(a: 1, c: 3, e: 5))

Context: ArgsParser and Args

An expressive, yet simple argument parser that takes full advantage of Ruby 3 and returns a modern Args struct (with modern I mean MyStruct) as a result.

Given a simple example like this on

  let(:simple_parser) do
    ExAequo::ArgsParser.new(
      allowed: %w[alpha: beta: :help :verbose],
      aliases: {h: :help, v: :verbose, a: :alpha}
    )
  end

Then, as northing is required we can successfully parse empty arguments

  result = simple_parser.parse([])

  expect(result).to be_ok
  expect(result.missing).to be_empty
  expect(result.superflous).to be_empty
  expect(result.positionals).to be_empty
  expect(result.keywords).to eq(MyStruct.new)

And, we can also provide allowed values (N.B. ruby sytnax is default)

  result = simple_parser.parse(%w[alpha: 42 43])

  expect(result).to be_ok
  expect(result.missing).to be_empty
  expect(result.superflous).to be_empty
  expect(result.positionals).to eq(%w[43])
  expect(result.keywords).to eq(MyStruct.new(alpha: '42'))

Other Tools are described here

LICENSE

Copyright 202[2,3] Robert Dober [email protected]

Apache-2.0 c.f LICENSE