Class: Jac::Configuration::ProfileResolver
- Inherits:
-
Object
- Object
- Jac::Configuration::ProfileResolver
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#merger ⇒ Object
readonly
Returns the value of attribute merger.
Instance Method Summary collapse
- #find_generic_profile(profile_name) ⇒ Object
- #find_profile(profile_name) ⇒ Object
- #generate_profile(match, matched_profile, profile_name) ⇒ Object
- #generic_profiles(config) ⇒ Object
-
#initialize(config) ⇒ ProfileResolver
constructor
A new instance of ProfileResolver.
- #resolve(profile, resolved = []) ⇒ Object
Constructor Details
#initialize(config) ⇒ ProfileResolver
Returns a new instance of ProfileResolver.
231 232 233 234 |
# File 'lib/jac/configuration.rb', line 231 def initialize(config) @config = config @merger = Merger.new end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
229 230 231 |
# File 'lib/jac/configuration.rb', line 229 def config @config end |
#merger ⇒ Object (readonly)
Returns the value of attribute merger.
229 230 231 |
# File 'lib/jac/configuration.rb', line 229 def merger @merger end |
Instance Method Details
#find_generic_profile(profile_name) ⇒ Object
TODO:
print warning if other matching generic profiles found
271 272 273 274 275 276 277 |
# File 'lib/jac/configuration.rb', line 271 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
252 253 254 255 256 257 258 259 |
# File 'lib/jac/configuration.rb', line 252 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
261 262 263 264 265 266 267 268 |
# File 'lib/jac/configuration.rb', line 261 def generate_profile(match, matched_profile, profile_name) gen_profile = {} gen_profile['captures'] = match.captures if match.captures gen_profile['named_captures'] = match.named_captures if match.named_captures gen_profile.merge!(config[matched_profile]) config[profile_name] = gen_profile end |
#generic_profiles(config) ⇒ Object
279 280 281 282 283 284 285 286 287 |
# File 'lib/jac/configuration.rb', line 279 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
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/jac/configuration.rb', line 236 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 |