Permalink
Browse files

Introduced SpecificationSet, ObjectSpecification, ActionSpecification

  • Loading branch information...
1 parent 9051a80 commit 6f031627ac0e478c844e5ffa987032692b7a112b @krakatoa krakatoa committed Sep 24, 2012
View
4 Gemfile
@@ -3,13 +3,13 @@ source "http://rubygems.org"
# Example:
# gem "activesupport", ">= 2.3.5"
+gem "inifile"
gem "msgpack"
# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
- gem "shoulda", ">= 0"
- gem "rdoc", "~> 3.12"
+ gem "rspec"
gem "bundler", "~> 1.1.3"
gem "jeweler", "~> 1.8.4"
gem "simplecov"
View
24 Gemfile.lock
@@ -1,11 +1,9 @@
GEM
remote: http://rubygems.org/
specs:
- activesupport (3.2.8)
- i18n (~> 0.6)
- multi_json (~> 1.0)
+ diff-lcs (1.1.3)
git (1.2.5)
- i18n (0.6.1)
+ inifile (2.0.2)
jeweler (1.8.4)
bundler (~> 1.0)
git (>= 1.2.5)
@@ -17,12 +15,14 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
- shoulda (3.1.1)
- shoulda-context (~> 1.0)
- shoulda-matchers (~> 1.2)
- shoulda-context (1.0.0)
- shoulda-matchers (1.3.0)
- activesupport (>= 3.0.0)
+ rspec (2.11.0)
+ rspec-core (~> 2.11.0)
+ rspec-expectations (~> 2.11.0)
+ rspec-mocks (~> 2.11.0)
+ rspec-core (2.11.1)
+ rspec-expectations (2.11.2)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.11.1)
simplecov (0.6.4)
multi_json (~> 1.0)
simplecov-html (~> 0.5.3)
@@ -33,8 +33,8 @@ PLATFORMS
DEPENDENCIES
bundler (~> 1.1.3)
+ inifile
jeweler (~> 1.8.4)
msgpack
- rdoc (~> 3.12)
- shoulda
+ rspec
simplecov
View
7 lib/protocol.rb
@@ -1,4 +1,9 @@
module Cucub
- class Protocol
+ module Protocol
end
end
+
+require 'protocol/action_specification'
+require 'protocol/object_specification'
+require 'protocol/specification_set'
+require 'protocol/loader'
View
6 lib/protocol/action_specification.rb
@@ -0,0 +1,6 @@
+module Cucub
+ module Protocol
+ class ActionSpecification
+ end
+ end
+end
View
31 lib/protocol/loader.rb
@@ -0,0 +1,31 @@
+module Cucub
+ module Protocol
+ class Loader
+ def initialize(filepath)
+ @filepath = filepath
+ end
+
+ def parse
+ basefile = IniFile.load(@filepath)
+
+ current_specifications = Cucub::Protocol::SpecificationSet.new
+
+ basefile.sections.reject { |x| x.include?("#") }.each { |section|
+ object_spec = parse_object_specification(section, basefile[section])
+ current_specifications.add_object_specification(object_spec)
+ }
+
+ current_specifications
+ end
+
+ def parse_object_specification(class_name, section)
+ object_specification = Cucub::Protocol::ObjectSpecification.new(class_name)
+
+ section.each_pair { |key, value|
+ object_specification.send("#{key}=".to_sym, value)
+ }
+ object_specification
+ end
+ end
+ end
+end
View
97 lib/protocol/object_specification.rb
@@ -0,0 +1,97 @@
+module Cucub
+ module Protocol
+ class ObjectSpecification
+ attr_reader :role, :uses_box, :uses_mailbox, :uses_board, :serialize, :compression, :encryption
+
+ def initialize(class_name)
+ @class_name = class_name
+
+ @role = :both # both receiver emitter
+
+ @uses_box = false
+ @uses_mailbox = false
+ @uses_board = false
+
+ @serialize = :none # msgpack
+ @compression = :none # gzip
+ @encryption = :none #
+ end
+
+ def role=(role)
+ role = role.to_sym
+ raise "invalid role!" if ![:both, :receiver, :emitter].include?(role)
+ @role = role
+ end
+
+ def uses_box=(uses_box)
+ if uses_box.is_a?(TrueClass) || uses_box.is_a?(FalseClass)
+ @uses_box = uses_box
+ elsif uses_box.is_a? String
+ case uses_box
+ when "true"
+ @uses_box = true
+ when "false"
+ @uses_box = false
+ else
+ raise "invalid value for uses_box!"
+ end
+ else
+ raise "invalid value for uses_box!"
+ end
+ end
+
+ def uses_mailbox=(uses_mailbox)
+ if uses_mailbox.is_a?(TrueClass) || uses_mailbox.is_a?(FalseClass)
+ @uses_mailbox = uses_mailbox
+ elsif uses_mailbox.is_a? String
+ case uses_mailbox
+ when "true"
+ @uses_mailbox = true
+ when "false"
+ @uses_mailbox = false
+ else
+ raise "invalid value for uses_mailbox!"
+ end
+ else
+ raise "invalid value for uses_mailbox!"
+ end
+ end
+
+ def uses_board=(uses_board)
+ if uses_board.is_a?(TrueClass) || uses_board.is_a?(FalseClass)
+ @uses_board = uses_board
+ elsif uses_board.is_a? String
+ case uses_board
+ when "true"
+ @uses_board = true
+ when "false"
+ @uses_board = false
+ else
+ raise "invalid value for uses_board!"
+ end
+ else
+ raise "invalid value for uses_board!"
+ end
+ end
+
+ def serialize=(serialization)
+ serialization = serialization.to_sym
+ raise "invalid serialization type!" if ![:none, :msgpack].include?(serialization)
+ @serialization = serialization
+ end
+
+ def compression=(compression)
+ compression = compression.to_sym
+ raise "invalid compression type!" if ![:none, :gzip].include?(compression)
+ @compression = compression
+ end
+
+ def encryption=(encryption)
+ encryption = encryption.to_sym
+ raise "invalid encryption type!" if ![:none].include?(encryption)
+ @encryption = encryption
+ end
+
+ end
+ end
+end
View
17 lib/protocol/specification_set.rb
@@ -0,0 +1,17 @@
+module Cucub
+ module Protocol
+ class SpecificationSet
+ def initialize
+ @object_specifications = []
+ end
+
+ def add_object_specification(obj_spec)
+ @object_specifications.push obj_spec
+ end
+
+ def object_specifications
+ @object_specifications
+ end
+ end
+ end
+end
View
10 spec/mock/protocol.ini
@@ -0,0 +1,10 @@
+[engine]
+ role = receiver
+ uses_box = true
+; cp = 1425
+
+[engine#create_vps]
+ test = "blo"
+
+[agent]
+ uses_box = true
View
72 spec/protocol_spec.rb
@@ -0,0 +1,72 @@
+require 'spec_helper'
+
+describe Cucub::Protocol::Loader do
+ before do
+ @filepath = File.dirname(File.expand_path(__FILE__))
+ end
+
+ describe "#parse" do
+ it "should return a valid SpecificationSet" do
+ @loader = Cucub::Protocol::Loader.new(@filepath + "/mock/protocol.ini")
+ specification_set = @loader.parse
+ puts specification_set.object_specifications.inspect
+ end
+ end
+
+=begin
+ describe 'run!' do
+ it 'should pass' do
+ constraint = Bliss::Constraint.new("root", :tag_name_required)
+ constraint.run!({'root' => {'tag_1' => 'test', 'tag_2' => 'test'}})
+ constraint.state.should == :passed
+ end
+
+ it 'should pass too' do
+ constraint = Bliss::Constraint.new("(root|ROOT)", :tag_name_required)
+ constraint.run!({'ROOT' => {'tag_1' => 'test', 'tag_2' => 'test'}})
+ constraint.state.should == :passed
+ end
+
+ it 'should not pass' do
+ constraint = Bliss::Constraint.new("(root|ROOT)", :tag_name_required)
+ constraint.run!({'another' => {'tag_1' => 'test', 'tag_2' => 'test'}})
+ constraint.state.should == :not_passed
+ end
+
+ context "multiple depth levels" do
+ it "should pass" do
+ constraint = Bliss::Constraint.new("(root|ROOT)/id", :tag_name_required)
+ constraint.run!({'id' => 'test', 'tag_2' => 'test'})
+ constraint.state.should == :passed
+ end
+
+ it "should not pass" do
+ constraint = Bliss::Constraint.new("(root|ROOT)/id", :tag_name_required)
+ constraint.run!({'root' => {'tag_1' => 'test', 'tag_2' => 'test'}})
+ constraint.state.should == :not_passed
+ end
+ end
+ end
+
+ describe '#settings_to_constraints' do
+ it 'should return an array with a Bliss::Constraint object' do
+ constraints = Bliss::Constraint.build_from_settings(['root'], {'tag_name_required' => true})
+ constraints.should be_a(Array)
+ constraints.size.should == 1
+ constraints.first.should be_a(Bliss::Constraint)
+ end
+
+ it 'should have depth and setting loaded' do
+ constraints = Bliss::Constraint.build_from_settings(['root'], {'tag_name_required' => true})
+ constraints.first.depth.should == 'root'
+ constraints.first.setting.should == :tag_name_required
+ end
+
+ it 'should have multiple depths' do
+ constraints = Bliss::Constraint.build_from_settings(['root'], {'tag_name_required' => true, 'tag_name_values' => ['root', 'ROOT']})
+ constraints.first.depth.should == '(root|ROOT)'
+ constraints.first.setting.should == :tag_name_required
+ end
+ end
+=end
+end
View
16 spec/spec_helper.rb
@@ -0,0 +1,16 @@
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'rspec'
+
+require 'bundler'
+Bundler.require(:default, :development)
+
+Dir["#{File.dirname(__FILE__)}/../lib/**/*.rb"].each {|f| require f}
+
+# Requires supporting files with custom matchers and macros, etc,
+# in ./support/ and its subdirectories.
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+
+RSpec.configure do |config|
+
+end

0 comments on commit 6f03162

Please sign in to comment.