Class: Longleaf::ConfigBuilder
- Inherits:
-
Object
- Object
- Longleaf::ConfigBuilder
- Defined in:
- lib/longleaf/specs/config_builder.rb
Overview
Test helper for constructing application configuration hashes
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
-
#get ⇒ Object
The constructed configuration.
-
#initialize ⇒ ConfigBuilder
constructor
A new instance of ConfigBuilder.
-
#map_services(loc_names, service_names) ⇒ Object
Add a mapping from one or more services to one or more location.
-
#with_location(name:, path: '/file/path/', md_path: '/metadata/path/', md_digests: nil) ⇒ Object
Add a 'location' to the config.
-
#with_locations(locations = Hash.new) ⇒ Object
Add a root 'locations' field to the config.
-
#with_mappings(mappings = Hash.new) ⇒ Object
Adds a 'service_mappings' section to the config.
-
#with_service(name:, work_script: 'some_pres_service.rb', work_class: nil, frequency: nil, delay: nil, properties: nil) ⇒ Object
Add a 'service' to the config.
-
#with_services(services = Hash.new) ⇒ Object
Add a root 'services' field to the config.
-
#with_system(sys_config) ⇒ Object
Add a system config section to the config.
-
#write_to_yaml_file(dest_path = nil) ⇒ Object
Writes the configuration from this builder into a temporary file.
Constructor Details
#initialize ⇒ ConfigBuilder
Returns a new instance of ConfigBuilder
13 14 15 |
# File 'lib/longleaf/specs/config_builder.rb', line 13 def initialize @config = Hash.new end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config
11 12 13 |
# File 'lib/longleaf/specs/config_builder.rb', line 11 def config @config end |
Instance Method Details
#get ⇒ Object
Returns the constructed configuration
99 100 101 |
# File 'lib/longleaf/specs/config_builder.rb', line 99 def get @config end |
#map_services(loc_names, service_names) ⇒ Object
Add a mapping from one or more services to one or more location
82 83 84 85 86 87 88 89 90 |
# File 'lib/longleaf/specs/config_builder.rb', line 82 def map_services(loc_names, service_names) @config[AF::SERVICE_MAPPINGS] = Array.new unless @config.key?(AF::SERVICE_MAPPINGS) mapping = Hash.new mapping[AF::LOCATIONS] = loc_names unless loc_names.nil? mapping[AF::SERVICES] = service_names unless service_names.nil? @config[AF::SERVICE_MAPPINGS].push(mapping) self end |
#with_location(name:, path: '/file/path/', md_path: '/metadata/path/', md_digests: nil) ⇒ Object
Add a 'location' to the config
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/longleaf/specs/config_builder.rb', line 30 def with_location(name:, path: '/file/path/', md_path: '/metadata/path/', md_digests: nil) @config[AF::LOCATIONS] = Hash.new unless @config.key?(AF::LOCATIONS) location = {} @config[AF::LOCATIONS][name] = location location[AF::LOCATION_PATH] = path unless path.nil? location[AF::METADATA_PATH] = md_path unless md_path.nil? location[AF::METADATA_DIGESTS] = md_digests unless md_digests.nil? self end |
#with_locations(locations = Hash.new) ⇒ Object
Add a root 'locations' field to the config
20 21 22 23 |
# File 'lib/longleaf/specs/config_builder.rb', line 20 def with_locations(locations = Hash.new) @config[AF::LOCATIONS] = locations self end |
#with_mappings(mappings = Hash.new) ⇒ Object
Adds a 'service_mappings' section to the config
74 75 76 77 |
# File 'lib/longleaf/specs/config_builder.rb', line 74 def with_mappings(mappings = Hash.new) @config[AF::SERVICE_MAPPINGS] = mappings self end |
#with_service(name:, work_script: 'some_pres_service.rb', work_class: nil, frequency: nil, delay: nil, properties: nil) ⇒ Object
Add a 'service' to the config
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/longleaf/specs/config_builder.rb', line 57 def with_service(name:, work_script: 'some_pres_service.rb', work_class: nil, frequency: nil, delay: nil, properties: nil) @config[AF::SERVICES] = Hash.new unless @config.key?(AF::SERVICES) service = {} service[SF::WORK_SCRIPT] = work_script service[SF::WORK_CLASS] = work_class service[SF::FREQUENCY] = frequency unless frequency.nil? service[SF::DELAY] = delay unless delay.nil? service = service.merge(properties) unless properties.nil? @config[AF::SERVICES][name] = service self end |
#with_services(services = Hash.new) ⇒ Object
Add a root 'services' field to the config
44 45 46 47 |
# File 'lib/longleaf/specs/config_builder.rb', line 44 def with_services(services = Hash.new) @config[AF::SERVICES] = services self end |
#with_system(sys_config) ⇒ Object
Add a system config section to the config
93 94 95 96 |
# File 'lib/longleaf/specs/config_builder.rb', line 93 def with_system(sys_config) @config[AF::SYSTEM] = sys_config self end |
#write_to_yaml_file(dest_path = nil) ⇒ Object
Writes the configuration from this builder into a temporary file
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/longleaf/specs/config_builder.rb', line 105 def write_to_yaml_file(dest_path = nil) if dest_path.nil? file = Tempfile.new('config') file.close dest_path = file.path # deleting temp file but reusing file name. This is to avoid premature garbage collection. file.unlink end File.open(dest_path, 'w') do |f| f.write(@config.to_yaml) end dest_path end |