Module: Igniter::Extensions::Contracts::CommercePack
- Defined in:
- lib/igniter/extensions/contracts/commerce_pack.rb
Class Method Summary collapse
- .grand_total_keyword ⇒ Object
- .install_dsl_keywords(kernel) ⇒ Object
- .install_into(kernel) ⇒ Object
- .manifest ⇒ Object
- .order_items_keyword ⇒ Object
- .subtotal_keyword ⇒ Object
- .tax_amount_keyword ⇒ Object
Class Method Details
.grand_total_keyword ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/igniter/extensions/contracts/commerce_pack.rb', line 69 def grand_total_keyword Igniter::Contracts::DslKeyword.new(:grand_total) do |name = :grand_total, subtotal:, builder:, tax: nil, shipping: nil, discount: nil| dependency_names = [subtotal, tax, shipping, discount].compact.map(&:to_sym) builder.add_operation( kind: :compute, name: name, depends_on: dependency_names, callable: lambda do |**values| total = values.fetch(subtotal.to_sym) total += values.fetch(tax.to_sym) if tax total += values.fetch(shipping.to_sym) if shipping total -= values.fetch(discount.to_sym) if discount total end ) end end |
.install_dsl_keywords(kernel) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/igniter/extensions/contracts/commerce_pack.rb', line 26 def install_dsl_keywords(kernel) kernel.dsl_keywords.register(:order_items, order_items_keyword) kernel.dsl_keywords.register(:subtotal, subtotal_keyword) kernel.dsl_keywords.register(:tax_amount, tax_amount_keyword) kernel.dsl_keywords.register(:grand_total, grand_total_keyword) end |
.install_into(kernel) ⇒ Object
21 22 23 24 |
# File 'lib/igniter/extensions/contracts/commerce_pack.rb', line 21 def install_into(kernel) install_dsl_keywords(kernel) kernel end |
.manifest ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/igniter/extensions/contracts/commerce_pack.rb', line 8 def manifest Igniter::Contracts::PackManifest.new( name: :extensions_commerce, requires_packs: [LookupPack, AggregatePack], registry_contracts: [ Igniter::Contracts::PackManifest.dsl_keyword(:order_items), Igniter::Contracts::PackManifest.dsl_keyword(:subtotal), Igniter::Contracts::PackManifest.dsl_keyword(:tax_amount), Igniter::Contracts::PackManifest.dsl_keyword(:grand_total) ] ) end |
.order_items_keyword ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/igniter/extensions/contracts/commerce_pack.rb', line 33 def order_items_keyword Igniter::Contracts::DslKeyword.new(:order_items) do |name = :items, from:, builder:, key: :items| builder.profile.dsl_keyword(:lookup).call( name, from: from.to_sym, key: key.to_sym, builder: builder ) end end |
.subtotal_keyword ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/igniter/extensions/contracts/commerce_pack.rb', line 44 def subtotal_keyword Igniter::Contracts::DslKeyword.new(:subtotal) do |name = :subtotal, from:, builder:, amount_key: :amount| builder.profile.dsl_keyword(:sum).call( name, from: from.to_sym, using: amount_key.to_sym, builder: builder ) end end |
.tax_amount_keyword ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/igniter/extensions/contracts/commerce_pack.rb', line 55 def tax_amount_keyword Igniter::Contracts::DslKeyword.new(:tax_amount) do |name = :tax, amount:, rate:, builder:| dependencies = [amount.to_sym, rate.to_sym] builder.add_operation( kind: :compute, name: name, depends_on: dependencies, callable: lambda do |**values| values.fetch(amount.to_sym) * values.fetch(rate.to_sym) end ) end end |