Permalink
Browse files

Merge branch 'add_invalid_descriptor_file_error' of https://github.co…

…m/ruprict/obvious into ruprict-add_invalid_descriptor_file_error
  • Loading branch information...
2 parents 5291967 + 5ed72b7 commit 4da5c84ac5d27bc247b30f52407f8526683178eb @brianknapp brianknapp committed Mar 30, 2013
View
1 .gitignore
@@ -1,5 +1,6 @@
*.gem
*.rbc
+*.swp
.bundle
.config
.yardoc
View
3 lib/generators/application_generator.rb
@@ -30,7 +30,8 @@ def generate
puts 'Creating actions from descriptors... ' unless descriptors.length.zero?
descriptors.each do |file|
- descriptor = Obvious::Generators::Descriptor.new file
+ yaml = YAML.load_file(file)
+ descriptor = Obvious::Generators::Descriptor.new yaml
descriptor.to_file
end
View
34 lib/generators/descriptor.rb
@@ -4,22 +4,25 @@
module Obvious
module Generators
+ class InvalidDescriptorError < StandardError; end
+
class Descriptor
def initialize descriptor
@descriptor = descriptor
end
def to_file
- action = YAML.load_file @descriptor
+ validate_descriptor
+
@jacks, @entities = {}, {}
@code = ''
- action['Code'].each do |entry|
+ @descriptor['Code'].each do |entry|
write_comments_for entry
process_requirements_for entry if entry['requires']
end
- write_action action
+ write_action
end
private
@@ -53,12 +56,12 @@ def process_requirements_for entry
end
end # #process_requirements_for
- def write_action action
+ def write_action
jacks_data = process_jacks
requirements = require_entities
output = %Q{#{requirements}
-class #{action['Action']}
+class #{@descriptor['Action']}
def initialize #{jacks_data[:inputs]}
#{jacks_data[:assignments]} end
@@ -68,24 +71,24 @@ def execute input
end
}
- snake_name = action['Action'].gsub(/(.)([A-Z])/,'\1_\2').downcase
+ snake_name = @descriptor['Action'].gsub(/(.)([A-Z])/,'\1_\2').downcase
filename = "#{Obvious::Generators::Application.instance.dir}/actions/#{snake_name}.rb"
File.open(filename, 'w') {|f| f.write(output) }
- output = %Q{require_relative '../../actions/#{snake_name}'
+ output = %Q{require_relative '../../actions/#{snake_name}'
-describe #{action['Action']} do
+describe #{@descriptor['Action']} do
- it '#{action['Description']}'
+ it '#{@descriptor['Description']}'
it 'should raise an error with invalid input'
end
-}
+ }
- filename = "#{Obvious::Generators::Application.instance.dir}/spec/actions/#{snake_name}_spec.rb"
- File.open(filename, 'w') {|f| f.write(output) }
+ filename = "#{Obvious::Generators::Application.instance.dir}/spec/actions/#{snake_name}_spec.rb"
+ File.open(filename, 'w') {|f| f.write(output) }
end
def process_jacks
@@ -116,6 +119,13 @@ def require_entities
entity_requires
end
+
+ def validate_descriptor
+ raise InvalidDescriptorError unless @descriptor
+ raise InvalidDescriptorError if @descriptor['Code'].nil?
+ raise InvalidDescriptorError if @descriptor['Action'].nil?
+ raise InvalidDescriptorError if @descriptor['Description'].nil?
+ end
end # ::Descriptor
end
end
View
2 lib/obvious/version.rb
@@ -1,3 +1,3 @@
module Obvious
- VERSION = "0.0.8"
+ VERSION = "0.0.9"
end
View
2 obvious.gemspec
@@ -16,4 +16,6 @@ Gem::Specification.new do |gem|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]
+
+ gem.add_development_dependency "rspec"
end
View
BIN spec/.spec_helper.rb.swp
Binary file not shown.
View
34 spec/generators/descriptor_spec.rb
@@ -0,0 +1,34 @@
+require_relative '../../lib/generators/descriptor'
+
+require File.expand_path('spec/spec_helper')
+
+module Obvious
+ module Generators
+ describe Descriptor do
+ subject {Descriptor.new(yaml_file)}
+
+ describe "#to_file" do
+
+ context "when the descriptor is empty" do
+ let( :yaml_file ) { {} }
+
+ it "should raise a meaningful error" do
+ expect {subject.to_file}.to raise_error(InvalidDescriptorError)
+ end
+ end
+
+ ["Action", "Code", "Description"].each do |section|
+ context "when the '#{section}' section is omitted" do
+ let( :yaml_file ) {
+ {"Action" => "Jackson", "Description" => "This is something"}.delete(section)
+ }
+
+ it "should raise a meaningful error" do
+ expect {subject.to_file}.to raise_error(InvalidDescriptorError)
+ end
+ end
+ end
+ end
+ end
+ end
+end

0 comments on commit 4da5c84

Please sign in to comment.