Skip to content
Permalink
Browse files

SC to Spectro!

  • Loading branch information...
robertodecurnex committed Mar 17, 2015
1 parent a6d16d7 commit 650e4948c99fe5ff510cc35149013920742411ac
Showing with 227 additions and 228 deletions.
  1. +7 −8 README.md
  2. +0 −5 bin/sc
  3. +5 −0 bin/spectro
  4. +0 −8 lib/sc/exception.rb
  5. +13 −13 lib/{sc.rb → spectro.rb}
  6. +4 −4 lib/{sc → spectro}/client.rb
  7. +12 −12 lib/{sc → spectro}/compiler.rb
  8. +2 −2 lib/{sc → spectro}/config.rb
  9. +3 −3 lib/{sc → spectro}/database.rb
  10. +8 −0 lib/spectro/exception.rb
  11. +2 −2 lib/{sc → spectro}/exception/undefined_method_definition.rb
  12. +3 −3 lib/{sc → spectro}/mock.rb
  13. +5 −5 lib/{sc → spectro}/spec.rb
  14. +18 −18 lib/{sc → spectro}/spec/parser.rb
  15. +1 −1 lib/{sc → spectro}/spec/rule.rb
  16. +1 −1 lib/{sc → spectro}/spec/signature.rb
  17. 0 sample/{.sc → .spectro}/cache/1db993c17e61ad1fa3a064b34892ff11.rb
  18. 0 sample/{.sc → .spectro}/cache/2e0dcba288b50596b4cdd8680fffbf2e.rb
  19. 0 sample/{.sc → .spectro}/cache/7e46ad07603d4044a8d70c9004c48adf.rb
  20. 0 sample/{.sc → .spectro}/cache/8832fa985fc6ed6453428472a05f50c0.rb
  21. 0 sample/{.sc → .spectro}/cache/aac019283279bcb5f59f9760865f7d77.rb
  22. 0 sample/{.sc → .spectro}/cache/bfc794ba0e2f5aca9a2e4550ad8632e7.rb
  23. 0 sample/{.sc → .spectro}/index.yml
  24. +3 −3 sample/{.sc → .spectro}/undefined.yml
  25. +2 −2 sample/sample.rb
  26. +4 −4 sc.gemspec → spectro.gemspec
  27. 0 test/files/{.sc → .spectro}/cache/1db993c17e61ad1fa3a064b34892ff11.rb
  28. 0 test/files/{.sc → .spectro}/cache/2e0dcba288b50596b4cdd8680fffbf2e.rb
  29. 0 test/files/{.sc → .spectro}/cache/7e46ad07603d4044a8d70c9004c48adf.rb
  30. 0 test/files/{.sc → .spectro}/cache/8832fa985fc6ed6453428472a05f50c0.rb
  31. 0 test/files/{.sc → .spectro}/cache/aac019283279bcb5f59f9760865f7d77.rb
  32. 0 test/files/{.sc → .spectro}/cache/bfc794ba0e2f5aca9a2e4550ad8632e7.rb
  33. 0 test/files/{.sc → .spectro}/index.yml
  34. +2 −2 test/files/sample.rb
  35. +2 −2 test/files/undefined_sample.rb
  36. +0 −19 test/minitest/sc/test_config.rb
  37. +0 −29 test/minitest/sc/test_mock.rb
  38. +4 −4 test/minitest/{sc → spectro}/exception/test_undefined_method_definition.rb
  39. +13 −13 test/minitest/{sc → spectro}/spec/test_parser.rb
  40. +2 −2 test/minitest/{sc → spectro}/spec/test_rule.rb
  41. +2 −2 test/minitest/{sc → spectro}/spec/test_signature.rb
  42. +3 −3 test/minitest/{sc → spectro}/test_client.rb
  43. +14 −14 test/minitest/{sc → spectro}/test_compiler.rb
  44. +19 −0 test/minitest/spectro/test_config.rb
  45. +4 −4 test/minitest/{sc → spectro}/test_database.rb
  46. +1 −1 test/minitest/{sc → spectro}/test_exception.rb
  47. +29 −0 test/minitest/spectro/test_mock.rb
  48. +2 −2 test/minitest/{sc → spectro}/test_spec.rb
  49. +0 −32 test/minitest/test_sc.rb
  50. +32 −0 test/minitest/test_spectro.rb
  51. +5 −5 test/test.rb
