Permalink
Browse files

Text Parser

- added DIGIT to Regular Expression of term
- added dedicated rspec to test text parsing
- fixed action handling
- fixed getting of classe with regards to dashes
  • Loading branch information...
1 parent c55ae47 commit 6bee64ce04ab39126ca8c249265df2837b21a87b ffeldhaus committed May 12, 2013
Showing with 39 additions and 11 deletions.
  1. +1 −3 lib/occi/core/actions.rb
  2. +7 −6 lib/occi/core/category.rb
  3. +2 −2 lib/occi/parser/text.rb
  4. +29 −0 spec/occi/parser/text_spec.rb
View
4 lib/occi/core/actions.rb
@@ -10,9 +10,7 @@ def convert(category)
if category.kind_of? String
scheme, term = category.split '#'
scheme += '#'
-
- klass = Occi::Core::Category.get_class scheme, term, [Occi::Core::Action.new]
- category = klass.new(scheme, term)
+ category = Occi::Core::Action.new(scheme, term)
end
category
end
View
13 lib/occi/core/category.rb
@@ -50,20 +50,21 @@ def self.get_class(scheme, term, related=['http://schemas.ogf.org/occi/core#enti
end
namespace = namespace.inject(Object) do |mod, name|
- if mod.constants.collect { |sym| sym.to_s }.include? name.classify
- mod.const_get name.classify
+ if mod.constants.collect { |sym| sym.to_s }.include? name.capitalize
+ mod.const_get name.capitalize
else
- mod.const_set name.classify, Module.new
+ mod.const_set name.capitalize, Module.new
end
end
- if namespace.const_defined? term.classify
- klass = namespace.const_get term.classify
+ class_name = term.gsub('-', '_').capitalize
+ if namespace.const_defined? class_name
+ klass = namespace.const_get class_name
unless klass.ancestors.include? Occi::Core::Entity or klass.ancestors.include? Occi::Core::Category
raise "OCCI Kind with type identifier #{scheme + term} could not be created as the corresponding class #{klass.to_s} already exists and is not derived from Occi::Core::Entity"
end
else
- klass = namespace.const_set term.classify, Class.new(parent)
+ klass = namespace.const_set class_name, Class.new(parent)
klass.kind = Occi::Core::Kind.new scheme, term, nil, {}, related unless parent.ancestors.include? Occi::Core::Category
end
View
4 lib/occi/parser/text.rb
@@ -10,7 +10,7 @@ module Text
end
# Regular expressions
- REGEXP_QUOTED_STRING = /([^"\\\\]|\\\\.)*/
+ REGEXP_QUOTED_STRING = /([^"\\]|\\.)*/
REGEXP_LOALPHA = /[a-z]/
REGEXP_DIGIT = /[0-9]/
REGEXP_INT = /#{REGEXP_DIGIT}*/
@@ -19,7 +19,7 @@ module Text
REGEXP_BOOL = /true|false/
# Regular expressions for OCCI
- REGEXP_TERM = /#{REGEXP_LOALPHA}(#{REGEXP_LOALPHA}|-|_)*/
+ REGEXP_TERM = /#{REGEXP_LOALPHA}(#{REGEXP_LOALPHA}|#{REGEXP_DIGIT}|-|_)*/
REGEXP_SCHEME = /#{URI::ABS_URI_REF}#/
REGEXP_TYPE_IDENTIFIER = /#{REGEXP_SCHEME}#{REGEXP_TERM}/
REGEXP_CLASS = /action|mixin|kind/
View
29 spec/occi/parser/text_spec.rb
@@ -0,0 +1,29 @@
+module Occi
+ module Parser
+ describe Text do
+
+ describe '.category' do
+
+ it 'parses a string to an OCCI Category' do
+ category_string = 'Category: a_a1-_;scheme="http://a.a/a#";class="kind";title="aA1!\"§$%&/()=?`´ß+*#-_.:,;<>";rel="http://a.a/b#a";location="/a1-A/";attributes="a_1-_.a1-_a a-1.a.b";actions="http://a.a/a1#a1 http://a.b1/b1#b2"'
+ category = Occi::Parser::Text.category category_string
+ category.term.should == 'a_a1-_'
+ category.scheme.should == 'http://a.a/a#'
+ category.class.should == Occi::Core::Kind
+ category.title.should == 'aA1!\"§$%&/()=?`´ß+*#-_.:,;<>'
+ category.related.first.should == 'http://a.a/b#a'
+ category.location.should == '/a1-A/'
+ category.attributes['a_1-_'].class.should == Occi::Core::Attributes
+ category.attributes['a_1-_']['a1-_a'].class.should == Occi::Core::AttributeProperties
+ category.attributes['a-1'].class.should == Occi::Core::Attributes
+ category.attributes['a-1']['a'].class.should == Occi::Core::Attributes
+ category.attributes['a-1']['a']['b'].class.should == Occi::Core::AttributeProperties
+ category.actions.to_a.first.to_s == 'http://a.a/a1#a1'
+ category.actions.to_a.last.to_s == 'http://a.b1/b1#b2'
+ end
+
+ end
+
+ end
+ end
+end

0 comments on commit 6bee64c

Please sign in to comment.