Class: Longleaf::ConfigBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/longleaf/specs/config_builder.rb

Overview

Test helper for constructing application configuration hashes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigBuilder

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

#configObject

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

#getObject

Returns the constructed configuration.

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

Parameters:

  • loc_names (Object)

    one or more location names. Can be a string or array.

  • service_names (Object)

    one or more service names. Can be a string or array.



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

Parameters:

  • name (String)

    name of the location

  • path (String) (defaults to: '/file/path/')

    value for the ‘path’ field

  • md_path (String) (defaults to: '/metadata/path/')

    value for the ‘metadata_path’ field

Returns:

  • this builder



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

Parameters:

  • locations (Hash) (defaults to: Hash.new)

    value for the locations fields. Default is {}

Returns:

  • this builder



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

Parameters:

  • mappings (Object) (defaults to: Hash.new)

    the mappings config

Returns:

  • this builder



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

Parameters:

  • name (String)

    name of the service

  • work_script (String) (defaults to: 'some_pres_service.rb')

    value for the ‘work_script’ field

  • work_class (String) (defaults to: nil)

    value for the ‘work_class’ field

  • frequency (String) (defaults to: nil)

    value for the ‘frequency’ field

  • delay (String) (defaults to: nil)

    value for the ‘delay’ field

  • properties (Hash) (defaults to: nil)

    hash of additional properties to include in the service

Returns:

  • this builder



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

Parameters:

  • services (Hash) (defaults to: Hash.new)

    value for the services field. Default is {}

Returns:

  • this builder



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

Returns:

  • the file path of the config 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