Class: Synthra::Types::FinanceBanking::Money

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/types/finance_banking/banking.rb

Overview

Money type

Instance Method Summary collapse

Instance Method Details

#generate_currency(context) ⇒ Object (private)



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/synthra/types/finance_banking/banking.rb', line 53

def generate_currency(context)
  adapter = faker_adapter(context)
  if adapter
    adapter.currency_code

  else
    Faker::Currency.code
  end

rescue StandardError
  "USD"
end

#generate_edge(rng, context, args) ⇒ Object



40
41
42
43
44
45
# File 'lib/synthra/types/finance_banking/banking.rb', line 40

def generate_edge(rng, context, args)
  {
    "amount" => 0.0,
    "currency" => "USD"
  }
end

#generate_invalid(rng, context, args) ⇒ Object



47
48
49
# File 'lib/synthra/types/finance_banking/banking.rb', line 47

def generate_invalid(rng, context, args)
  rng.sample([nil, "not money", [], {}, 123])
end

#generate_random(rng, context, args) ⇒ Hash

Generate a random money value

Creates a hash with amount (2 decimal places) and currency code. Uses Faker::Currency.code if no currency specified.

Examples:

generate_random(rng, { range: 0..100, currency: "EUR" })
# => { "amount" => 45.67, "currency" => "EUR" }

Parameters:

  • rng (Generator::RNG)

    random number generator

  • args (Hash)

    type arguments

Options Hash (args):

  • :range (Range, Hash)

    amount range (default: 0..10000)

  • :currency (String)

    currency code (default: random from Faker)

Returns:

  • (Hash)

    { "amount" => Float, "currency" => String }



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/synthra/types/finance_banking/banking.rb', line 23

def generate_random(rng, context, args)
  range = args[:range] || (0..10_000)
  min = range.is_a?(Hash) ? (range[:min] || 0) : range.min
  max = range.is_a?(Hash) ? (range[:max] || 10_000) : range.max

  # Use provided currency or generate one using Faker
  currency = args[:currency] || generate_currency(context)

  # Generate amount and round to 2 decimal places (cents)
  amount = (rng.rand * (max.to_f - min.to_f) + min.to_f).round(2)

  {
    "amount" => amount,
    "currency" => currency
  }
end