@@ -1,21 +1,20 @@
# SC

# Spectro

Specs driven social meta-programming

[![Build Status](https://api.travis-ci.org/robertodecurnex/sc.png)](https://travis-ci.org/robertodecurnex/sc)
[![Code Climate](https://codeclimate.com/github/robertodecurnex/sc/badges/gpa.svg)](https://codeclimate.com/github/robertodecurnex/sc)
[![Test Coverage](https://codeclimate.com/github/robertodecurnex/sc/badges/coverage.svg)](https://codeclimate.com/github/robertodecurnex/sc)
[![YARD Docs](https://img.shields.io/badge/YARD-Docs-blue.svg)](http://www.rubydoc.info/github/robertodecurnex/sc/master)
[![Build Status](https://api.travis-ci.org/robertodecurnex/spectro.png)](https://travis-ci.org/robertodecurnex/spectro)
[![Code Climate](https://codeclimate.com/github/robertodecurnex/spectro/badges/gpa.svg)](https://codeclimate.com/github/robertodecurnex/spectro)
[![Test Coverage](https://codeclimate.com/github/robertodecurnex/spectro/badges/coverage.svg)](https://codeclimate.com/github/robertodecurnex/spectro)
[![YARD Docs](https://img.shields.io/badge/YARD-Docs-blue.svg)](http://www.rubydoc.info/github/robertodecurnex/spectro/master)

## Prototype

```ruby
require 'sc'
require 'spectro'
class Sample
include SC
include Spectro
implements \
hello: [:name]
5 bin/sc

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby

require 'spectro/client'

Spectro::Client.start

This file was deleted.

Oops, something went wrong.
@@ -3,44 +3,44 @@
require 'singleton'
require 'yaml'

require 'sc/config'
require 'sc/database'
require 'sc/exception'
require 'sc/mock'
require 'sc/spec'
require 'spectro/config'
require 'spectro/database'
require 'spectro/exception'
require 'spectro/mock'
require 'spectro/spec'

# Specs driven social meta-programming
module SC
module Spectro

# Extends the caller with the SC class methods on #include
# Extends the caller with the Spectro class methods on #include
def self.included klass
klass.extend(ClassMethods)
end

# Gives access to the SC::Config instance insde the given block
# Gives access to the Spectro::Config instance insde the given block
#
# Usage:
# SC.configure do |config|
# Spectro.configure do |config|
# config.enable_mocks!
# end
def self.configure
yield SC::Config.instance
yield Spectro::Config.instance
end

module ClassMethods

# Register the given method name supporting the given parameters.
#
# Whenever SC::Config.mocks_enabled? is true it will try to cover unfulfilled
# Whenever Spectro::Config.mocks_enabled? is true it will try to cover unfulfilled
# specs using the knwon rules as mocks.
#
# @param [{String, Symbol=><String, Symbol>}] interfaces hash of method names and required param names that the method supports
def implements interfaces
file_path = caller.first.match(/#{Dir.pwd}\/(.+):\d+:in .+/)[1]
interfaces.each do |method_name, required_params|
λ = SC::Database.fetch(file_path, method_name, *required_params) || SC::Mock.create(file_path, method_name)
λ = Spectro::Database.fetch(file_path, method_name, *required_params) || Spectro::Mock.create(file_path, method_name)

raise SC::Exception::UndefinedMethodDefinition.new(file_path, method_name) if λ.nil?
raise Spectro::Exception::UndefinedMethodDefinition.new(file_path, method_name) if λ.nil?

self.send(:define_method, method_name, &λ)
end
@@ -1,14 +1,14 @@
require 'sc'
require 'spectro'
require 'thor'

module SC
module Spectro

class Client < Thor

desc 'compile', 'Parses the current project looking for unfulfilled specs and looks for suitable lambdas in the repos. It then updates the cache with them.'
def compile
require 'sc/compiler'
SC::Compiler.compile
require 'spectro/compiler'
Spectro::Compiler.compile
end

end
@@ -1,11 +1,11 @@
require 'sc'
require 'sc/spec/parser'
require 'spectro'
require 'spectro/spec/parser'
require 'yaml/store'

module SC
module Spectro

# SC:Compiler is in charge of scan the projects and parse its files,
# updating the project SC index and dumping information about the missing
# Spectro:Compiler is in charge of spectroan the projects and parse its files,
# updating the project Spectro index and dumping information about the missing
# implementations (specs without an associated lambda)
class Compiler

@@ -16,13 +16,13 @@ class << self
def_delegators :instance, :compile
end

# Filters the project files keeping those making use of SC.
# Filters the project files keeping those making use of Spectro.
# It them parses those files, check for missing implementations
# and creates an .sc/undefined.yml with the specs of them.
# and creates an .spectro/undefined.yml with the specs of them.
#
# @return [SC::Compiler] self
# @return [Spectro::Compiler] self
def compile
undefined_yaml = YAML::Store.new(".sc/undefined.yml")
undefined_yaml = YAML::Store.new(".spectro/undefined.yml")
undefined_yaml.transaction do
targets().map do |path|
missing_specs = missing_specs_from_file(path)
@@ -42,10 +42,10 @@ def compile
# that have not been fulfilled or need to be updated.
#
# @param [String] path target file path
# @return [<SC::Spec>] collection of specs not fulfilled or out of date
# @return [<Spectro::Spec>] collection of specs not fulfilled or out of date
def missing_specs_from_file(path)
SC::Spec::Parser.parse(path).select do |spec|
index_spec = SC::Database.index[path] && SC::Database.index[path][spec.signature.name]
Spectro::Spec::Parser.parse(path).select do |spec|
index_spec = Spectro::Database.index[path] && Spectro::Database.index[path][spec.signature.name]
index_spec.nil? || index_spec['spec_md5'] != spec.md5
end
end
@@ -1,4 +1,4 @@
module SC
module Spectro

class Config

@@ -13,7 +13,7 @@ class << self

# Sets mocks_enabled to true
#
# @return [SC::Config] self
# @return [Spectro::Config] self
def enable_mocks!
self.mocks_enabled = true
return self
@@ -1,4 +1,4 @@
module SC
module Spectro

# Gives access to the current collection of
# algorithms (lambdas) providing several ways
@@ -22,7 +22,7 @@ def initialize
#
# @return [Hash] the parsed index.yml
def index
@index ||= YAML.load_file('./.sc/index.yml')
@index ||= YAML.load_file('./.spectro/index.yml')
end

# Fetches and return the target lambda based on the
@@ -37,7 +37,7 @@ def fetch file_path, method_name, *required_params
return nil
end
λ_id = self.index["#{file_path}"]["#{method_name}"]['lambda_id']
return self.cache[λ_id] ||= eval(File.read(".sc/cache/#{λ_id}.rb"))
return self.cache[λ_id] ||= eval(File.read(".spectro/cache/#{λ_id}.rb"))
end

end
@@ -0,0 +1,8 @@
require 'spectro/exception/undefined_method_definition'

module Spectro

module Exception
end

end
@@ -1,4 +1,4 @@
module SC
module Spectro

module Exception

@@ -12,7 +12,7 @@ def initialize file_path, method_name
end

def to_s
"[##{self.method_name}] has not been defiend for \"#{self.file_path}\". Verify the specs at \"#{self.file_path}\" and check the `sc compile` output for more detailed information."
"[##{self.method_name}] has not been defiend for \"#{self.file_path}\". Verify the specs at \"#{self.file_path}\" and check the `spectro compile` output for more detailed information."
end

end
@@ -1,4 +1,4 @@
module SC
module Spectro

class Mock

@@ -10,8 +10,8 @@ class Mock
# @param [Symbol] method_name the method name that would be implemented
# @return [NilClass, Proc] the mock as Proc or nil if mocks are disabled
def self.create file_path, method_name
return nil unless SC::Config.mocks_enabled?
spec = SC::Spec::Parser.parse(file_path).detect do |spec|
return nil unless Spectro::Config.mocks_enabled?
spec = Spectro::Spec::Parser.parse(file_path).detect do |spec|
spec.signature.name == method_name
end
param_names = Array.new(spec.signature.params_types.count) do |index|
@@ -1,15 +1,15 @@
require 'sc/spec/rule'
require 'sc/spec/signature'
require 'spectro/spec/rule'
require 'spectro/spec/signature'

module SC
module Spectro

class Spec

attr_accessor :md5, :rules, :signature

# @param [String] spec md5
# @param [SC::Spec::Signature] signature spec signature
# @param [<SC::Spec::Rule>] rules collection of spec rules
# @param [Spectro::Spec::Signature] signature spec signature
# @param [<Spectro::Spec::Rule>] rules collection of spec rules
def initialize md5, signature, rules
self.md5 = md5
self.rules = rules
@@ -1,10 +1,10 @@
require 'sc'
require 'spectro'

module SC
module Spectro

class Spec

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

attr_accessor :file_path
@@ -14,30 +14,30 @@ def initialize file_path
self.file_path = file_path
end

# Create an instance of SC::Spec::Parser for the given file path
# and return the #parse response (the collection of SC::Spec instances
# 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)
#
# @param [String] file_path the path of the file to parse
# @return [<SC::Spec>] collection of specs found in the given file path
# @return [<Spectro::Spec>] collection of specs found in the given file path
def self.parse(file_path)
SC::Spec::Parser.new(file_path).parse
Spectro::Spec::Parser.new(file_path).parse
end

# Look for specs on the given file and parse them as SC::Specs
# Look for specs on the given file and parse them as Spectro::Specs
#
# @return [<SC::Spec>] collection of specs found in the given file path
# @return [<Spectro::Spec>] collection of specs found in the given file path
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

# Parses a raw spec and returns an SC::Spec instance
# Parses a raw spec and returns an Spectro::Spec instance
#
# @param [String] raw_spec raw spec
# @return [SC::Spec] the SC::Spec instance
# @return [Spectro::Spec] the Spectro::Spec instance
def parse_spec raw_spec
spec_raw_signature, *spec_raw_rules = raw_spec.split("\n").reject(&:empty?)

@@ -49,13 +49,13 @@ def parse_spec raw_spec

spec_md5 = Digest::MD5.hexdigest(raw_spec)

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

# Returns a SC::Spec::Rule instance from the raw spec rule
# Returns a Spectro::Spec::Rule instance from the raw spec rule
#
# @param [String] spec_raw_rule raw rule if the spec
# @return [SC::Spec::Rule]
# @return [Spectro::Spec::Rule]
def parse_spec_rule spec_raw_rule
# REGEX HERE PLEASE, F%#&!@* EASY
raw_params, raw_output = spec_raw_rule.split('->').map(&:strip)
@@ -64,19 +64,19 @@ def parse_spec_rule spec_raw_rule
eval(raw_param)
end

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

# Returns a SC::Spec::Signature from the raw spec signature
# Returns a Spectro::Spec::Signature from the raw spec signature
#
# @param [String] spec_raw_signature raw signature of the spec
# @param [<SC::Spec::Signature]
# @param [<Spectro::Spec::Signature]
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 SC::Spec::Signature.new(name, params_types, output_type)
return Spectro::Spec::Signature.new(name, params_types, output_type)
end

end
@@ -1,4 +1,4 @@
module SC
module Spectro

class Spec

@@ -1,4 +1,4 @@
module SC
module Spectro

class Spec

File renamed without changes.
@@ -1,13 +1,13 @@
---
sample.rb:
- !ruby/object:SC::Spec
- !ruby/object:Spectro::Spec
md5: 94dd639208a00598a7248336398ad769
rules:
- !ruby/object:SC::Spec::Rule
- !ruby/object:Spectro::Spec::Rule
output: true
params:
- false
signature: !ruby/object:SC::Spec::Signature
signature: !ruby/object:Spectro::Spec::Signature
name: unknown_lambda
output_type: TrueClass
params_types:
Oops, something went wrong.

0 comments on commit 650e494

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.