odata4 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9adfc90966866d69ec4082404f10a60cd8404563
4
- data.tar.gz: 293c604e274b209c738496e41318f2441d41e159
3
+ metadata.gz: a9465568c8c8b575d04c9367e8c51b3c6161a8b9
4
+ data.tar.gz: 1da130b782964c9ccf410e74a6309c78bd13e050
5
5
  SHA512:
6
- metadata.gz: 52bad8a865f20e227aa331f09e680d857caaaa3da8e70df5be873d78477d96b7e4451506e1a70efec9239034c4e98693c0f7b5600f78e5f383fcb56249e6085c
7
- data.tar.gz: c1868354a432bed72b9b6db7b0e76d6194fd221e05827d3077cdaf9d258687e46bd489a444132154eb95a4835f959ca18e7f64c22dbaf49a41a1299f6d3be382
6
+ metadata.gz: c2949929b4b43e3fde0b26d845f630d59dfc7d5936717dca0531c11bba7514487c72e889a372863f8be2c2934ff85dee1cd8df9dc67193d145ca0245f42133cf
7
+ data.tar.gz: a1de80f33ab0719567cabbc948dc645a31363697ee705ba0654cabb31fbaffeb130cb751ed061b55dccf169f977e62689b96d3b6180a4052d1fdd31a10b7b5ca
@@ -1 +1 @@
1
- 2.1
1
+ 2.2
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.8.2
4
+
5
+ * [Refactor] Moved `ComplexType` and `EnumType` class into `Schema`, respective property types into `Properties` namespace
6
+
3
7
  ## 0.8.1
4
8
 
5
9
  * [New Feature] Basic support for `Collection` property type
@@ -16,8 +16,6 @@ require 'odata4/property_registry'
16
16
  require 'odata4/property'
17
17
  require 'odata4/properties'
18
18
  require 'odata4/navigation_property'
19
- require 'odata4/complex_type'
20
- require 'odata4/enum_type'
21
19
  require 'odata4/entity'
22
20
  require 'odata4/entity_container'
23
21
  require 'odata4/entity_set'
@@ -56,7 +56,7 @@ module OData4
56
56
  # @param property_name [to_s]
57
57
  # @return [*]
58
58
  def [](property_name)
59
- if get_property(property_name).is_a?(::OData4::ComplexType::Property)
59
+ if get_property(property_name).is_a?(::OData4::Properties::Complex)
60
60
  get_property(property_name)
61
61
  else
62
62
  get_property(property_name).value
@@ -5,10 +5,12 @@ require 'odata4/properties/number'
5
5
  require 'odata4/properties/binary'
6
6
  require 'odata4/properties/boolean'
7
7
  require 'odata4/properties/collection'
8
+ require 'odata4/properties/complex'
8
9
  require 'odata4/properties/date'
9
10
  require 'odata4/properties/date_time'
10
11
  require 'odata4/properties/date_time_offset'
11
12
  require 'odata4/properties/decimal'
13
+ require 'odata4/properties/enum'
12
14
  require 'odata4/properties/float'
13
15
  require 'odata4/properties/geography'
14
16
  require 'odata4/properties/guid'
@@ -20,7 +22,11 @@ require 'odata4/properties/time_of_day'
20
22
  OData4::Properties.constants.each do |property_name|
21
23
  klass = OData4::Properties.const_get(property_name)
22
24
  if klass.is_a?(Class)
23
- property = klass.new('test', nil)
24
- OData4::PropertyRegistry.add(property.type, property.class)
25
+ begin
26
+ property = klass.new('test', nil)
27
+ OData4::PropertyRegistry.add(property.type, property.class)
28
+ rescue NotImplementedError
29
+ # Abstract type classes cannot be instantiated, ignore
30
+ end
25
31
  end
26
32
  end
@@ -1,8 +1,8 @@
1
1
  module OData4
2
- class ComplexType
2
+ module Properties
3
3
  # Abstract base class for OData4 ComplexTypes
