class Technologist::YamlParser

Attributes

file_name[R]

Public Class Methods

new(file_name) click to toggle source
# File lib/technologist/yaml_parser.rb, line 7
def initialize(file_name)
  @file_name = file_name
end

Public Instance Methods

rules() click to toggle source
# File lib/technologist/yaml_parser.rb, line 11
def rules
  @rules ||=
    begin
      parsed_rules = from_file.map do |technology, definition|
        definition['rules'].map! do |rule|
          instancify(technology, rule)
        end

        [technology, definition]
      end

      Hash[parsed_rules]
    end
end

Private Instance Methods

from_file() click to toggle source
# File lib/technologist/yaml_parser.rb, line 28
def from_file
  YAML.load_file(file_name)
end
instancify(technology, rule) click to toggle source

Create a class instance for a rule entry

# File lib/technologist/yaml_parser.rb, line 33
def instancify(technology, rule)
  class_name, attributes = send("parse_rule_of_type_#{rule.class.name.downcase}", rule)

  Rule.const_get("#{class_name}Rule").new(technology, attributes)
end
parse_rule_of_type_hash(rule) click to toggle source

Parses a yaml rule where the rule entry is a string, meaning that only the rule class name is given. Sample yaml structure:

```
  Rails:
    rules:
      - Gem
```
# File lib/technologist/yaml_parser.rb, line 47
def parse_rule_of_type_hash(rule)
  class_name = rule.keys.first
  rule.delete(class_name)
  attributes = rule

  [class_name, attributes]
end
parse_rule_of_type_string(rule) click to toggle source

Parses a yaml rule where the rule entry is a hash, meaning that the rule class also has attributes to be assigned. Sample yaml structure:

```
  Rails:
    rules:
      - Gem:
        gem_name: 'jrails'
```
# File lib/technologist/yaml_parser.rb, line 64
def parse_rule_of_type_string(rule)
  class_name = rule
  attributes = {}

  [class_name, attributes]
end