Module: Straight::GatewayModule

Included in:
Gateway
Defined in:
lib/straight/gateway.rb

Overview

This module should be included into your own class to extend it with Gateway functionality. For example, if you have a ActiveRecord model called Gateway, you can include GatewayModule into it and you’ll now be able to do everything Straight::Gateway can do, but you’ll also get AR Database storage funcionality, its validations etc.

The right way to implement this would be to do it the other way: inherit from Straight::Gateway, then include ActiveRecord, but at this point ActiveRecord doesn’t work this way. Furthermore, some other libraries, like Sequel, also require you to inherit from them. Thus, the module.

When this module is included, it doesn’t actually include all the methods, some are prepended (see Ruby docs on #prepend). It is important specifically for getters and setters and as a general rule only getters and setters are prepended.

If you don’t want to bother yourself with modules, please use Straight::Gateway class and simply create new instances of it. However, if you are contributing to the library, all new funcionality should go to either Straight::GatewayModule::Includable or Straight::GatewayModule::Prependable (most likely the former).

Defined Under Namespace

Modules: Includable, Prependable Classes: NoAdaptersAvailable, OrderAmountInvalid

Constant Summary collapse

DEFAULT_STATUS_CHECK_SCHEDULE =

Determines the algorithm for consequitive checks of the order status.

-> (period, iteration_index) do
  iteration_index += 1
  if iteration_index >= 20
    period          *= 2
    iteration_index  = 0
  end
  return { period: period, iteration_index: iteration_index }
end

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Only add getters and setters for those properties in the extended class that don’t already have them. This is very useful with ActiveRecord for example where we don’t want to override AR getters and setters that set attributes.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/straight/gateway.rb', line 27

def self.included(base)
  base.class_eval do
    [
      :pubkey,
      :test_pubkey,
      :confirmations_required,
      :status_check_schedule,
      :blockchain_adapters,
      :exchange_rate_adapters,
      :order_callbacks,
      :order_class,
      :default_currency,
      :name,
      :address_provider,
      :address_provider_type,
      :address_derivation_scheme,
      :test_mode
    ].each do |field|
      attr_reader field unless base.method_defined?(field)
      attr_writer field unless base.method_defined?("#{field}=")
      prepend Prependable
      include Includable
    end
  end
end