4
- # @see [OData4::ComplexType]
5
- class Property < OData4::Property
4
+ # @see [OData4::Schema::ComplexType]
5
+ class Complex < OData4::Property
6
6
  def initialize(name, value, options = {})
7
7
  super(name, value, options)
8
8
  init_properties
@@ -1,8 +1,8 @@
1
1
  module OData4
2
- class EnumType
2
+ module Properties
3
3
  # Abstract base class for OData4 EnumTypes
4
- # @see [OData4::EnumType]
5
- class Property < OData4::Property
4
+ # @see [OData4::Schema::EnumType]
5
+ class Enum < OData4::Property
6
6
  # Returns the property value, properly typecast
7
7
  # @return [String, nil]
8
8
  def value
@@ -1,3 +1,6 @@
1
+ require 'odata4/schema/complex_type'
2
+ require 'odata4/schema/enum_type'
3
+
1
4
  module OData4
2
5
  class Schema
3
6
  # The schema's parent service
@@ -37,23 +40,23 @@ module OData4
37
40
  end
38
41
 
39
42
  # Returns a list of `ComplexType`s defined by the schema.
40
- # @return [Hash<String, OData4::ComplexType>]
43
+ # @return [Hash<String, OData4::Schema::ComplexType>]
41
44
  def complex_types
42
45
  @complex_types ||= metadata.xpath('//ComplexType').map do |entity|
43
46
  [
44
47
  entity.attributes['Name'].value,
45
- ::OData4::ComplexType.new(entity, self)
48
+ ComplexType.new(entity, self)
46
49
  ]
47
50
  end.to_h
48
51
  end
49
52
 
50
53
  # Returns a list of EnumTypes defined by the schema.
51
- # @return [Hash<String, OData4::EnumType>]
54
+ # @return [Hash<String, OData4::Schema::EnumType>]
52
55
  def enum_types
53
56
  @enum_types ||= metadata.xpath('//EnumType').map do |entity|
54
57
  [
55
58
  entity.attributes['Name'].value,
56
- ::OData4::EnumType.new(entity, self)
59
+ EnumType.new(entity, self)
57
60
  ]
58
61
  end.to_h
59
62
  end
