Sha256: 43614fc92d3096a2403addc9ec5d3e1e8f382e4bb836cefad08eadd971e12d37

Contents?: true

Size: 896 Bytes

Versions: 8

Compression:

Stored size: 896 Bytes

Contents

require 'alf'

module Database
  include Alf::Viewpoint

  def orders
    Relation(
      Tuple(oid: "O1", city: "London", amount: 100.00),
      Tuple(oid: "O2", city: "Paris",  amount: 100.00))
  end

  def discount_rules
    Relation(
      Tuple(city: "London", rule: ->{ amount * 0.20 }),
      Tuple(city: "Paris",  rule: ->{ amount * 0.15 }))
  end

end

# Let open a connection on this database
db = Database.connect(Path.pwd)

# Compute the discount to apply according to the city
rel = db.query do
  allbut(
    extend( 
      join(orders, discount_rules), 
      discount: ->{ instance_exec(&rule) } ),
    [:rule]
  )
end

puts rel
# +------+--------+---------+-----------+
# | :oid | :city  | :amount | :discount |
# +------+--------+---------+-----------+
# | O1   | London | 100.000 |    20.000 |
# | O2   | Paris  | 100.000 |    15.000 |
# +------+--------+---------+-----------+

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
alf-core-0.16.3 examples/functions-as-values.rb
alf-core-0.16.2 examples/functions-as-values.rb
alf-core-0.16.1 examples/functions-as-values.rb
alf-core-0.16.0 examples/functions-as-values.rb
alf-core-0.15.0 examples/functions-as-values.rb
alf-core-0.14.0 examples/functions-as-values.rb
alf-core-0.13.1 examples/functions-as-values.rb
alf-core-0.13.0 examples/functions-as-values.rb