Class: MonkeyKing::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey_king/parser.rb

Instance Method Summary collapse

Instance Method Details

#get_tags(yaml_file) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/monkey_king/parser.rb', line 86

def get_tags(yaml_file)
  tags = []
  nodes = Psych.parse_file(yaml_file)
  # traverse the tree and return
  nodes.each do |n|
    if n.class == Psych::Nodes::Scalar
      unless n.tag.nil?
        tags << n.tag
      end
    end
  end
  tags.uniq
end

#transform(yaml_file) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/monkey_king/parser.rb', line 62

def transform(yaml_file)
  tags = get_tags(yaml_file)
  env_tag_instances={}
  tags.each do |tag|
    command = tag.split(':')[1]
    unless command.nil? or command != 'env'
      class_name = tag.split(':')[2]
      unless class_name.nil?
        tag_class = Class.new(EnvTag)

        # Hacky way to give each class a global uniq name
        random_string = [*('a'..'z')].shuffle[0,32].join
        Object.const_set("EnvTag#{class_name}#{random_string}", tag_class)

        tag_instance = tag_class.new
        tag_instance.register(tag)
        env_tag_instances[tag] = tag_instance
      end
    end
  end
  yaml = YAML.load_file(yaml_file)
  yaml.to_yaml
end