@@ -0,0 +1,79 @@
1
+ module OData4
2
+ class Schema
3
+ # ComplexTypes are used in OData4 to either encapsulate richer data types for
4
+ # use as Entity properties. ComplexTypes are composed of properties the same
5
+ # way that Entities are and, so, the interface for working with the various
6
+ # properties of a ComplexType mimics that of Entities.
7
+ class ComplexType
8
+ # Creates a new ComplexType based on the supplied options.
9
+ # @param type_xml [Nokogiri::XML::Element]
10
+ # @param service [OData4::Service]
11
+ def initialize(type_definition, schema)
12
+ @type_definition = type_definition
13
+ @schema = schema
14
+ end
15
+
16
+ # The name of the ComplexType
17
+ # @return [String]
18
+ def name
19
+ @name ||= type_definition.attributes['Name'].value
20
+ end
21
+
22
+ # Returns the namespaced type for the ComplexType.
23
+ # @return [String]
24
+ def type
25
+ "#{namespace}.#{name}"
26
+ end
27
+
28
+ # Returns the namespace this ComplexType belongs to.
29
+ # @return [String]
30
+ def namespace
31
+ @namespace ||= service.namespace
32
+ end
33
+
34
+ # Returns this ComplexType's properties.
35
+ # @return [Hash<String, OData4::Property>]
36
+ def properties
37
+ @properties ||= collect_properties
38
+ end
39
+
40
+ # Returns a list of this ComplexType's property names.
41
+ # @return [Array<String>]
42
+ def property_names
43
+ @property_names ||= properties.keys
44
+ end
45
+
46
+ # Returns the property class that implements this `ComplexType`.
47
+ # @return [Class < OData4::Properties::Complex]
48
+ def property_class
49
+ @property_class ||= lambda { |type, complex_type|
50
+ klass = Class.new ::OData4::Properties::Complex
51
+ klass.send(:define_method, :type) { type }
52
+ klass.send(:define_method, :complex_type) { complex_type }
53
+ klass
54
+ }.call(type, self)
55
+ end
56
+
57
+ private
58
+
59
+ def schema
60
+ @schema
61
+ end
62
+
63
+ def service
64
+ @schema.service
65
+ end
66
+
67
+ def type_definition
68
+ @type_definition
69
+ end
70
+
71
+ def collect_properties
72
+ Hash[type_definition.xpath('./Property').map do |property_xml|
73
+ property_name, property = schema.send(:process_property_from_xml, property_xml)
74
+ [property_name, property]
75
+ end]
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,95 @@
1
+ module OData4
2
+ class Schema
3
+ # Enumeration types are nominal types that represent a series of related values.
4
+ # Enumeration types expose these related values as members of the enumeration.
5
+ class EnumType
6
+ # Creates a new EnumType based on the supplied options.
7
+ # @param type_xml [Nokogiri::XML::Element]
8
+ # @param service [OData4::Service]
9
+ # @return [self]
10
+ def initialize(type_definition, schema)
11
+ @type_definition = type_definition
12
+ @schema = schema
13
+ end
14
+
15
+ # The name of the EnumType
16
+ # @return [String]
17
+ def name
18
+ options['Name']
19
+ end
20
+
21
+ # Returns the namespaced type for the EnumType.
22
+ # @return [String]
23
+ def type
24
+ "#{namespace}.#{name}"
25
+ end
26
+
27
+ # Whether this EnumType supports setting multiple values.
28
+ # @return [Boolean]
29
+ def is_flags?
30
+ options['IsFlags'] == 'true'
31
+ end
32
+
33
+ # The underlying type of this EnumType.
34
+ # @return [String]
35
+ def underlying_type
36
+ options['UnderlyingType'] || 'Edm.Int32'
37
+ end
38
+
39
+ # Returns the namespace this EnumType belongs to.
40
+ # @return [String]
41
+ def namespace
42
+ @namespace ||= service.namespace
43
+ end
44
+
45
+ # Returns the members of this EnumType and their values.
46
+ # @return [Hash]
47
+ def members
48
+ @members ||= collect_members
49
+ end
50
+
51
+ # Returns the property class that implements this `EnumType`.
52
+ # @return [Class < OData4::Properties::Enum]
53
+ def property_class
54
+ @property_class ||= lambda { |type, members, is_flags|
55
+ klass = Class.new ::OData4::Properties::Enum
56
+ klass.send(:define_method, :type) { type }
57
+ klass.send(:define_method, :members) { members }
58
+ klass.send(:define_method, :is_flags?) { is_flags }
59
+ klass
60
+ }.call(type, members, is_flags?)
61
+ end
62
+
63
+ # Returns the value of the requested member.
64
+ # @param member_name [to_s]
65
+ # @return [*]
66
+ def [](member_name)
67
+ members.invert[member_name.to_s]
68
+ end
69
+
70
+ private
71
+
72
+ def service
73
+ @schema.service
74
+ end
75
+
76
+ def type_definition
77
+ @type_definition
78
+ end
79
+
80
+ def options
81
+ @options = type_definition.attributes.map do |name, attr|
82
+ [name, attr.value]
83
+ end.to_h
84
+ end
85
+
86
+ def collect_members
87
+ Hash[type_definition.xpath('./Member').map.with_index do |member_xml, index|
88
+ member_name = member_xml.attributes['Name'].value
89
+ member_value = member_xml.attributes['Value'].andand.value.andand.to_i
90
+ [member_value || index, member_name]
91
+ end]
92
+ end
93
+ end
94
+ end
95
+ end
@@ -111,7 +111,7 @@ module OData4
111
111
  end
