Class: Democritus::FromJsonClassBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/democritus/from_json_class_builder.rb

Overview

Note:

This is a class with a greater “reek” than I would like. However, it is parsing JSON and loading that into ruby; Its complicated. So I’m willing to accept and assume responsibility for this code “reek”.

Responsible for building a class based on the given JSON document.

Note the following structure:

Commands that are called against the builder are Hash keys that start with ‘#’. Keywords are command parameters that do not start with ‘#’.

Examples:

```json
{ "#command_name": { "keyword_param_one": "param_value", "#nested_command_name": { "nested_keyword_param": "nested_param_value"} } }
```

See Also:

Constant Summary collapse

KEY_IS_COMMAND_REGEXP =

Used to answer the question “Does the given Hash key represent a command?”

/\A\#(.+)$/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(json_document) ⇒ FromJsonClassBuilder

Returns a new instance of FromJsonClassBuilder.

Parameters:

  • json_document (String)

    A JSON document



25
26
27
# File 'lib/democritus/from_json_class_builder.rb', line 25

def initialize(json_document)
  self.data = json_document
end

Instance Method Details

#generate_classObject

A wrapper around the Democritus::ClassBuilder#generate_class. However instead of evaulating blocks, the builder must be called directly.

Returns:

  • Class object



45
46
47
48
49
50
# File 'lib/democritus/from_json_class_builder.rb', line 45

def generate_class
  keywords, nested_commands = extract_keywords_and_nested_commands(node: data)
  class_builder = ClassBuilder.new(**keywords)
  build(node: nested_commands, class_builder: class_builder)
  class_builder.generate_class
end