Class: Upkeep::Rails::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/upkeep/rails/configuration.rb

Defined Under Namespace

Classes: IdentityBuilder, IdentityDefinition

Constant Summary collapse

SUBSCRIPTION_STORES =
[:active_record, :memory].freeze
REFUSED_BOUNDARY_BEHAVIORS =
[:raise, :warn].freeze
IDENTITY_SOURCES =
[:current, :session, :cookie, :warden].freeze
DELIVERY_ADAPTERS =
[:async, :active_job, :inline].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



106
107
108
109
110
111
112
113
114
115
# File 'lib/upkeep/rails/configuration.rb', line 106

def initialize
  @enabled = true
  @subscription_store = :active_record
  @delivery_adapter = :async
  @delivery_queue = :upkeep_realtime
  @delivery_batch_window = 0.01
  @refused_boundary_behavior = nil
  @activation_token_expires_in = 24 * 60 * 60
  @identity_definitions = {}
end

Instance Attribute Details

#activation_token_expires_inObject

Returns the value of attribute activation_token_expires_in.



100
101
102
# File 'lib/upkeep/rails/configuration.rb', line 100

def activation_token_expires_in
  @activation_token_expires_in
end

#delivery_adapterObject

Returns the value of attribute delivery_adapter.



104
105
106
# File 'lib/upkeep/rails/configuration.rb', line 104

def delivery_adapter
  @delivery_adapter
end

#delivery_batch_windowObject

Returns the value of attribute delivery_batch_window.



101
102
103
# File 'lib/upkeep/rails/configuration.rb', line 101

def delivery_batch_window
  @delivery_batch_window
end

#delivery_queueObject

Returns the value of attribute delivery_queue.



102
103
104
# File 'lib/upkeep/rails/configuration.rb', line 102

def delivery_queue
  @delivery_queue
end

#enabledObject

Returns the value of attribute enabled.



99
100
101
# File 'lib/upkeep/rails/configuration.rb', line 99

def enabled
  @enabled
end

#subscription_storeObject

Returns the value of attribute subscription_store.



103
104
105
# File 'lib/upkeep/rails/configuration.rb', line 103

def subscription_store
  @subscription_store
end

Instance Method Details

#clear_identities!Object



198
199
200
# File 'lib/upkeep/rails/configuration.rb', line 198

def clear_identities!
  @identity_definitions.clear
end

#identify(name, current: nil, session: nil, cookie: nil, warden: nil, &block) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/upkeep/rails/configuration.rb', line 154

def identify(name, current: nil, session: nil, cookie: nil, warden: nil, &block)
  source, source_key = identity_source(current: current, session: session, cookie: cookie, warden: warden)
  builder = IdentityBuilder.new
  if block
    block.arity == 1 ? block.call(builder) : builder.instance_eval(&block)
  end

  unless builder.subscribe_block
    raise ConfigurationError, "config.identify :#{name} requires a subscribe block"
  end

  @identity_definitions[name.to_sym] = IdentityDefinition.new(
    name: name,
    source: source,
    source_key: source_key,
    subscribe_block: builder.subscribe_block,
    absent_block: builder.absent_block
  )
end

#identity_definition(name) ⇒ Object



194
195
196
# File 'lib/upkeep/rails/configuration.rb', line 194

def identity_definition(name)
  @identity_definitions.fetch(name.to_sym)
end

#identity_definitionsObject



190
191
192
# File 'lib/upkeep/rails/configuration.rb', line 190

def identity_definitions
  @identity_definitions.values
end

#identity_presence_metadata(source:, key:, value:) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/upkeep/rails/configuration.rb', line 174

def (source:, key:, value:)
  definitions = identity_definitions.select { |definition| definition.matches_source?(source, key) }
  absent_by_name = definitions.to_h do |definition|
    [definition.name.to_s, definition.absent?(value)]
  end

  {
    partitioning: if definitions.any?
      absent_by_name.values.any? { |absent| !absent }
    else
      !value.nil?
    end,
    absent_by_name: absent_by_name
  }
end

#refused_boundary_behaviorObject



139
140
141
# File 'lib/upkeep/rails/configuration.rb', line 139

def refused_boundary_behavior
  @refused_boundary_behavior || default_refused_boundary_behavior
end

#refused_boundary_behavior=(value) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/upkeep/rails/configuration.rb', line 143

def refused_boundary_behavior=(value)
  value = value.to_sym if value.respond_to?(:to_sym)

  unless REFUSED_BOUNDARY_BEHAVIORS.include?(value)
    raise ConfigurationError,
      "Unknown Upkeep refused_boundary_behavior #{value.inspect}; expected one of #{REFUSED_BOUNDARY_BEHAVIORS.join(", ")}"
  end

  @refused_boundary_behavior = value
end