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/', s_type: nil, md_path: '/metadata/path/', md_type: nil, 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.
106 107 108 |
# File 'lib/longleaf/specs/config_builder.rb', line 106 def get @config end |
#map_services(loc_names, service_names) ⇒ Object
Add a mapping from one or more services to one or more location
89 90 91 92 93 94 95 96 97 |
# File 'lib/longleaf/specs/config_builder.rb', line 89 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/', s_type: nil, md_path: '/metadata/path/', md_type: nil, md_digests: nil) ⇒ Object
Add a ‘location’ to the config
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/longleaf/specs/config_builder.rb', line 30 def with_location(name:, path: '/file/path/', s_type: nil, md_path: '/metadata/path/', md_type: nil, 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::STORAGE_TYPE] = s_type unless s_type.nil? if !md_path.nil? md_loc = { AF::LOCATION_PATH => md_path } location[AF::METADATA_CONFIG] = md_loc md_loc[AF::METADATA_DIGESTS] = md_digests unless md_digests.nil? md_loc[AF::STORAGE_TYPE] = md_type unless md_type.nil? end 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
81 82 83 84 |
# File 'lib/longleaf/specs/config_builder.rb', line 81 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
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/longleaf/specs/config_builder.rb', line 64 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
51 52 53 54 |
# File 'lib/longleaf/specs/config_builder.rb', line 51 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
100 101 102 103 |
# File 'lib/longleaf/specs/config_builder.rb', line 100 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
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/longleaf/specs/config_builder.rb', line 112 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 |