From f6970c41ea678709de0093bb6e28c7250173b395 Mon Sep 17 00:00:00 2001 From: Omkar Moghe Date: Sat, 23 Mar 2024 19:27:37 -0700 Subject: [PATCH] init --- .github/workflows/main.yml | 27 +++++++++ .gitignore | 8 +++ .rubocop.yml | 8 +++ CHANGELOG.md | 4 ++ Gemfile | 12 ++++ LICENSE.txt | 21 +++++++ README.md | 116 +++++++++++++++++++++++++++++++++++++ Rakefile | 12 ++++ bin/console | 11 ++++ bin/setup | 8 +++ expressive.gemspec | 40 +++++++++++++ lib/expressive.rb | 8 +++ lib/expressive/version.rb | 5 ++ sig/expressive.rbs | 4 ++ test/test_expressive.rb | 13 +++++ test/test_helper.rb | 6 ++ 16 files changed, 303 insertions(+) create mode 100644 .github/workflows/main.yml create mode 100644 .gitignore create mode 100644 .rubocop.yml create mode 100644 CHANGELOG.md create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100755 bin/console create mode 100755 bin/setup create mode 100644 expressive.gemspec create mode 100644 lib/expressive.rb create mode 100644 lib/expressive/version.rb create mode 100644 sig/expressive.rbs create mode 100644 test/test_expressive.rb create mode 100644 test/test_helper.rb diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..c81a1ae --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,27 @@ +name: Ruby + +on: + push: + branches: + - main + + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + name: Ruby ${{ matrix.ruby }} + strategy: + matrix: + ruby: + - '3.2.0' + + steps: + - uses: actions/checkout@v4 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - name: Run the default task + run: bundle exec rake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9106b2a --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..762eebb --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,8 @@ +AllCops: + TargetRubyVersion: 3.0 + +Style/StringLiterals: + EnforcedStyle: double_quotes + +Style/StringLiteralsInInterpolation: + EnforcedStyle: double_quotes diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..7c8b442 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +## [Unreleased] + +## [0.1.0] - 2024-03-23 +- Initial release diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..366e198 --- /dev/null +++ b/Gemfile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# Specify your gem's dependencies in expressive.gemspec +gemspec + +gem "rake", "~> 13.0" + +gem "minitest", "~> 5.16" + +gem "rubocop", "~> 1.21" diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..3797666 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2024 Omkar Moghe + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3d0091f --- /dev/null +++ b/README.md @@ -0,0 +1,116 @@ +# Expressive + +A simple and flexible Ruby library to build and evaluate mathematical or other expressions. + +## Installation + +Install the gem and add to the application's Gemfile by executing: + + $ bundle add expressive + +If bundler is not being used to manage dependencies, install the gem by executing: + + $ gem install expressive + +## Usage + +### Models + +#### Scalar + +A `Scalar` is the simplest object that can be evaluated. It holds a single `value`. When used in an `Expression`, this `value` must respond to the symbol (i.e. support the method) defined by the `Expression#operator`. + +```ruby +Scalar.new(1) +``` + +#### Variable + +A `Variable` represents a named value stored in the `Environment`. Unlike `Scalars`, `Variables` have no value until they are evaluated by an `Environment`. Evaluating a `Variable` that isn't present in the `Environment` will result in a `MissingVariableError`. + +```ruby +Variable.new("variable_a") +``` + +#### Expression + +An expression represents 2 or more `operands` that are reduced using a defined `operator`. The `operands` of an `Expression` can be `Scalars`, `Variables`, or other `Expressions`. All `operands` must `respond_to?` the method defined by the `operator`. + +And `Expression` can store its result back into the `Environment` by defining an `output`. + +```ruby +# addition +Expression.new(:+, Scalar.new(1), Scalar.new(2)) + +# multiplication +Expression.new(:*, Variable.new("variable_a"), Scalar.new(2)) + +# storing output +Expression.new(:+, Scalar.new(1), Scalar.new(2), output: "one_plus_two") +``` + +#### Environment + +The `Environment` holds state in the form of a `variables` hash and can evaluate `Expressions`, `Scalars`, and `Variables` within a context. The environment handles updates to the state as `Expressions` run. + +```ruby +environment = Environment.new( + "variable_a" => 1, + "variable_b" => 2, +) + +environment.evaluate(Variable.new("variable_a")) +#=> 1 + +environment.evaluate( + Expression.new( + :+, + Variable.new("variable_a"), + Variable.new("variable_b"), + output: "variable_c" # defines where to store the result value + ) +) +#=> 3 + +environment.variables +#=> { "variable_a" => 1, "variable_b" => 2, "variable_c" => 3 } +``` + +When evaluating multiple objects at a time, the value of the _last_ object will be returned. + +```ruby +environment = Environment.new +environment.evaluate( + Scalar.new(1), + Expression.new(:+, Scalar.new(1), Scalar.new(2)) +) +#=> 3 +``` + +### Serialization (to JSON) + +All models support serialization via: +- `as_json`: builds a serializable hash representation of the object +- `to_json`: builds a JSON string representing the object + +### Building (from JSON) + + + +### Beyond math + + + +## Development + +After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. + +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/omkarmoghe/expressive. + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2bf771f --- /dev/null +++ b/Rakefile @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require "bundler/gem_tasks" +require "minitest/test_task" + +Minitest::TestTask.create + +require "rubocop/rake_task" + +RuboCop::RakeTask.new + +task default: %i[test rubocop] diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..81ecd90 --- /dev/null +++ b/bin/console @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "bundler/setup" +require "expressive" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +require "irb" +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/expressive.gemspec b/expressive.gemspec new file mode 100644 index 0000000..68e18c5 --- /dev/null +++ b/expressive.gemspec @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require_relative "lib/expressive/version" + +Gem::Specification.new do |spec| + spec.name = "expressive" + spec.version = Expressive::VERSION + spec.authors = ["Omkar Moghe"] + spec.email = ["theomkarmoghe@gmail.com"] + + spec.summary = "A simple and flexible Ruby library to build and evaluate mathematical or other expressions." + spec.homepage = "https://github.com/omkarmoghe/expressive" + spec.license = "MIT" + spec.required_ruby_version = ">= 3.0.0" + + spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'" + + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = spec.homepage + spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here." + + # Specify which files should be added to the gem when it is released. + # The `git ls-files -z` loads the files in the RubyGem that have been added into git. + gemspec = File.basename(__FILE__) + spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls| + ls.readlines("\x0", chomp: true).reject do |f| + (f == gemspec) || + f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile]) + end + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + # Uncomment to register a new dependency of your gem + # spec.add_dependency "example-gem", "~> 1.0" + + # For more information and examples about making a new gem, check out our + # guide at: https://bundler.io/guides/creating_gem.html +end diff --git a/lib/expressive.rb b/lib/expressive.rb new file mode 100644 index 0000000..8f5802a --- /dev/null +++ b/lib/expressive.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require_relative "expressive/version" + +module Expressive + class Error < StandardError; end + # Your code goes here... +end diff --git a/lib/expressive/version.rb b/lib/expressive/version.rb new file mode 100644 index 0000000..d02fa8f --- /dev/null +++ b/lib/expressive/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module Expressive + VERSION = "0.1.0" +end diff --git a/sig/expressive.rbs b/sig/expressive.rbs new file mode 100644 index 0000000..517adc9 --- /dev/null +++ b/sig/expressive.rbs @@ -0,0 +1,4 @@ +module Expressive + VERSION: String + # See the writing guide of rbs: https://github.com/ruby/rbs#guides +end diff --git a/test/test_expressive.rb b/test/test_expressive.rb new file mode 100644 index 0000000..d8e64c7 --- /dev/null +++ b/test/test_expressive.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require "test_helper" + +class TestExpressive < Minitest::Test + def test_that_it_has_a_version_number + refute_nil ::Expressive::VERSION + end + + def test_it_does_something_useful + assert false + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..939d797 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) +require "expressive" + +require "minitest/autorun"