Class: Spectro::Spec::Parser

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

Overview

Parser to get Spectro::Spec instances from the metadata on the program's files

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Parser


13
14
15
# File 'lib/spectro/spec/parser.rb', line 13

def initialize file_path
  self.file_path = file_path
end

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path


10
11
12
# File 'lib/spectro/spec/parser.rb', line 10

def file_path
  @file_path
end

Class Method Details

.parse(file_path) ⇒ <Spectro::Spec>

Create an instance of Spectro::Spec::Parser for the given file path and return the #parse response (the collection of Spectro::Spec instances for the given file)


23
24
25
# File 'lib/spectro/spec/parser.rb', line 23

def self.parse(file_path)
  Spectro::Spec::Parser.new(file_path).parse
end

Instance Method Details

#parse<Spectro::Spec>

Look for specs on the given file and parse them as Spectro::Specs


30
31
32
33
34
35
# File 'lib/spectro/spec/parser.rb', line 30

def parse
  /.*^__END__$(?<raw_specs>.*)\Z/m =~ File.read(self.file_path)
  return raw_specs.split('spec_for')[1..-1].map do |raw_spec|
    self.parse_spec raw_spec
  end
end

#parse_spec(raw_spec) ⇒ Spectro::Spec

Parses a raw spec and returns an Spectro::Spec instance


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spectro/spec/parser.rb', line 41

def parse_spec raw_spec
  spec_raw_signature, *spec_raw_rules = raw_spec.split("\n").reject(&:empty?)
  
  spec_signature = self.parse_spec_signature(spec_raw_signature)

  spec_rules = spec_raw_rules.map do |spec_raw_rule|
    self.parse_spec_rule(spec_raw_rule)
  end

  spec_md5 = Digest::MD5.hexdigest(raw_spec)

  return Spectro::Spec.new(spec_md5, spec_signature, spec_rules)
end

#parse_spec_rule(spec_raw_rule) ⇒ Spectro::Spec::Rule

Returns a Spectro::Spec::Rule instance from the raw spec rule


59
60
61
62
63
64
65
66
67
68
# File 'lib/spectro/spec/parser.rb', line 59

def parse_spec_rule spec_raw_rule
  # REGEX HERE PLEASE, F%#&[email protected]* EASY
  raw_params, raw_output = spec_raw_rule.split('->').map(&:strip) 
  output = eval(raw_output)
  params = raw_params.split(/,\s+/).map do |raw_param|
    eval(raw_param)
  end

  return Spectro::Spec::Rule.new(params, output)
end

#parse_spec_signature(spec_raw_signature) ⇒ Object

Returns a Spectro::Spec::Signature from the raw spec signature


74
75
76
77
78
79
80
# File 'lib/spectro/spec/parser.rb', line 74

def parse_spec_signature spec_raw_signature
  # REGEX HERE PLEASE, F%#&[email protected]* EASY
  raw_name_and_params_types, output_type = spec_raw_signature.split('->').map(&:strip)
  name, *params_types = raw_name_and_params_types.split(/,?\s+/).map(&:strip)

  return Spectro::Spec::Signature.new(name, params_types, output_type)
end