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

Returns a new instance of Parser.

Parameters:

  • file_path (String)

    the path of the file to parse



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 returns the #parse response (the collection of Spectro::Spec instances for the given file)

Parameters:

  • file_path (String)

    the path of the file to parse

Returns:

  • (<Spectro::Spec>)

    collection of specs found in the given file path



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>

Looks for specs on the given file and parses them as Spectro::Specs

Returns:

  • (<Spectro::Spec>)

    collection of specs found in the given file path



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

Parameters:

  • raw_spec (String)

    raw spec

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spectro/spec/parser.rb', line 41

def parse_spec raw_spec
  spec_raw_signature, *spec_raw_desc_and_rules  = raw_spec.split("\n").reject(&:empty?)

  spec_signature = self.parse_spec_signature(spec_raw_signature)

  spec_raw_description = spec_raw_desc_and_rules.take_while do |desc_or_rule|
    desc_or_rule.match(/^`/)
  end

  spec_description = self.parse_spec_description(spec_raw_description)

  spec_raw_rules = spec_raw_desc_and_rules - spec_raw_description

  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_description, spec_rules)
end

#parse_spec_description(spec_raw_description) ⇒ String

Returns the spec description from the raw spec description

Parameters:

  • spec_raw_description (String)

    spec’s raw description

Returns:

  • (String)

    spec description



67
68
69
70
71
72
73
74
75
# File 'lib/spectro/spec/parser.rb', line 67

def parse_spec_description(spec_raw_description)
  return spec_raw_description.collect do |raw_description|
    if raw_description[1..-1].empty?
      next "\n"
    end

    next raw_description[1..-1].strip
  end.join(' ').strip.gsub("\n ", "\n")
end

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

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

Parameters:

  • spec_raw_rule (String)

    raw rule of the spec

Returns:



81
82
83
84
85
86
87
88
89
90
# File 'lib/spectro/spec/parser.rb', line 81

def parse_spec_rule spec_raw_rule
  # REGEX HERE PLEASE, F%#&!@* 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

Parameters:

  • spec_raw_signature (String)

    raw signature of the spec

  • <Spectro::Spec::Signature] (<Spectro::Spec::Signature] spec signature instance)

    spec signature instance



96
97
98
99
100
101
102
# File 'lib/spectro/spec/parser.rb', line 96

def parse_spec_signature spec_raw_signature
  # REGEX HERE PLEASE, F%#&!@* 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