Module: FmRest::Spyke::Model::Connection

Extended by:
ActiveSupport::Concern
Included in:
FmRest::Spyke::Model
Defined in:
lib/fmrest/spyke/model/connection.rb

Overview

This module provides methods for configuring the Farday connection for the model, as well as setting up the connection itself.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_fmrest_config_overlayObject

Clears the connection settings overlay.



70
71
72
# File 'lib/fmrest/spyke/model/connection.rb', line 70

def clear_fmrest_config_overlay
  Thread.current[fmrest_config_overlay_key] = nil
end

.connectionObject

Spyke override -- Defaults to fmrest_connection



98
99
100
# File 'lib/fmrest/spyke/model/connection.rb', line 98

def connection
  super || fmrest_connection
end

.faraday(&block) ⇒ Object

Sets a block for injecting custom middleware into the Faraday connection.

Examples:

class MyModel < FmRest::Spyke::Base
  faraday do |conn|
    # Set up a custom logger for the model
    conn.response :logger, MyApp.logger, bodies: true
  end
end


113
114
115
# File 'lib/fmrest/spyke/model/connection.rb', line 113

def faraday(&block)
  self.faraday_block = block
end

.fmrest_configObject



21
22
23
24
25
26
27
# File 'lib/fmrest/spyke/model/connection.rb', line 21

def fmrest_config
  if fmrest_config_overlay
    return FmRest.default_connection_settings.merge(fmrest_config_overlay, skip_validation: true)
  end

  FmRest.default_connection_settings
end

.fmrest_config=(settings) ⇒ Object

Sets the FileMaker connection settings for the model.

Behaves similar to ActiveSupport's class_attribute, so it can be inherited and safely overwritten in subclasses.

Parameters:

  • settings (Hash)

    The settings hash



36
37
38
39
40
41
42
43
44
# File 'lib/fmrest/spyke/model/connection.rb', line 36

def fmrest_config=(settings)
  settings = ConnectionSettings.new(settings, skip_validation: true)

  singleton_class.redefine_method(:fmrest_config) do
    overlay = fmrest_config_overlay
    return settings.merge(overlay, skip_validation: true) if overlay
    settings
  end
end

.fmrest_config_overlayFmRest::ConnectionSettings

Returns the connection settings overlay if any is in use.

Returns:

  • (FmRest::ConnectionSettings)

    the connection settings overlay if any is in use



60
61
62
63
64
65
66
# File 'lib/fmrest/spyke/model/connection.rb', line 60

def fmrest_config_overlay
  Thread.current[fmrest_config_overlay_key] || begin
    superclass.fmrest_config_overlay
  rescue NoMethodError
    nil
  end
end

.fmrest_config_overlay=(settings) ⇒ Object

Allows overriding some connection settings in a thread-local manner. Useful in the use case where you want to connect to the same database using different accounts (e.g. credentials provided by users in a web app context).



53
54
55
# File 'lib/fmrest/spyke/model/connection.rb', line 53

def fmrest_config_overlay=(settings)
  Thread.current[fmrest_config_overlay_key] = settings
end

.with_overlay(settings, &block) ⇒ Object

Runs a block of code in the context of the given connection settings without affecting the connection settings outside said block.

Examples:

Honeybee.with_overlay(username: "...", password: "...") do
  Honeybee.query(...)
end


85
86
87
88
89
90
91
92
93
94
# File 'lib/fmrest/spyke/model/connection.rb', line 85

def with_overlay(settings, &block)
  Fiber.new do
    begin
      self.fmrest_config_overlay = settings
      yield
    ensure
      self.clear_fmrest_config_overlay
    end
  end.resume
end

Instance Method Details

#fmrest_configObject



157
158
159
# File 'lib/fmrest/spyke/model/connection.rb', line 157

def fmrest_config
  self.class.fmrest_config
end