Class: ConsoleKit::TenantConfigurator::ContextWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/console_kit/tenant_configurator/context_wrapper.rb

Overview

Encapsulates context and attributes to resolve DataClump smells

Constant Summary collapse

HANDLER_ATTRIBUTES =
{
  Connections::SqlConnectionHandler => :tenant_shard,
  Connections::MongoConnectionHandler => :tenant_mongo_db,
  Connections::RedisConnectionHandler => :tenant_redis_db,
  Connections::ElasticsearchConnectionHandler => :tenant_elasticsearch_prefix
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx, attributes) ⇒ ContextWrapper

Returns a new instance of ContextWrapper.



48
49
50
51
# File 'lib/console_kit/tenant_configurator/context_wrapper.rb', line 48

def initialize(ctx, attributes)
  @ctx = ctx
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



14
15
16
# File 'lib/console_kit/tenant_configurator/context_wrapper.rb', line 14

def attributes
  @attributes
end

#ctxObject (readonly)

Returns the value of attribute ctx.



14
15
16
# File 'lib/console_kit/tenant_configurator/context_wrapper.rb', line 14

def ctx
  @ctx
end

Class Method Details

.for_context(ctx) ⇒ Object



17
18
19
# File 'lib/console_kit/tenant_configurator/context_wrapper.rb', line 17

def for_context(ctx)
  new(ctx, detect_attributes(ctx))
end

Instance Method Details

#any_set?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/console_kit/tenant_configurator/context_wrapper.rb', line 53

def any_set?
  attributes.any? { |attr| ctx.public_send(attr).present? }
end

#assign(constant, mapping) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/console_kit/tenant_configurator/context_wrapper.rb', line 61

def assign(constant, mapping)
  attributes.map do |attr|
    existing = safe_read(attr)
    new_value = constant[mapping[attr]]
    ctx.public_send("#{attr}=", new_value)
    [attr, existing, new_value]
  end
end

#resetObject



57
58
59
# File 'lib/console_kit/tenant_configurator/context_wrapper.rb', line 57

def reset
  attributes.each { |attr| ctx.public_send("#{attr}=", nil) }
end