112
112
 
113
113
  # Returns a list of `ComplexType`s used by the service.
114
- # @return [Hash<String, OData4::ComplexType>]
114
+ # @return [Hash<String, OData4::Schema::ComplexType>]
115
115
  def complex_types
116
116
  @complex_types ||= schemas.map do |namespace, schema|
117
117
  schema.complex_types.map do |name, complex_type|
@@ -121,7 +121,7 @@ module OData4
121
121
  end
122
122
 
123
123
  # Returns a list of `EnumType`s used by the service
124
- # @return [Hash<String, OData4::EnumType>]
124
+ # @return [Hash<String, OData4::Schema::EnumType>]
125
125
  def enum_types
126
126
  @enum_types ||= schemas.map do |namespace, schema|
127
127
  schema.enum_types.map do |name, enum_type|
@@ -1,3 +1,3 @@
1
1
  module OData4
2
- VERSION = '0.8.1'
2
+ VERSION = '0.8.2'
3
3
  end
@@ -65,7 +65,7 @@ shared_examples 'a valid supplier' do
65
65
  # Check property types
66
66
  it { expect(subject.get_property('ID')).to be_a(OData4::Properties::Integer) }
67
67
  it { expect(subject.get_property('Name')).to be_a(OData4::Properties::String) }
68
- it { expect(subject.get_property('Address')).to be_a(OData4::ComplexType::Property) }
68
+ it { expect(subject.get_property('Address')).to be_a(OData4::Properties::Complex) }
69
69
  it { expect(subject.get_property('Location')).to be_a(OData4::Properties::Geography::Point) }
70
70
  # it { expect(subject.get_property('Products')).to be_a(OData4::NavigationProperty::Proxy)}
71
71
 
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe OData4::ComplexType, vcr: {cassette_name: 'complex_type_specs'} do
3
+ describe OData4::Schema::ComplexType, vcr: {cassette_name: 'schema/complex_type_specs'} do
4
4
  before(:example) do
5
5
  OData4::Service.open('http://services.odata.org/V4/OData/OData.svc', name: 'ODataDemo')
6
6
  end
@@ -27,7 +27,7 @@ describe OData4::ComplexType, vcr: {cassette_name: 'complex_type_specs'} do
27
27
 
28
28
  # Check property instance inheritance hierarchy
29
29
  it { expect(subject).to be_a(OData4::Property) }
30
- it { expect(subject).to be_a(OData4::ComplexType::Property) }
30
+ it { expect(subject).to be_a(OData4::Properties::Complex) }
31
31
 
32
32
  it { expect(subject).to respond_to(:name) }
33
33
  it { expect(subject).to respond_to(:type) }
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe OData4::EnumType, vcr: {cassette_name: 'enum_type_specs'} do
3
+ describe OData4::Schema::EnumType, vcr: {cassette_name: 'schema/enum_type_specs'} do
4
4
  before(:example) do
5
5
  OData4::Service.open('http://services.odata.org/V4/OData/OData.svc', name: 'ODataDemo', metadata_file: metadata_file)
6
6
  end
@@ -22,7 +22,7 @@ describe OData4::EnumType, vcr: {cassette_name: 'enum_type_specs'} do
22
22
 
23
23
  # Check property instance inheritance hierarchy
24
24
  it { expect(subject).to be_a(OData4::Property) }
25
- it { expect(subject).to be_a(OData4::EnumType::Property) }
25
+ it { expect(subject).to be_a(OData4::Properties::Enum) }
26
26
 
27
27
  it { expect(subject).to respond_to(:name) }
