Skip to content

Commit

Permalink
nothing to see here
Browse files Browse the repository at this point in the history
  • Loading branch information
omkarmoghe committed Mar 28, 2024
1 parent c8e4f72 commit 5f4a241
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ If bundler is not being used to manage dependencies, install the gem by executin
`gem install portable_expressions`

## Why would I need this?
<!-- TODO -->

See our [example use cases](#example-use-cases) for more ideas.

## Usage

> [!IMPORTANT]
> When using the gem, all references to the models below must be prefixed with `PortableExpressions::`. This is omitted in the README for simplicity.
> When using the gem, all references to the models below must be prefixed with `PortableExpressions::` when used in your project. This is omitted in the README for simplicity.
### Scalar

Expand Down Expand Up @@ -141,6 +140,18 @@ environment.variables["variable_a"] = 2
environment.evaluate(Variable.new("variable_a")) # => 2
```

> [!CAUTION]
> Be careful when using `symbols` as variable names. While this works in Ruby, any `symbol` keys or names are converted to `strings` when the `Environment` or `Variable` are serialized to JSON.
>
> ```ruby
> environment = Environment.new(foo: "bar")
>
> variable_foo = Variable.new(:foo)
> environment.evaluate(variable_foo) #=> "bar"
>
> variable_foo = PortableExpressions.from_json(variable_foo.to_json)
> environment.evaluate(variable_foo) #=> MissingVariableError
### Serialization (to JSON)
All models including the `Environment` support serialization via:
Expand Down Expand Up @@ -187,7 +198,7 @@ a_greater_than_b = Expression.new(
Variable.new("variable_a"),
Variable.new("variable_b"),
)
conditional = Expression.new(
condition = Expression.new(
:and,
a_greater_than_b,
Variable.new("variable_c"),
Expand All @@ -196,7 +207,7 @@ Environment.new(
"variable_a" => 2,
"variable_b" => 1,
"variable_c" => "truthy",
).evaluate(conditional)
).evaluate(condition)
#=> true
```
Expand Down
4 changes: 1 addition & 3 deletions lib/portable_expressions/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def evaluate(*objects)
end

def as_json
super.merge(
variables: variables
)
super.merge(variables: variables)
end

private
Expand Down

0 comments on commit 5f4a241

Please sign in to comment.