nokogiri-happymapper 0.8.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +1 -4
  4. data/lib/happymapper/anonymous_mapper.rb +6 -5
  5. data/lib/happymapper/class_methods.rb +466 -0
  6. data/lib/happymapper/element.rb +3 -3
  7. data/lib/happymapper/item.rb +19 -13
  8. data/lib/happymapper/supported_types.rb +4 -6
  9. data/lib/happymapper/syntax_error.rb +6 -0
  10. data/lib/happymapper/version.rb +1 -1
  11. data/lib/happymapper.rb +54 -505
  12. data/lib/nokogiri-happymapper.rb +1 -1
  13. metadata +100 -107
  14. data/spec/features/after_parse_callbacks_spec.rb +0 -32
  15. data/spec/features/attribute_default_value_spec.rb +0 -48
  16. data/spec/features/attributes_spec.rb +0 -35
  17. data/spec/features/has_many_empty_array_spec.rb +0 -44
  18. data/spec/features/ignay_spec.rb +0 -92
  19. data/spec/features/inheritance_spec.rb +0 -121
  20. data/spec/features/mixed_namespaces_spec.rb +0 -60
  21. data/spec/features/parse_with_object_to_update_spec.rb +0 -116
  22. data/spec/features/same_tag_different_meaning_spec.rb +0 -44
  23. data/spec/features/to_xml_spec.rb +0 -205
  24. data/spec/features/to_xml_with_namespaces_spec.rb +0 -237
  25. data/spec/features/wildcard_tag_name_spec.rb +0 -110
  26. data/spec/features/wrap_spec.rb +0 -87
  27. data/spec/features/xpath_spec.rb +0 -84
  28. data/spec/fixtures/address.xml +0 -9
  29. data/spec/fixtures/ambigous_items.xml +0 -22
  30. data/spec/fixtures/analytics.xml +0 -61
  31. data/spec/fixtures/analytics_profile.xml +0 -127
  32. data/spec/fixtures/atom.xml +0 -19
  33. data/spec/fixtures/commit.xml +0 -52
  34. data/spec/fixtures/current_weather.xml +0 -89
  35. data/spec/fixtures/current_weather_missing_elements.xml +0 -18
  36. data/spec/fixtures/default_namespace_combi.xml +0 -6
  37. data/spec/fixtures/dictionary.xml +0 -20
  38. data/spec/fixtures/family_tree.xml +0 -21
  39. data/spec/fixtures/inagy.xml +0 -85
  40. data/spec/fixtures/lastfm.xml +0 -355
  41. data/spec/fixtures/multiple_namespaces.xml +0 -170
  42. data/spec/fixtures/multiple_primitives.xml +0 -5
  43. data/spec/fixtures/optional_attributes.xml +0 -6
  44. data/spec/fixtures/pita.xml +0 -133
  45. data/spec/fixtures/posts.xml +0 -23
  46. data/spec/fixtures/product_default_namespace.xml +0 -18
  47. data/spec/fixtures/product_no_namespace.xml +0 -10
  48. data/spec/fixtures/product_single_namespace.xml +0 -10
  49. data/spec/fixtures/quarters.xml +0 -19
  50. data/spec/fixtures/radar.xml +0 -21
  51. data/spec/fixtures/set_config_options.xml +0 -3
  52. data/spec/fixtures/statuses.xml +0 -422
  53. data/spec/fixtures/subclass_namespace.xml +0 -50
  54. data/spec/fixtures/unformatted_address.xml +0 -1
  55. data/spec/fixtures/wrapper.xml +0 -11
  56. data/spec/happymapper/anonymous_mapper_spec.rb +0 -158
  57. data/spec/happymapper/attribute_spec.rb +0 -12
  58. data/spec/happymapper/item_spec.rb +0 -177
  59. data/spec/happymapper_spec.rb +0 -1208
  60. data/spec/spec_helper.rb +0 -25
