Module: Euler

Defined in:
lib/euler.rb,
lib/euler/errors.rb,
lib/euler/problem.rb,
lib/euler/version.rb,
lib/euler/solution.rb

Defined Under Namespace

Classes: ConfigOptions, EulerFileNotFoundError, LanguageNotRegisteredError, Problem, Solution

Constant Summary collapse

VERSION =
'0.2.0'
@@config_options =
Euler::ConfigOptions.new
@@languages =
Hash.new

Class Method Summary collapse

Class Method Details

.config {|@@config_options| ... } ⇒ Object

Yields config_options to a block. Used to configure the Euler module.

Yields:

  • (@@config_options)


39
40
41
# File 'lib/euler.rb', line 39

def config
  yield @@config_options
end

.euler_file_pathObject



102
103
104
# File 'lib/euler.rb', line 102

def euler_file_path
  "#{root}/Eulerfile.rb"
end

.get_language(language_name) ⇒ Object

Returns an instance of a registered language class.

not already been registered.



66
67
68
69
70
71
72
73
# File 'lib/euler.rb', line 66

def get_language language_name
  language_string = language_name.to_s
  if @@languages[language_string].nil?
    raise Euler::LanguageNotRegisteredError.new "#{language_string} has not been registered."
  else
    @@languages[language_string].new
  end
end

.method_missing(method, *args, &block) ⇒ Object

Returns configuration options. If the configuration option is a Proc then this method will call it with the arguments passed to this method and return the result.



78
79
80
81
82
83
84
85
# File 'lib/euler.rb', line 78

def method_missing method, *args, &block
  temp = @@config_options.send method
  if temp.is_a?(Proc)
    temp.call *args
  else
    temp
  end
end

.params_from_dir_or_args(args) ⇒ Object

Returns an array with the first element being the problem id and the second element being the language gotten from either the args passed in or by parsing the directory the command was ran from



109
110
111
112
113
114
115
# File 'lib/euler.rb', line 109

def params_from_dir_or_args args
  from_dir = parse_params_from_directory
  [
    args.shift || from_dir.shift,
    args.shift || from_dir.shift
  ]
end

.parse_params_from_directory(dir = ) ⇒ Object

Uses the directory_parse_strategy to attempt to parse the problem id and language of the solution’s directory the command was ran from.



119
120
121
# File 'lib/euler.rb', line 119

def parse_params_from_directory dir = ENV['PWD']
  self.directory_parse_strategy(dir)
end

.register_language(language_name, language = nil) ⇒ Object

Register a language by calling this method with the languages name and the class for that language. To unregister a language call this method without a second argument.

Language classes require a run method which accepts an Euler::Solution and returns the result of running the solution. Optionally language classes can also have an init method which also accepts a solution. The init method does any extra steps required in initializing an empty solution.



52
53
54
55
# File 'lib/euler.rb', line 52

def register_language language_name, language = nil
  language_string = language_name.to_s
  @@languages[language_string.to_s] = language
end

.rootObject

Returns the root directory of the current project.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/euler.rb', line 88

def root
  if @root.nil?
    root = ENV['PWD']
    until File.exists?("#{root}/Eulerfile.rb") || File.expand_path(root) == '/' do
      root = File.dirname(root)
    end
    if not File.exists?("#{root}/Eulerfile.rb")
      raise Euler::EulerFileNotFoundError.new "Unable to find an Eulerfile.rb in any of the parent directories."
    end
    @root = root
  end
  @root
end

.unregister_language(language_name) ⇒ Object

Unregisters a language



58
59
60
# File 'lib/euler.rb', line 58

def unregister_language language_name
  register_language(language_name)
end