xml_schema 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ff48690d0cb01933c7741f459b3e2f0f5659b345
4
+ data.tar.gz: 3ee04f08574b8348ab331b95d944167eb34780a6
5
+ SHA512:
6
+ metadata.gz: 7dee738cb196c977812d2753394c8fc614abe4dffd2ca2125e5a5d2392825a405975b946069cfbe4ef26f8dc3a7d81813d06d6caf90b12903378d30e0427d0a6
7
+ data.tar.gz: aad15ed12b80ca1ef3cbd11c2d87040a1e353b14c6c8f5312802d56a7a110fb52791956da76d44fd23632fcec2831fb324677db45d61796b61a640c17823bda9
data/ChangeLog CHANGED
@@ -1,3 +1,7 @@
1
+ xml_schema (0.2.0)
2
+
3
+ * introducing LocalizedString to handle string literals like "hello@en"
4
+
1
5
  xml_schema (0.1.2)
2
6
 
3
7
  * fix for comparing URIs ending in "/"
@@ -0,0 +1,41 @@
1
+ # XML Schema
2
+
3
+ `XmlSchema` gem is a simple convertor utility for XML Schema (literal) datatypes to/from native Ruby objects.
4
+
5
+
6
+ # Usage
7
+
8
+ $ require "xml_schema"
9
+
10
+ $ XmlSchema.datatype_of(true) # => #<URI::HTTP:0x00000000dcff78 URL:http://www.w3.org/2001/XMLSchema#boolean>
11
+ $ XmlSchema.datatype_of(Time.now) # => #<URI::HTTP:0x00000000dcff78 URL:http://www.w3.org/2001/XMLSchema#time>
12
+
13
+ $ XmlSchema.instantiate("55^^<http://www.w3.org/2001/XMLSchema#int>") # => 55
14
+
15
+ XmlSchema also provides means to access and work with frequently used namespaces:
16
+
17
+ $ NS::OWL.uri # => #<URI::HTTP:0x00000000dcff78 URL:http://www.w3.org/2002/07/owl>
18
+ $ NS::RDFS["hello"] # => #<URI::HTTP:0x00000000dcff78 URL:http://www.w3.org/2000/01/rdf-schema#hello>
19
+
20
+ That's pretty much all there is to it.
21
+
22
+
23
+ # Extras
24
+
25
+ This gem introduces LocalizedString to handle localized string literals.
26
+
27
+ $ s = LocalizedString.new("hello", :en)
28
+ $ s.lang # => :en
29
+ $ XMLSchema.instantiate("hello@en") # => <LocalizedString: "hello">
30
+
31
+ When compared to a String object, language is ignored and only the string content is compared.
32
+ When compared to another LocalizedString, languages are also compared.
33
+
34
+ You can also initialize a LocalizedString as follows:
35
+
36
+ $ "bonjour".with_lang(:fr) # => "bonjour"
37
+
38
+
39
+ # Authors
40
+
41
+ - [Slava Kravchenko](https://github.com/cordawyn)
@@ -0,0 +1,30 @@
1
+ # LocalizedString is just a common String with "lang" attribute,
2
+ # which specifies the language of the text stored in the string.
3
+ #
4
+ # @example
5
+ # str = LocalizedString.new("Hello", :en)
6
+ # "Bonjour".with_lang(:fr) == str # => false
7
+ class LocalizedString < String
8
+ attr_reader :lang
9
+
10
+ def initialize(str, lng = nil)
11
+ @lang = lng
12
+ super str
13
+ end
14
+
15
+ # In addition to comparing string values
16
+ # LocalizedString also compares "lang" values.
17
+ # When compared to a common String,
18
+ # "lang" value is ignored.
19
+ #
20
+ # @param [String, LocalizedString] str
21
+ # @return [Boolean]
22
+ def eql?(str)
23
+ if str.respond_to?(:lang)
24
+ self.lang == str.lang && super
25
+ else
26
+ super
27
+ end
28
+ end
29
+ alias :== :eql?
30
+ end
@@ -0,0 +1,11 @@
1
+ class String
2
+ # Helper method to set the language of the string.
3
+ #
4
+ # @example
5
+ # "Bonjour".with_lang(:fr).sub(/.$/, "s")
6
+ #
7
+ # @return [self]
8
+ def with_lang(lng)
9
+ LocalizedString.new(self, lng)
10
+ end
11
+ end
@@ -1,15 +1,15 @@
1
1
  require 'time'
2
2
  require 'xml_schema/ns'
3
+ require 'ext/string'
4
+ require 'ext/localized_string'
3
5
 
4
6
  module XmlSchema
5
7
  TYPES = %w(integer boolean float dateTime time date string decimal double duration gYearMonth gYear gMonthDay gDay gMonth hexBinary base64Binary anyURI QName NOTATION)
6
8
 
7
-
8
9
  # Obtain XML Schema datatype URI for a Ruby object.
9
10
  def self.datatype_of(object)
10
11
  #--
11
12
  # TODO: decimal, double, duration (Range?), gYearMonth, gYear, gMonthDay, gDay, gMonth, hexBinary, base64Binary, anyURI, QName, NOTATION
12
- # TODO: language tag?
13
13
  #++
14
14
  case object.class.name
15
15
  when "Fixnum", "Integer"
@@ -24,7 +24,7 @@ module XmlSchema
24
24
  NS::XMLSchema['time']
25
25
  when "Date"
26
26
  NS::XMLSchema['date']
27
- when "String"
27
+ when "String", "LocalizedString"
28
28
  NS::XMLSchema['string']
29
29
  else
30
30
  raise "#{object.class} cannot be coerced into any XMLSchema datatype"
@@ -36,7 +36,15 @@ module XmlSchema
36
36
  # +literal+ is interpreted as RDF typed literal (with datatype postfix).
37
37
  def self.instantiate(literal, datatype_uri = nil)
38
38
  # NOTE: removing language tag (e.g. "@en")
39
- full_literal = datatype_uri ? literal.sub(/@\w+$/, '') + "^^<#{datatype_uri}>" : literal.sub(/@\w+$/, '')
39
+ lang_regexp = /@(\w+{2})$/
40
+ lang = nil
41
+ if literal =~ lang_regexp
42
+ lang = $1
43
+ # make a local copy of literal, with removed lang tag
44
+ literal = literal.sub(lang_regexp, "")
45
+ end
46
+
47
+ full_literal = datatype_uri ? literal + "^^<#{datatype_uri}>" : literal
40
48
  literal_value, literal_type = full_literal.split('^^')
41
49
  datatype =
42
50
  if literal_type
@@ -51,7 +59,7 @@ module XmlSchema
51
59
  'string'
52
60
  end
53
61
 
54
- # clean-up the literal_value
62
+ # clean-up literal_value
55
63
  literal_value.sub!(/^["']/, '')
56
64
  literal_value.sub!(/["']$/, '')
57
65
 
@@ -69,8 +77,12 @@ module XmlSchema
69
77
  when 'float'
70
78
  literal_value.to_f
71
79
  else
72
- # FIXME: fallback for unknown datatypes and 'string'
73
- literal_value
80
+ if lang
81
+ LocalizedString.new(literal_value, lang.to_sym)
82
+ else
83
+ # FIXME: fallback for unknown datatypes and 'string'
84
+ literal_value
85
+ end
74
86
  end
75
87
  end
76
88
  end
@@ -1,3 +1,3 @@
1
1
  module XmlSchema
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,27 @@
1
+ require "test_helper"
2
+
3
+ class LocalizedStringTest < Test::Unit::TestCase
4
+ def test_initialized_with_no_lang
5
+ assert_equal nil, LocalizedString.new("hello").lang
6
+ end
7
+
8
+ def test_initialized_with_given_locale
9
+ assert_equal :de, LocalizedString.new("tag", :de).lang
10
+ end
11
+
12
+ def test_compared_to_another_localized_string
13
+ assert_equal LocalizedString.new("hello"), LocalizedString.new("hello")
14
+ end
15
+
16
+ def test_does_not_compare_to_localized_string_in_another_language
17
+ assert_not_equal LocalizedString.new("hello"), LocalizedString.new("hello", :ja)
18
+ end
19
+
20
+ def test_compared_to_string
21
+ assert_equal LocalizedString.new("hello"), "hello"
22
+ end
23
+
24
+ def test_initialized_via_string_with_lang
25
+ assert "hello".with_lang(:en).is_a?(LocalizedString)
26
+ end
27
+ end
@@ -41,6 +41,12 @@ class XmlSchemaTest < Test::Unit::TestCase
41
41
  assert_equal "hello", value
42
42
  end
43
43
 
44
+ def test_should_instantiate_a_localized_string
45
+ value = XmlSchema.instantiate "bonjour@fr"
46
+ assert_equal "bonjour", value
47
+ assert_equal :fr, value.lang
48
+ end
49
+
44
50
  def test_should_instantiate_a_string_by_default
45
51
  value = XmlSchema.instantiate "ahoy!"
46
52
  assert_equal "ahoy!", value
@@ -80,6 +86,10 @@ class XmlSchemaTest < Test::Unit::TestCase
80
86
  assert_equal URI("http://www.w3.org/2001/XMLSchema#string"), XmlSchema.datatype_of("test")
81
87
  end
82
88
 
89
+ def test_should_return_datatype_for_localized_string
90
+ assert_equal URI("http://www.w3.org/2001/XMLSchema#string"), XmlSchema.datatype_of("test".with_lang(:de))
91
+ end
92
+
83
93
  def test_should_raise_exception_on_unknown_class
84
94
  assert_raise RuntimeError do
85
95
  XmlSchema.datatype_of(Object.new)
@@ -1,5 +1,3 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
1
  $:.unshift(File.dirname(__FILE__)) unless
4
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
5
3
 
@@ -20,5 +18,7 @@ Gem::Specification.new do |s|
20
18
  s.files = `git ls-files`.split("\n")
21
19
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
20
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.license = "The MIT License (MIT)"
22
+ s.extra_rdoc_files = ['README.md']
23
23
  s.require_paths = ["lib"]
24
24
  end
metadata CHANGED
@@ -1,59 +1,62 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xml_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Slava Kravchenko
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-07-31 00:00:00.000000000 Z
11
+ date: 2013-03-13 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Helper methods and constants to handle XmlSchema
15
14
  email:
16
15
  - slava.kravchenko@gmail.com
17
16
  executables: []
18
17
  extensions: []
19
- extra_rdoc_files: []
18
+ extra_rdoc_files:
19
+ - README.md
20
20
  files:
21
21
  - .gitignore
22
22
  - ChangeLog
23
23
  - Gemfile
24
24
  - Gemfile.lock
25
25
  - LICENSE
26
- - README.rdoc
26
+ - README.md
27
27
  - Rakefile
28
+ - lib/ext/localized_string.rb
29
+ - lib/ext/string.rb
28
30
  - lib/xml_schema.rb
29
31
  - lib/xml_schema/ns.rb
30
32
  - lib/xml_schema/version.rb
33
+ - test/localized_string_test.rb
31
34
  - test/test_helper.rb
32
35
  - test/xml_schema_test.rb
33
36
  - xml_schema.gemspec
34
37
  homepage: https://github.com/cordawyn/xml_schema
35
- licenses: []
38
+ licenses:
39
+ - The MIT License (MIT)
40
+ metadata: {}
36
41
  post_install_message:
37
42
  rdoc_options: []
38
43
  require_paths:
39
44
  - lib
40
45
  required_ruby_version: !ruby/object:Gem::Requirement
41
- none: false
42
46
  requirements:
43
- - - ! '>='
47
+ - - '>='
44
48
  - !ruby/object:Gem::Version
45
49
  version: '0'
46
50
  required_rubygems_version: !ruby/object:Gem::Requirement
47
- none: false
48
51
  requirements:
49
- - - ! '>='
52
+ - - '>='
50
53
  - !ruby/object:Gem::Version
51
54
  version: '0'
52
55
  requirements: []
53
56
  rubyforge_project: xml_schema
54
- rubygems_version: 1.8.24
57
+ rubygems_version: 2.0.0.rc.2
55
58
  signing_key:
56
- specification_version: 3
59
+ specification_version: 4
57
60
  summary: Helper methods and constants to handle XmlSchema
58
61
  test_files: []
59
62
  has_rdoc:
@@ -1,23 +0,0 @@
1
- = XML Schema
2
-
3
- XmlSchema gem is a simple convertor utility for XML Schema (literal) datatypes to/from native Ruby objects.
4
-
5
- = Usage
6
-
7
- $ require "xml_schema"
8
-
9
- $ XmlSchema.datatype_of(true) # => #<URI::HTTP:0x00000000dcff78 URL:http://www.w3.org/2001/XMLSchema#boolean>
10
- $ XmlSchema.datatype_of(Time.now) # => #<URI::HTTP:0x00000000dcff78 URL:http://www.w3.org/2001/XMLSchema#time>
11
-
12
- $ XmlSchema.instantiate("55^^<http://www.w3.org/2001/XMLSchema#int>") # => 55
13
-
14
- XmlSchema also provides means to access and work with frequently used namespaces:
15
-
16
- $ NS::OWL.uri # => #<URI::HTTP:0x00000000dcff78 URL:http://www.w3.org/2002/07/owl>
17
- $ NS::RDFS["hello"] # => #<URI::HTTP:0x00000000dcff78 URL:http://www.w3.org/2000/01/rdf-schema#hello>
18
-
19
- That's pretty much all there is to it.
20
-
21
- = Authors
22
-
23
- Slava Kravchenko <slava.kravchenko@gmail.com>