Class: JayAPI::Configuration

Inherits:
OpenStruct
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/jay_api/configuration.rb,
lib/jay_api/mergeables/merge_selector.rb

Overview

This class declaration serves as an extension of the already defined JayAPI::Configuration class.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_file(file_name) ⇒ JayAPI::Configuration

Loads the configuration from the given file.

Parameters:

  • file_name (String)

    The file from which to load the configuration.

Returns:

Raises:

  • (Errno::ENOENT)

    If the given file cannot be found.

  • (Psych::DisallowedClass)

    If the YAML contains a class other than Symbol



27
28
29
# File 'lib/jay_api/configuration.rb', line 27

def self.from_file(file_name)
  from_string(File.read(file_name))
end

.from_string(yaml) ⇒ JayAPI::Configuration

Loads the configuration from the given YAML string.

Parameters:

  • yaml (String)

    The YAML string containing the configuration.

Returns:

Raises:

  • (Psych::DisallowedClass)

    If the YAML contains a class other than Symbol



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jay_api/configuration.rb', line 36

def self.from_string(yaml)
  yaml = ERB.new(yaml).result
  config = YAML.safe_load(yaml, permitted_classes: [Symbol])

  unless config.is_a?(Hash)
    raise JayAPI::Errors::ConfigurationError.new(
      "Jay's configuration should be a set of key-value pairs.", yaml
    )
  end

  from_hash(config)
end

Instance Method Details

#deep_to_hHash

Recursively converts the receiver into a standard Hash

Returns:

  • (Hash)

    The result of the conversion.



83
84
85
# File 'lib/jay_api/configuration.rb', line 83

def deep_to_h
  to_h { |key, value| [key, value_for_h(value)] }
end

#keysArray<Object>

Returns An array with the keys (generally the name of the attributes) that the configuration object has.

Returns:

  • (Array<Object>)

    An array with the keys (generally the name of the attributes) that the configuration object has.



94
95
96
# File 'lib/jay_api/configuration.rb', line 94

def keys
  @table.keys
end

#to_yamlString

Returns The configuration in the YAML format.

Returns:

  • (String)

    The configuration in the YAML format



88
89
90
# File 'lib/jay_api/configuration.rb', line 88

def to_yaml
  YAML.dump(deep_to_h.deep_stringify_keys)
end

#with_merge_selectorJayAPI::Mergeables::MergeSelector::Configuration

Returns A Configuration object that contains the merging functionality.

Returns:



11
12
13
# File 'lib/jay_api/mergeables/merge_selector.rb', line 11

def with_merge_selector
  JayAPI::Mergeables::MergeSelector::Configuration.from_hash(deep_to_h)
end