Class: Jac::Configuration::ProfileResolver

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

Overview

Describes profile resolving strategy

Constant Summary collapse

EXTENDS_KEY =

Key where all inherited profiles listed

'extends'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ProfileResolver

Returns a new instance of ProfileResolver.


264
265
266
267
# File 'lib/jac/configuration.rb', line 264

def initialize(config)
  @config = config
  @merger = Merger.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config


262
263
264
# File 'lib/jac/configuration.rb', line 262

def config
  @config
end

#mergerObject (readonly)

Returns the value of attribute merger


262
263
264
# File 'lib/jac/configuration.rb', line 262

def merger
  @merger
end

Instance Method Details

#find_generic_profile(profile_name) ⇒ Object

TODO:

print warning if other matching generic profiles found


306
307
308
309
310
311
312
# File 'lib/jac/configuration.rb', line 306

def find_generic_profile(profile_name)
  generic_profiles(config)
    .detect do |profile, regex|
      m = regex.match(profile_name)
      break [m, profile] if m
    end
end

#find_profile(profile_name) ⇒ Object

Raises:

  • (ArgumentError)

285
286
287
288
289
290
291
292
# File 'lib/jac/configuration.rb', line 285

def find_profile(profile_name)
  return config[profile_name] if config.key?(profile_name)
  # First and last chars are '/'
  match, matched_profile = find_generic_profile(profile_name)
  # Generating profile
  return generate_profile(match, matched_profile, profile_name) if match
  raise(ArgumentError, 'No such profile ' + profile_name)
end

#generate_profile(match, matched_profile, profile_name) ⇒ Object


294
295
296
297
298
299
300
301
302
303
# File 'lib/jac/configuration.rb', line 294

def generate_profile(match, matched_profile, profile_name)
  gen_profile = {}
  gen_profile['captures'] = match.captures if match.captures
  if match.respond_to?(:named_captures) && match.named_captures
    gen_profile['named_captures'] = match.named_captures
  end
  gen_profile.merge!(config[matched_profile])

  config[profile_name] = gen_profile
end

#generic_profiles(config) ⇒ Object


314
315
316
317
318
319
320
321
322
# File 'lib/jac/configuration.rb', line 314

def generic_profiles(config)
  # Create generic profiles if missing
  @generic_profiles ||= config
                        .keys
                        .select { |k| k[0] == '/' && k[-1] == '/' }
                        .map { |k| [k, Regexp.new(k[1..-2])] }

  @generic_profiles
end

#resolve(profile, resolved = []) ⇒ Object


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/jac/configuration.rb', line 269

def resolve(profile, resolved = [])
  profile.inject({}) do |acc, elem|
    if resolved.include?(elem)
      msg = 'Cyclic dependency found ' + (resolved + [elem]).join(' -> ')
      raise(ArgumentError, msg)
    end
    profile_values = find_profile(elem)
    # Find all inheritors
    extends = *profile_values[EXTENDS_KEY] || []
    # We can do not check extends. Empty profile returns {}
    # Inherited values goes first
    inherited = merger.merge(acc, resolve(extends, resolved + [elem]))
    merger.merge(inherited, profile_values)
  end
end