28
28
  it { expect(subject).to respond_to(:type) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: odata4
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christoph Wagner
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-02-23 00:00:00.000000000 Z
12
+ date: 2018-04-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -200,23 +200,21 @@ files:
200
200
  - Rakefile
201
201
  - TODO.md
202
202
  - lib/odata4.rb
203
- - lib/odata4/complex_type.rb
204
- - lib/odata4/complex_type/property.rb
205
203
  - lib/odata4/entity.rb
206
204
  - lib/odata4/entity_container.rb
207
205
  - lib/odata4/entity_set.rb
208
- - lib/odata4/enum_type.rb
209
- - lib/odata4/enum_type/property.rb
210
206
  - lib/odata4/navigation_property.rb
211
207
  - lib/odata4/navigation_property/proxy.rb
212
208
  - lib/odata4/properties.rb
213
209
  - lib/odata4/properties/binary.rb
214
210
  - lib/odata4/properties/boolean.rb
215
211
  - lib/odata4/properties/collection.rb
212
+ - lib/odata4/properties/complex.rb
216
213
  - lib/odata4/properties/date.rb
217
214
  - lib/odata4/properties/date_time.rb
218
215
  - lib/odata4/properties/date_time_offset.rb
219
216
  - lib/odata4/properties/decimal.rb
217
+ - lib/odata4/properties/enum.rb
220
218
  - lib/odata4/properties/float.rb
221
219
  - lib/odata4/properties/geography.rb
222
220
  - lib/odata4/properties/geography/base.rb
@@ -241,6 +239,8 @@ files:
241
239
  - lib/odata4/query/in_batches.rb
242
240
  - lib/odata4/railtie.rb
243
241
  - lib/odata4/schema.rb
242
+ - lib/odata4/schema/complex_type.rb
243
+ - lib/odata4/schema/enum_type.rb
244
244
  - lib/odata4/service.rb
245
245
  - lib/odata4/service/request.rb
246
246
  - lib/odata4/service/response.rb
@@ -260,7 +260,6 @@ files:
260
260
  - spec/fixtures/files/products.xml
261
261
  - spec/fixtures/files/supplier_0.json
262
262
  - spec/fixtures/files/supplier_0.xml
263
- - spec/fixtures/vcr_cassettes/complex_type_specs.yml
264
263
  - spec/fixtures/vcr_cassettes/entity_set_specs.yml
265
264
  - spec/fixtures/vcr_cassettes/entity_set_specs/bad_entry.yml
266
265
  - spec/fixtures/vcr_cassettes/entity_set_specs/existing_entry.yml
@@ -269,16 +268,15 @@ files:
269
268
  - spec/fixtures/vcr_cassettes/navigation_property_proxy_specs.yml
270
269
  - spec/fixtures/vcr_cassettes/query/result_specs.yml
271
270
  - spec/fixtures/vcr_cassettes/query_specs.yml
271
+ - spec/fixtures/vcr_cassettes/schema/complex_type_specs.yml
272
272
  - spec/fixtures/vcr_cassettes/service/request_specs.yml
273
273
  - spec/fixtures/vcr_cassettes/service_registry_specs.yml
274
274
  - spec/fixtures/vcr_cassettes/service_specs.yml
275
275
  - spec/fixtures/vcr_cassettes/usage_example_specs.yml
276
- - spec/odata4/complex_type_spec.rb
277
276
  - spec/odata4/entity/shared_examples.rb
278
277
  - spec/odata4/entity_container_spec.rb
279
278
  - spec/odata4/entity_set_spec.rb
280
279
  - spec/odata4/entity_spec.rb
281
- - spec/odata4/enum_type_spec.rb
282
280
  - spec/odata4/navigation_property/proxy_spec.rb
283
281
  - spec/odata4/navigation_property_spec.rb
284
282
  - spec/odata4/properties/binary_spec.rb
@@ -302,6 +300,8 @@ files:
302
300
  - spec/odata4/property_spec.rb
303
301
  - spec/odata4/query/criteria_spec.rb
304
302
  - spec/odata4/query_spec.rb
303
+ - spec/odata4/schema/complex_type_spec.rb
304
+ - spec/odata4/schema/enum_type_spec.rb
305
305
  - spec/odata4/schema_spec.rb
306
306
  - spec/odata4/service/request_spec.rb
307
307
  - spec/odata4/service/response_spec.rb
@@ -345,7 +345,6 @@ test_files:
345
345
  - spec/fixtures/files/products.xml
346
346
  - spec/fixtures/files/supplier_0.json
347
347
  - spec/fixtures/files/supplier_0.xml
348
- - spec/fixtures/vcr_cassettes/complex_type_specs.yml
349
348
  - spec/fixtures/vcr_cassettes/entity_set_specs.yml
350
349
  - spec/fixtures/vcr_cassettes/entity_set_specs/bad_entry.yml
351
350
  - spec/fixtures/vcr_cassettes/entity_set_specs/existing_entry.yml
@@ -354,16 +353,15 @@ test_files:
354
353
  - spec/fixtures/vcr_cassettes/navigation_property_proxy_specs.yml
355
354
  - spec/fixtures/vcr_cassettes/query/result_specs.yml
356
355
  - spec/fixtures/vcr_cassettes/query_specs.yml
356
+ - spec/fixtures/vcr_cassettes/schema/complex_type_specs.yml
357
357
  - spec/fixtures/vcr_cassettes/service/request_specs.yml
358
358
  - spec/fixtures/vcr_cassettes/service_registry_specs.yml
359
359
  - spec/fixtures/vcr_cassettes/service_specs.yml
360
360
  - spec/fixtures/vcr_cassettes/usage_example_specs.yml
361
- - spec/odata4/complex_type_spec.rb
362
361
  - spec/odata4/entity/shared_examples.rb
363
362
  - spec/odata4/entity_container_spec.rb
364
363
  - spec/odata4/entity_set_spec.rb
365
364
  - spec/odata4/entity_spec.rb
366
- - spec/odata4/enum_type_spec.rb
367
365
  - spec/odata4/navigation_property/proxy_spec.rb
368
366
  - spec/odata4/navigation_property_spec.rb
369
367
  - spec/odata4/properties/binary_spec.rb
@@ -387,6 +385,8 @@ test_files:
387
385
  - spec/odata4/property_spec.rb
388
386
  - spec/odata4/query/criteria_spec.rb
389
387
  - spec/odata4/query_spec.rb
388
+ - spec/odata4/schema/complex_type_spec.rb
389
+ - spec/odata4/schema/enum_type_spec.rb
390
390
  - spec/odata4/schema_spec.rb
391
391
  - spec/odata4/service/request_spec.rb
392
392
  - spec/odata4/service/response_spec.rb
@@ -1,79 +0,0 @@
1
- require 'odata4/complex_type/property'
2
-
3
- module OData4
4
- # ComplexTypes are used in OData4 to either encapsulate richer data types for
5
- # use as Entity properties. ComplexTypes are composed of properties the same
6
- # way that Entities are and, so, the interface for working with the various
7
- # properties of a ComplexType mimics that of Entities.
8
- class ComplexType
9
- # Creates a new ComplexType based on the supplied options.
10
- # @param type_xml [Nokogiri::XML::Element]
11
- # @param service [OData4::Service]
12
- def initialize(type_definition, schema)
13
- @type_definition = type_definition
14
- @schema = schema
15
- end
16
-
17
- # The name of the ComplexType
18
- # @return [String]
19
- def name
20
- @name ||= type_definition.attributes['Name'].value
21
- end
22
-
23
- # Returns the namespaced type for the ComplexType.
24
- # @return [String]
25
- def type
26
- "#{namespace}.#{name}"
27
- end
28
-
29
- # Returns the namespace this ComplexType belongs to.
30
- # @return [String]
31
- def namespace
32
- @namespace ||= service.namespace
33
- end
34
-
35
- # Returns this ComplexType's properties.
36
- # @return [Hash<String, OData4::Property>]
37
- def properties
38
- @properties ||= collect_properties
39
- end
40
-
41
- # Returns a list of this ComplexType's property names.
42
- # @return [Array<String>]
43
- def property_names
44
- @property_names ||= properties.keys
45
- end
46
-
47
- # Returns the property class that implements this `ComplexType`.
48
- # @return [Class < OData4::ComplexType::Property]
49
- def property_class
50
- @property_class ||= lambda { |type, complex_type|
51
- klass = Class.new ::OData4::ComplexType::Property
52
- klass.send(:define_method, :type) { type }
53
- klass.send(:define_method, :complex_type) { complex_type }
54
- klass
55
- }.call(type, self)
56
- end
57
-
58
- private
59
-
60
- def schema
61
- @schema
62
- end
63
-
64
- def service
65
- @schema.service
66
- end
67
-
68
- def type_definition
69
- @type_definition
70
- end
71
-
72
- def collect_properties
73
- Hash[type_definition.xpath('./Property').map do |property_xml|
74
- property_name, property = schema.send(:process_property_from_xml, property_xml)
75
- [property_name, property]
76
- end]
77
- end
78
- end
79
- end
@@ -1,95 +0,0 @@
1
- require 'odata4/enum_type/property'
2
-
3
- module OData4
4
- # Enumeration types are nominal types that represent a series of related values.
5
- # Enumeration types expose these related values as members of the enumeration.
6
- class EnumType
7
- # Creates a new EnumType based on the supplied options.
8
- # @param type_xml [Nokogiri::XML::Element]
9
- # @param service [OData4::Service]
10
- # @return [self]
11
- def initialize(type_definition, schema)
12
- @type_definition = type_definition
13
- @schema = schema
14
- end
15
-
16
- # The name of the EnumType
17
- # @return [String]
18
- def name
19
- options['Name']
20
- end
21
-
22
- # Returns the namespaced type for the EnumType.
23
- # @return [String]
24
- def type
25
- "#{namespace}.#{name}"
26
- end
27
-
28
- # Whether this EnumType supports setting multiple values.
29
- # @return [Boolean]
30
- def is_flags?
31
- options['IsFlags'] == 'true'
32
- end
33
-
34
- # The underlying type of this EnumType.
35
- # @return [String]
36
- def underlying_type
37
- options['UnderlyingType'] || 'Edm.Int32'
38
- end
39
-
40
- # Returns the namespace this EnumType belongs to.
41
- # @return [String]
42
- def namespace
43
- @namespace ||= service.namespace
44
- end
45
-
46
- # Returns the members of this EnumType and their values.
47
- # @return [Hash]
48
- def members
49
- @members ||= collect_members
50
- end
51
-
52
- # Returns the property class that implements this `EnumType`.
53
- # @return [Class < OData4::EnumType::Property]
54
- def property_class
55
- @property_class ||= lambda { |type, members, is_flags|
56
- klass = Class.new ::OData4::EnumType::Property
57
- klass.send(:define_method, :type) { type }
58
- klass.send(:define_method, :members) { members }
59
- klass.send(:define_method, :is_flags?) { is_flags }
60
- klass
61
- }.call(type, members, is_flags?)
62
- end
63
-
64
- # Returns the value of the requested member.
65
- # @param member_name [to_s]
66
- # @return [*]
67
- def [](member_name)
68
- members.invert[member_name.to_s]
69
- end
70
-
71
- private
72
-
73
- def service
74
- @schema.service
75
- end
76
-
77
- def type_definition
78
- @type_definition
79
- end
80
-
81
- def options
82
- @options = type_definition.attributes.map do |name, attr|
83
- [name, attr.value]
84
- end.to_h
85
- end
86
-
87
- def collect_members
88
- Hash[type_definition.xpath('./Member').map.with_index do |member_xml, index|
89
- member_name = member_xml.attributes['Name'].value
90
- member_value = member_xml.attributes['Value'].andand.value.andand.to_i
91
- [member_value || index, member_name]
92
- end]
93
- end
94
- end
95
- end