xml-mapping_extensions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4fea3e32640619a2a820288fe42d28a8087062dc
4
+ data.tar.gz: f141e3b7ccc655cc8f2a09141c0e32f266dac8c4
5
+ SHA512:
6
+ metadata.gz: 6530a2a270d47f95a11d909304b0899080cf315d6f6e7929ff2ac5d6400479130d2dfd7be8a5ea77ebbd9450f5ac1f129d11bb2b1aa1e5c0deac41d9b4305b4c
7
+ data.tar.gz: 8db7ece8cf07717ddecb34f837f71dd68792f47e4e593bfd36db241061030839d1b1f4982ae184bcd519c6d4110d5bb966e98442b6cc67b0310fc9273d36ccb3
data/.gitignore ADDED
@@ -0,0 +1,42 @@
1
+ # Ruby defaults
2
+
3
+ /.bundle/
4
+ /.yardoc
5
+ /Gemfile.lock
6
+ /_yardoc/
7
+ /coverage/
8
+ /doc/
9
+ /pkg/
10
+ /spec/reports/
11
+ /tmp/
12
+ *.bundle
13
+ *.so
14
+ *.o
15
+ *.a
16
+ mkmf.log
17
+
18
+ # Rails engine
19
+
20
+ .bundle/
21
+ log/*.log
22
+ spec/dummy/db/*.sqlite3
23
+ spec/dummy/db/*.sqlite3-journal
24
+ spec/dummy/log/*.log
25
+ spec/dummy/tmp/
26
+ spec/dummy/.sass-cache
27
+
28
+ # IntellJ
29
+
30
+ *.iml
31
+ *.ipr
32
+ *.iws
33
+ .rakeTasks
34
+ .idea
35
+
36
+ # Emacs
37
+
38
+ *~
39
+
40
+ # Mac OS
41
+
42
+ .DS_Store
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ # Disable line-length check; it's too easy for the cure to be worse than the disease
2
+ Metrics/LineLength:
3
+ Enabled: False
4
+
5
+ # Disable problematic module documentation check (see https://github.com/bbatsov/rubocop/issues/947)
6
+ Style/Documentation:
7
+ Enabled: false
8
+
9
+ # Allow one line around class body (Style/EmptyLines will still disallow two or more)
10
+ Style/EmptyLinesAroundClassBody:
11
+ Enabled: false
12
+
13
+ # Allow one line around module body (Style/EmptyLines will still disallow two or more)
14
+ Style/EmptyLinesAroundModuleBody:
15
+ Enabled: false
16
+
17
+ # Allow one line around block body (Style/EmptyLines will still disallow two or more)
18
+ Style/EmptyLinesAroundBlockBody:
19
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.2
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown
data/CHANGES.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.1.0 (21 Sep 2015)
2
+
3
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 The Regents of the University of California
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # XML::MappingExtensions [![Build Status](https://travis-ci.org/dmolesUC3/xml-mapping_extensions.png?branch=master)](https://travis-ci.org/dmolesUC3/xml-mapping_extensions) [![Code Climate](https://codeclimate.com/github/dmolesUC3/xml-mapping_extensions.png)](https://codeclimate.com/github/dmolesUC3/xml-mapping_extensions) [![Inline docs](http://inch-ci.org/github/dmolesUC3/xml-mapping_extensions.png)](http://inch-ci.org/github/dmolesUC3/xml-mapping_extensions)
2
+
3
+
4
+ Additional mapping nodes and other utility classes for working with
5
+ [XML::Mapping](http://multi-io.github.io/xml-mapping/).
6
+
7
+ ## Usage
8
+
9
+ Require `xml/mapping_extensions` and extend one of the abstract node
10
+ classes, or use one of the provided implementations.
11
+
12
+ ### Abstract nodes
13
+
14
+ - `NodeBase`: Base class for simple single-attribute nodes that
15
+ convert XML strings to object values.
16
+ - `EnumNodeBase`: maps XML strings to `Ruby::Enum` values
17
+
18
+ Note that you must call `::XML::Mapping.add_node_class` for your new node class
19
+ to be registered with the XML mapping engine.
20
+
21
+ ### Provided implementations
22
+
23
+ - `DateNode`: maps XML Schema dates to `Date` objects
24
+ - `TimeNode`: ISO 8601 strings to `Time` objects
25
+ - `UriNode`: maps URI strings to `URI` objects
26
+ - `MimeTypeNode`: maps MIME type strings to `MIME::Type` objects
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ # ------------------------------------------------------------
2
+ # RSpec
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+
7
+ namespace :spec do
8
+
9
+ desc 'Run all unit tests'
10
+ RSpec::Core::RakeTask.new(:unit) do |task|
11
+ task.rspec_opts = %w(--color --format documentation --order default)
12
+ task.pattern = 'unit/**/*_spec.rb'
13
+ end
14
+
15
+ desc 'Run all acceptance tests'
16
+ RSpec::Core::RakeTask.new(:acceptance) do |task|
17
+ ENV['COVERAGE'] = nil
18
+ task.rspec_opts = %w(--color --format documentation --order default)
19
+ task.pattern = 'acceptance/**/*_spec.rb'
20
+ end
21
+
22
+ task all: [:unit, :acceptance]
23
+ end
24
+
25
+ desc 'Run all tests'
26
+ task spec: 'spec:all'
27
+
28
+ # ------------------------------------------------------------
29
+ # Coverage
30
+
31
+ desc 'Run all unit tests with coverage'
32
+ task :coverage do
33
+ ENV['COVERAGE'] = 'true'
34
+ Rake::Task['spec:unit'].execute
35
+ end
36
+
37
+ # ------------------------------------------------------------
38
+ # RuboCop
39
+
40
+ require 'rubocop/rake_task'
41
+ RuboCop::RakeTask.new
42
+
43
+ # ------------------------------------------------------------
44
+ # TODOs
45
+
46
+ desc 'List TODOs (from spec/todo.rb)'
47
+ RSpec::Core::RakeTask.new(:todo) do |task|
48
+ task.rspec_opts = %w(--color --format documentation --order default)
49
+ task.pattern = 'todo.rb'
50
+ end
51
+
52
+ # ------------------------------------------------------------
53
+ # Defaults
54
+
55
+ desc 'Run unit tests, check test coverage, run acceptance tests, check code style'
56
+ task default: [:coverage, 'spec:acceptance', :rubocop]
@@ -0,0 +1,26 @@
1
+ require_relative 'node_base'
2
+
3
+ require 'time'
4
+
5
+ module XML
6
+ module MappingExtensions
7
+ # XML mapping for XML Schema dates.
8
+ # Known limitation: loses time zone info
9
+ class DateNode < NodeBase
10
+
11
+ # param xml_text [String] an XML Schema date
12
+ # @return [Date] the value as a `Date`
13
+ def to_value(xml_text)
14
+ Date.xmlschema(xml_text)
15
+ end
16
+
17
+ # @param value [Date] the value as a `Date`
18
+ # @return [String] the value as an XML Schema date string, without
19
+ # time zone information
20
+ def to_xml_text(value)
21
+ value.xmlschema
22
+ end
23
+ end
24
+ ::XML::Mapping.add_node_class DateNode
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ require_relative 'node_base'
2
+
3
+ require 'ruby-enum'
4
+
5
+ module XML
6
+ module MappingExtensions
7
+
8
+ # Base class for single-attribute nodes with `Ruby::Enum` values
9
+ #
10
+ # Usage:
11
+ # - extend this class
12
+ # - add an `ENUM_CLASS` constant whose value is the `Ruby::Enum` class
13
+ # to be mapped
14
+ # - call `::XML::Mapping.add_node_class` to add your new node class
15
+ class EnumNodeBase < NodeBase
16
+
17
+ def to_value(xml_text)
18
+ enum_class = self.class::ENUM_CLASS
19
+ enum_class.parse(xml_text)
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'node_base'
2
+
3
+ require 'mime-types'
4
+
5
+ module XML
6
+ module MappingExtensions
7
+
8
+ # Converts MIME type strings to `MIME::Type` objects
9
+ class MimeTypeNode < NodeBase
10
+
11
+ # Converts a MIME type string to a `MIME::Type` object,
12
+ # either the first corresponding value in the `MIME::Types`
13
+ # registry, or a newly created value.
14
+ # @param xml_text the MIME type string
15
+ # @return [MIME::Type] the corresponding `MIME::Type`
16
+ def to_value(xml_text)
17
+ if (mt = MIME::Types[xml_text].first)
18
+ mt
19
+ else
20
+ MIME::Type.new(xml_text)
21
+ end
22
+ end
23
+ end
24
+ ::XML::Mapping.add_node_class MimeTypeNode
25
+ end
26
+ end
@@ -0,0 +1,56 @@
1
+ require 'xml/mapping'
2
+
3
+ module XML
4
+ module MappingExtensions
5
+
6
+ # Base class for simple single-attribute nodes that convert
7
+ # XML strings to object values.
8
+ #
9
+ # Usage:
10
+ # - extend this class
11
+ # - override `to_value` (and, optionally, `to_xml_text`) to do your conversion
12
+ # - call `::XML::Mapping.add_node_class` to add your new node class
13
+ class NodeBase < ::XML::Mapping::SingleAttributeNode
14
+
15
+ # See `::XML::Mapping::SingleAttributeNode#initialize`
16
+ def initialize(*args)
17
+ path, *args = super(*args)
18
+ @path = ::XML::XXPath.new(path)
19
+ args
20
+ end
21
+
22
+ # Implements `::XML::Mapping::SingleAttributeNode#extract_attr_value`.
23
+ # @param xml [Element] The XML element to extract the value from
24
+ # @return [Object, nil] the mapped value, or nil if
25
+ def extract_attr_value(xml)
26
+ xml_text = default_when_xpath_err { @path.first(xml).text }
27
+ to_value(xml_text) if xml_text
28
+ end
29
+
30
+ # Implements `::XML::Mapping::SingleAttributeNode#set_attr_value`.
31
+ # The object value is passed to `to_xml_text` for string conversion.
32
+ # @param xml [Element] The XML element to write to
33
+ # @param value [Object] The value to write
34
+ def set_attr_value(xml, value)
35
+ text = to_xml_text(value)
36
+ @path.first(xml, ensure_created: true).text = text
37
+ end
38
+
39
+ # Override this method to convert XML text to an object value
40
+ # @param _xml_text [String] The XML string to parse
41
+ # @return [Object] The object value
42
+ def to_value(_xml_text)
43
+ fail NoMethodError, "#{self.class} should override #to_value to convert an XML string to an object value"
44
+ end
45
+
46
+ # Override this method to convert object values to XML text.
47
+ # The default implementation simply calls `to_s`.
48
+ # @param value [Object] The object value to convert
49
+ # @return [String] The XML string to write
50
+ def to_xml_text(value)
51
+ value.to_s
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,24 @@
1
+ require_relative 'node_base'
2
+
3
+ module XML
4
+ module MappingExtensions
5
+
6
+ # Maps `Time` objects to ISO 8601 strings.
7
+ class TimeNode < NodeBase
8
+
9
+ # param xml_text [String] an ISO 8601 datetime value
10
+ # @return [Time] the value as a UTC `Time`
11
+ def to_value(xml_text)
12
+ Time.iso8601(xml_text).utc
13
+ end
14
+
15
+ # @param value [Time] the value as a `Time`
16
+ # @return the value as an ISO 8601 string
17
+ def to_xml_text(value)
18
+ value.utc.iso8601
19
+ end
20
+ end
21
+ ::XML::Mapping.add_node_class TimeNode
22
+
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'node_base'
2
+
3
+ require 'uri'
4
+
5
+ module XML
6
+ module MappingExtensions
7
+ # Coverts URI strings to `URI` objects.
8
+ class UriNode < NodeBase
9
+ # @param xml_text [String] the URI string to convert
10
+ # @return [URI] the URI
11
+ # @raise [URI::InvalidURIError] if `xml_text` is not a valid URI.
12
+ def to_value(xml_text)
13
+ URI(xml_text.strip)
14
+ end
15
+ end
16
+ ::XML::Mapping.add_node_class UriNode
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ module XML
2
+ module MappingExtensions
3
+ # The version of this gem
4
+ VERSION = '0.1.0'
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ module XML
2
+ # Additional mapping nodes and other utility classes for working with
3
+ # [XML::Mapping](http://multi-io.github.io/xml-mapping/)
4
+ module MappingExtensions
5
+ Dir.glob(File.expand_path('../mapping_extensions/*.rb', __FILE__), &method(:require))
6
+ end
7
+ end
@@ -0,0 +1,47 @@
1
+ require 'rspec/expectations'
2
+ require 'mime/types'
3
+
4
+ RSpec::Matchers.define :be_time do |expected|
5
+
6
+ def to_string(time)
7
+ time.is_a?(Time) ? time.utc.round(2).iso8601(2) : time.to_s
8
+ end
9
+
10
+ match do |actual|
11
+ if expected
12
+ fail "Expected value #{expected} is not a Time" unless expected.is_a?(Time)
13
+ actual.is_a?(Time) && (to_string(expected) == to_string(actual))
14
+ else
15
+ return actual.nil?
16
+ end
17
+ end
18
+
19
+ failure_message do |actual|
20
+ expected_str = to_string(expected)
21
+ actual_str = to_string(actual)
22
+ "expected time:\n#{expected_str}\n\nbut was:\n#{actual_str}"
23
+ end
24
+ end
25
+
26
+ def to_mime_type(mime_type)
27
+ return nil unless mime_type
28
+ return mime_type if mime_type.is_a?(MIME::Type)
29
+
30
+ mt = MIME::Types[mime_type].first
31
+ return mt if mt
32
+
33
+ MIME::Type.new(mime_type)
34
+ end
35
+
36
+ RSpec::Matchers.define :be_mime_type do |expected|
37
+
38
+ expected_mime_type = to_mime_type(expected)
39
+
40
+ match do |actual|
41
+ actual == expected_mime_type
42
+ end
43
+
44
+ failure_message do |actual|
45
+ "expected MIME type:\n#{expected_mime_type}\nbut was:\n#{actual}"
46
+ end
47
+ end
@@ -0,0 +1,36 @@
1
+ # ------------------------------------------------------------
2
+ # SimpleCov/CodeClimate setup
3
+
4
+ if ENV['COVERAGE']
5
+ if ENV['CODECLIMATE_REPO_TOKEN']
6
+ require 'codeclimate-test-reporter'
7
+ CodeClimate::TestReporter.start
8
+ end
9
+
10
+ require 'simplecov'
11
+ require 'simplecov-console'
12
+
13
+ SimpleCov.minimum_coverage 100
14
+ SimpleCov.start do
15
+ add_filter '/spec/'
16
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
17
+ SimpleCov::Formatter::HTMLFormatter,
18
+ SimpleCov::Formatter::Console,
19
+ ]
20
+ end
21
+ end
22
+
23
+ # ------------------------------------------------------------
24
+ # Rspec configuration
25
+
26
+ RSpec.configure do |config|
27
+ config.raise_errors_for_deprecations!
28
+ config.mock_with :rspec
29
+ end
30
+
31
+ require 'rspec_custom_matchers'
32
+
33
+ # ------------------------------------------------------------
34
+ # XML::MappingExtensions
35
+
36
+ require 'xml/mapping_extensions'
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ module XML
4
+ module MappingExtensions
5
+ class DateNodeSpecElem
6
+ include ::XML::Mapping
7
+ date_node :date, '@date', default_value: nil
8
+
9
+ def self.from_str(date_str)
10
+ xml_string = "<elem date='#{date_str}'/>"
11
+ doc = REXML::Document.new(xml_string)
12
+ load_from_xml(doc.root)
13
+ end
14
+ end
15
+ describe DateNode do
16
+
17
+ def to_date(str)
18
+ DateNodeSpecElem.from_str(str).date
19
+ end
20
+
21
+ def to_text(date)
22
+ elem = DateNodeSpecElem.new
23
+ elem.date = date
24
+ xml = elem.save_to_xml
25
+ xml.attributes['date']
26
+ end
27
+
28
+ it 'parses a date' do
29
+ actual = to_date('2002-09-24')
30
+ expected = Date.new(2002, 9, 24)
31
+ expect(actual).to eq(expected)
32
+ end
33
+
34
+ it 'parses a UTC "zulu" date (time zone designator "Z")' do
35
+ actual = to_date('2002-09-24Z')
36
+ expected = Date.new(2002, 9, 24)
37
+ expect(actual).to eq(expected)
38
+ end
39
+
40
+ it 'parses a date with a numeric timezone offset' do
41
+ actual = to_date('2002-09-24-06:00')
42
+ expected = Date.new(2002, 9, 24)
43
+ expect(actual).to eq(expected)
44
+ end
45
+
46
+ it 'outputs a date' do
47
+ expected = '2002-09-24'
48
+ actual = to_text(Date.new(2002, 9, 24))
49
+ expect(actual).to eq(expected)
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ module XML
4
+ module MappingExtensions
5
+
6
+ class MyEnum
7
+ include Ruby::Enum
8
+
9
+ define :FOO, 'foo'
10
+ define :BAR, 'bar'
11
+ end
12
+
13
+ class MyEnumNode < EnumNodeBase
14
+ ENUM_CLASS = MyEnum
15
+ end
16
+ ::XML::Mapping.add_node_class(MyEnumNode)
17
+
18
+ class EnumNodeBaseSpecElem
19
+ include ::XML::Mapping
20
+ my_enum_node :my_enum, '@my_enum', default_value: nil
21
+
22
+ def self.from_str(enum_str)
23
+ xml_string = enum_str ? "<elem my_enum='#{enum_str}'/>" : '<elem/>'
24
+ doc = REXML::Document.new(xml_string)
25
+ load_from_xml(doc.root)
26
+ end
27
+ end
28
+ describe EnumNodeBase do
29
+
30
+ def to_my_enum(str)
31
+ EnumNodeBaseSpecElem.from_str(str).my_enum
32
+ end
33
+
34
+ def to_text(enum)
35
+ elem = EnumNodeBaseSpecElem.new
36
+ elem.my_enum = enum
37
+ xml = elem.save_to_xml
38
+ xml.attributes['my_enum']
39
+ end
40
+
41
+ it 'parses an enum value' do
42
+ expect(to_my_enum('foo')).to eq(MyEnum::FOO)
43
+ end
44
+
45
+ it 'transforms an enum to a string' do
46
+ expect(to_text(MyEnum::BAR)).to eq('bar')
47
+ end
48
+
49
+ it 'parses a missing value as nil' do
50
+ expect(to_my_enum(nil)).to be_nil
51
+ end
52
+
53
+ it 'doesn\'t set an attribute for a nil value' do
54
+ expect(to_text(nil)).to be_nil
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ module XML
4
+ module MappingExtensions
5
+
6
+ class MimeTypeSpecElem
7
+ include XML::Mapping
8
+ mime_type_node :mime_type, '@mime_type', default_value: nil
9
+
10
+ def self.from_str(mt_string)
11
+ xml_string = mt_string ? "<elem mime_type='#{mt_string}'/>" : '<elem/>'
12
+ doc = REXML::Document.new(xml_string)
13
+ load_from_xml(doc.root)
14
+ end
15
+ end
16
+
17
+ describe MimeTypeNode do
18
+ def to_mime_type(str)
19
+ MimeTypeSpecElem.from_str(str).mime_type
20
+ end
21
+
22
+ def to_text(mime_type)
23
+ elem = MimeTypeSpecElem.new
24
+ elem.mime_type = mime_type
25
+ xml = elem.save_to_xml
26
+ xml.attributes['mime_type']
27
+ end
28
+
29
+ it 'accepts a standard MIME type' do
30
+ mt_str = 'text/plain'
31
+ mt = MIME::Types[mt_str].first
32
+ expect(to_mime_type(mt_str)).to eq(mt)
33
+ end
34
+
35
+ it 'accepts a non-standard MIME type' do
36
+ mt_str = 'elvis/presley'
37
+ mt = MIME::Type.new(mt_str)
38
+ expect(to_mime_type(mt_str)).to eq(mt)
39
+ end
40
+
41
+ it 'doesn\'t set an attribute for a nil value' do
42
+ expect(to_text(nil)).to be_nil
43
+ end
44
+
45
+ it 'fails if mime_type isn\'t a MIME type' do
46
+ mt_str = 'I am not a mime type'
47
+ expect { to_mime_type(mt_str) }.to raise_error(MIME::Type::InvalidContentType)
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ module XML
4
+ module MappingExtensions
5
+
6
+ class SomeMappingClass
7
+ include ::XML::Mapping
8
+ end
9
+
10
+ describe NodeBase do
11
+ before :each do
12
+ @node = NodeBase.new(SomeMappingClass, :attr_name, 'attr_name')
13
+ end
14
+
15
+ describe '#extract_attr_value' do
16
+ it 'forwards to #to_value'
17
+ end
18
+
19
+ describe '#set_attr_value' do
20
+ it 'forwards to #to_xml_text'
21
+ end
22
+
23
+ describe '#xml_text' do
24
+ it 'should be abstract' do
25
+ expect { @node.to_value('some text') }.to raise_error(NoMethodError)
26
+ end
27
+ end
28
+
29
+ describe '#to_xml_text' do
30
+ it 'should call to_s by default' do
31
+ values = ['elvis', 123, Object.new]
32
+ values.each do |v|
33
+ expect(@node.to_xml_text(v)).to eq(v.to_s)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ module XML
4
+ module MappingExtensions
5
+ class TimeNodeSpecElem
6
+ include ::XML::Mapping
7
+ time_node :time, '@time', default_value: nil
8
+
9
+ def self.from_str(time_str)
10
+ xml_string = "<elem time='#{time_str}'/>"
11
+ doc = REXML::Document.new(xml_string)
12
+ load_from_xml(doc.root)
13
+ end
14
+ end
15
+ describe TimeNode do
16
+
17
+ def to_time(str)
18
+ TimeNodeSpecElem.from_str(str).time
19
+ end
20
+
21
+ def to_text(time)
22
+ elem = TimeNodeSpecElem.new
23
+ elem.time = time
24
+ xml = elem.save_to_xml
25
+ xml.attributes['time']
26
+ end
27
+
28
+ it 'parses a date with hours, minutes, and seconds' do
29
+ actual = to_time('1997-07-16T19:20:30')
30
+ expected = Time.new(1997, 7, 16, 19, 20, 30)
31
+ expect(actual).to be_time(expected)
32
+ end
33
+
34
+ it 'parses a date with hours, minutes, seconds, and fractional seconds' do
35
+ actual = to_time('1997-07-16T19:20:30.45')
36
+ expected = Time.new(1997, 7, 16, 19, 20, 30.45)
37
+ expect(actual).to be_time(expected)
38
+ end
39
+
40
+ it 'parses a UTC "zulu" time (time zone designator "Z")' do
41
+ actual = to_time('1997-07-16T19:20:30.45Z')
42
+ expected = Time.new(1997, 7, 16, 19, 20, 30.45, '+00:00')
43
+ expect(actual).to be_time(expected)
44
+ end
45
+
46
+ it 'parses a time with a numeric timezone offset' do
47
+ actual = to_time('1997-07-16T19:20:30.45+01:30')
48
+ expected = Time.new(1997, 7, 16, 19, 20, 30.45, '+01:30')
49
+ expect(actual).to be_time(expected)
50
+ end
51
+
52
+ it 'outputs a date with hours, minutes, and seconds' do
53
+ expected = '1997-07-16T19:20:30Z'
54
+ actual = to_text(Time.utc(1997, 7, 16, 19, 20, 30))
55
+ expect(actual).to eq(expected)
56
+ end
57
+
58
+ it 'truncates fractional seconds' do
59
+ expected = '1997-07-16T19:20:30Z'
60
+ actual = to_text(Time.utc(1997, 7, 16, 19, 20, 30))
61
+ expect(actual).to eq(expected)
62
+ end
63
+
64
+ it 'outputs a time with a numeric timezone offset as UTC' do
65
+ expected = '1997-07-16T17:50:30Z'
66
+ actual = to_text(Time.new(1997, 7, 16, 19, 20, 30, '+01:30'))
67
+ expect(actual).to eq(expected)
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ module XML
4
+ module MappingExtensions
5
+
6
+ class UriNodeSpecElem
7
+ include XML::Mapping
8
+ uri_node :uri, '@uri', default_value: nil
9
+
10
+ def self.from_str(uri_str)
11
+ xml_string = uri_str ? "<elem uri='#{uri_str}'/>" : '<elem/>'
12
+ doc = REXML::Document.new(xml_string)
13
+ load_from_xml(doc.root)
14
+ end
15
+ end
16
+
17
+ describe UriNode do
18
+
19
+ def to_uri(str)
20
+ UriNodeSpecElem.from_str(str).uri
21
+ end
22
+
23
+ def to_text(uri)
24
+ elem = UriNodeSpecElem.new
25
+ elem.uri = uri
26
+ xml = elem.save_to_xml
27
+ xml.attributes['uri']
28
+ end
29
+
30
+ it 'parses a URI' do
31
+ uri_str = 'http://example.org/'
32
+ expect(to_uri(uri_str)).to eq(URI(uri_str))
33
+ end
34
+
35
+ it 'strips whitespace' do
36
+ uri_str = 'http://example.org/'
37
+ expect(to_uri(" #{uri_str} ")).to eq(URI(uri_str))
38
+ end
39
+
40
+ it 'fails on a malformed URI' do
41
+ bad_uri = 'I am not a URI'
42
+ expect { to_uri(" #{bad_uri} ") }.to raise_error(URI::InvalidURIError)
43
+ end
44
+
45
+ it 'transforms a URI to a string' do
46
+ uri_str = 'http://example.org/'
47
+ expect(to_text(URI(uri_str))).to eq(uri_str)
48
+ end
49
+
50
+ it 'doesn\'t set an attribute for a nil value' do
51
+ expect(to_text(nil)).to be_nil
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'uri'
6
+ require 'xml/mapping_extensions/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'xml-mapping_extensions'
10
+ spec.version = XML::MappingExtensions::VERSION
11
+ spec.authors = ['David Moles']
12
+ spec.email = ['david.moles@ucop.edu']
13
+ spec.summary = 'Some extensions for for XML::Mapping'
14
+ spec.description = 'Mapping nodes and other utility classes extending XML::Mapping'
15
+ spec.license = 'MIT'
16
+
17
+ origin_uri = URI(`git config --get remote.origin.url`.chomp)
18
+ spec.homepage = URI::HTTP.build(host: origin_uri.host, path: origin_uri.path.chomp('.git')).to_s
19
+
20
+ spec.files = `git ls-files -z`.split("\x0")
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'mime-types', '~> 2.5'
26
+ spec.add_dependency 'ruby-enum', '~> 0.4'
27
+ spec.add_dependency 'xml-mapping', '~> 0.10'
28
+
29
+ spec.add_development_dependency 'rake', '~> 10.4'
30
+ spec.add_development_dependency 'rspec', '~> 3.2'
31
+ spec.add_development_dependency 'rubocop', '~> 0.32.1'
32
+ spec.add_development_dependency 'simplecov', '~> 0.9.2'
33
+ spec.add_development_dependency 'simplecov-console', '~> 0.2.0'
34
+ spec.add_development_dependency 'yard', '~> 0.8'
35
+ spec.add_development_dependency 'codeclimate-test-reporter'
36
+ end
metadata ADDED
@@ -0,0 +1,219 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xml-mapping_extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Moles
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mime-types
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-enum
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: xml-mapping
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.10'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.32.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.32.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.9.2
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.9.2
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov-console
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.2.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.2.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: yard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.8'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.8'
139
+ - !ruby/object:Gem::Dependency
140
+ name: codeclimate-test-reporter
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Mapping nodes and other utility classes extending XML::Mapping
154
+ email:
155
+ - david.moles@ucop.edu
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rubocop.yml"
162
+ - ".ruby-version"
163
+ - ".yardopts"
164
+ - CHANGES.md
165
+ - Gemfile
166
+ - LICENSE.md
167
+ - README.md
168
+ - Rakefile
169
+ - lib/xml/mapping_extensions.rb
170
+ - lib/xml/mapping_extensions/date_node.rb
171
+ - lib/xml/mapping_extensions/enum_node_base.rb
172
+ - lib/xml/mapping_extensions/mime_type_node.rb
173
+ - lib/xml/mapping_extensions/node_base.rb
174
+ - lib/xml/mapping_extensions/time_node.rb
175
+ - lib/xml/mapping_extensions/uri_node.rb
176
+ - lib/xml/mapping_extensions/version.rb
177
+ - spec/rspec_custom_matchers.rb
178
+ - spec/spec_helper.rb
179
+ - spec/unit/xml/mapping_extensions/date_node_spec.rb
180
+ - spec/unit/xml/mapping_extensions/enum_node_base_spec.rb
181
+ - spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
182
+ - spec/unit/xml/mapping_extensions/node_base_spec.rb
183
+ - spec/unit/xml/mapping_extensions/time_node_spec.rb
184
+ - spec/unit/xml/mapping_extensions/uri_node_spec.rb
185
+ - xml-mapping_extensions.gemspec
186
+ homepage: http://github.com/dmolesUC3/xml-mapping_extensions
187
+ licenses:
188
+ - MIT
189
+ metadata: {}
190
+ post_install_message:
191
+ rdoc_options: []
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ required_rubygems_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ requirements: []
205
+ rubyforge_project:
206
+ rubygems_version: 2.4.6
207
+ signing_key:
208
+ specification_version: 4
209
+ summary: Some extensions for for XML::Mapping
210
+ test_files:
211
+ - spec/rspec_custom_matchers.rb
212
+ - spec/spec_helper.rb
213
+ - spec/unit/xml/mapping_extensions/date_node_spec.rb
214
+ - spec/unit/xml/mapping_extensions/enum_node_base_spec.rb
215
+ - spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
216
+ - spec/unit/xml/mapping_extensions/node_base_spec.rb
217
+ - spec/unit/xml/mapping_extensions/time_node_spec.rb
218
+ - spec/unit/xml/mapping_extensions/uri_node_spec.rb
219
+ has_rdoc: