Module: Generator::MoneyDetector

Defined in:
lib/generator/support/money_detector.rb

Overview

Detects Money-like objects in JSON Schema definitions and known Money type names

Constant Summary collapse

MONEY_TYPES =

Known Money type names used in SP-API specifications

["Money", "MoneyType", "Currency", "CurrencyAmount"].freeze

Class Method Summary collapse

Class Method Details

.money_like?(definition) ⇒ Boolean

Detect if an object definition looks like a Money type Money objects have: amount/Amount/currencyAmount (number) + currencyCode/CurrencyCode (string)

Parameters:

  • definition (Hash)

    the JSON Schema object definition

Returns:

  • (Boolean)

    true if the object has Money-like structure



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generator/support/money_detector.rb', line 21

def money_like?(definition)
  return false unless definition.is_a?(Hash) && definition["type"] == "object"
  return false unless definition["properties"]

  props = definition["properties"]

  # Check for both camelCase and PascalCase variants
  has_amount = props.key?("Amount") || props.key?("amount") || props.key?("currencyAmount")
  has_currency = props.key?("CurrencyCode") || props.key?("currencyCode")

  return false unless has_amount && has_currency

  # Verify types
  amount_prop = props["Amount"] || props["amount"] || props["currencyAmount"]
  currency_prop = props["CurrencyCode"] || props["currencyCode"]

  amount_prop&.dig("type") == "number" && currency_prop&.dig("type") == "string"
end

.money_type?(name) ⇒ Boolean

Check if a type name is a known Money type

Parameters:

  • name (String)

    the type name to check

Returns:

  • (Boolean)

    true if the name is a known Money type



13
14
15
# File 'lib/generator/support/money_detector.rb', line 13

def money_type?(name)
  MONEY_TYPES.include?(name)
end