@@ -1,177 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- module Foo
6
- class Bar; end
7
- end
8
-
9
- describe HappyMapper::Item do
10
- describe 'new instance' do
11
- before do
12
- @item = described_class.new(:foo, String, tag: 'foobar')
13
- end
14
-
15
- it 'accepts a name' do
16
- expect(@item.name).to eq('foo')
17
- end
18
-
19
- it 'accepts a type' do
20
- expect(@item.type).to eq(String)
21
- end
22
-
23
- it 'accepts :tag as an option' do
24
- expect(@item.tag).to eq('foobar')
25
- end
26
-
27
- it 'has a method_name' do
28
- expect(@item.method_name).to eq('foo')
29
- end
30
- end
31
-
32
- describe '#constant' do
33
- it 'justs use type if constant' do
34
- item = described_class.new(:foo, String)
35
- expect(item.constant).to eq(String)
36
- end
37
-
38
- it 'converts string type to constant' do
39
- item = described_class.new(:foo, 'String')
40
- expect(item.constant).to eq(String)
41
- end
42
-
43
- it 'converts string with :: to constant' do
44
- item = described_class.new(:foo, 'Foo::Bar')
45
- expect(item.constant).to eq(Foo::Bar)
46
- end
47
- end
48
-
49
- describe '#method_name' do
50
- it 'converts dashes to underscores' do
51
- item = described_class.new(:'foo-bar', String, tag: 'foobar')
52
- expect(item.method_name).to eq('foo_bar')
53
- end
54
- end
55
-
56
- describe '#xpath' do
57
- it 'defaults to tag' do
58
- item = described_class.new(:foo, String, tag: 'foobar')
59
- expect(item.xpath).to eq('foobar')
60
- end
61
-
62
- it 'prepends with .// if options[:deep] true' do
63
- item = described_class.new(:foo, String, tag: 'foobar', deep: true)
64
- expect(item.xpath).to eq('.//foobar')
65
- end
66
-
67
- it 'prepends namespace if namespace exists' do
68
- item = described_class.new(:foo, String, tag: 'foobar')
69
- item.namespace = 'v2'
70
- expect(item.xpath).to eq('v2:foobar')
71
- end
72
- end
73
-
74
- describe 'typecasting' do
75
- it 'works with Strings' do
76
- item = described_class.new(:foo, String)
77
- [21, '21'].each do |a|
78
- expect(item.typecast(a)).to eq('21')
79
- end
80
- end
81
-
82
- context 'with Integers' do
83
- let(:item) { described_class.new(:foo, Integer) }
84
-
85
- it 'works with an integer' do
86
- expect(item.typecast(21)).to eq 21
87
- end
88
-
89
- it 'works with a float' do
90
- expect(item.typecast(21.0)).to eq 21
91
- end
92
-
93
- it 'works with a string' do
94
- expect(item.typecast('21')).to eq 21
95
- end
96
-
97
- it 'works with a string with trailing characters' do
98
- expect(item.typecast('21foo')).to eq 21
99
- end
100
-
101
- it 'works with a string with zero plus trailing characters' do
102
- expect(item.typecast('0foo')).to eq 0
103
- end
104
-
105
- it 'handles a non-numeric string' do
106
- expect(item.typecast('foo')).to be_nil
107
- end
108
-
109
- it 'handles an empty string' do
110
- expect(item.typecast('')).to be_nil
111
- end
112
-
113
- it 'handles nil' do
114
- expect(item.typecast(nil)).to be_nil
115
- end
116
- end
117
-
118
- it 'works with Floats' do
119
- item = described_class.new(:foo, Float)
120
- [21, 21.0, '21'].each do |a|
121
- expect(item.typecast(a)).to eq 21.0
122
- end
123
- end
124
-
125
- it 'works with Times' do
126
- item = described_class.new(:foo, Time)
127
- expect(item.typecast('2000-01-01 01:01:01.123456')).to eq(Time.local(2000, 1, 1, 1, 1, 1, 123_456))
128
- end
129
-
130
- context 'with Date' do
131
- let(:item) { described_class.new(:foo, Date) }
132
-
133
- it 'works with a string' do
134
- expect(item.typecast('2000-01-01')).to eq(Date.new(2000, 1, 1))
135
- end
136
-
137
- it 'handles nil' do
138
- expect(item.typecast(nil)).to be_nil
139
- end
140
-
141
- it 'handles empty string' do
142
- expect(item.typecast('')).to eq(nil)
143
- end
144
- end
145
-
146
- context 'with DateTime' do
147
- let(:item) { described_class.new(:foo, DateTime) }
148
-
149
- it 'works with a string' do
150
- result = item.typecast('2000-01-01 13:42:37')
151
- expect(result.to_time).to eq Time.new(2000, 1, 1, 13, 42, 37, '+00:00')
152
- end
153
-
154
- it 'works with a historical date in a string' do
155
- result = item.typecast('1616-04-23')
156
-
157
- aggregate_failures do
158
- expect(result.to_time).to eq Time.new(1616, 4, 23, 0, 0, 0, '+00:00')
159
- expect(result).to be_gregorian
160
- end
161
- end
162
-
163
- it 'handles nil' do
164
- expect(item.typecast(nil)).to eq(nil)
165
- end
166
-
167
- it 'handles empty strings' do
168
- expect(item.typecast('')).to eq(nil)
169
- end
170
- end
171
-
172
- it 'works with Boolean' do
173
- item = described_class.new(:foo, HappyMapper::Boolean)
174
- expect(item.typecast('false')).to eq(false)
175
- end
176
- end
177
- end