class OData4::Schema::EnumType
Enumeration types are nominal types that represent a series of related values. Enumeration types expose these related values as members of the enumeration.
Public Class Methods
Creates a new EnumType
based on the supplied options. @param type_xml [Nokogiri::XML::Element] @param service [OData4::Service] @return [self]
# File lib/odata4/schema/enum_type.rb, line 10 def initialize(type_definition, schema) @type_definition = type_definition @schema = schema end
Public Instance Methods
Returns the value of the requested member. @param member_name [to_s] @return [*]
# File lib/odata4/schema/enum_type.rb, line 66 def [](member_name) members.invert[member_name.to_s] end
Whether this EnumType
supports setting multiple values. @return [Boolean]
# File lib/odata4/schema/enum_type.rb, line 29 def is_flags? options['IsFlags'] == 'true' end
Returns the members of this EnumType
and their values. @return [Hash]
# File lib/odata4/schema/enum_type.rb, line 47 def members @members ||= collect_members end
The name of the EnumType
@return [String]
# File lib/odata4/schema/enum_type.rb, line 17 def name options['Name'] end
Returns the namespace this EnumType
belongs to. @return [String]
# File lib/odata4/schema/enum_type.rb, line 41 def namespace @namespace ||= service.namespace end
Returns the property class that implements this `EnumType`. @return [Class < OData4::Properties::Enum]
# File lib/odata4/schema/enum_type.rb, line 53 def property_class @property_class ||= lambda { |type, members, is_flags| klass = Class.new ::OData4::Properties::Enum klass.send(:define_method, :type) { type } klass.send(:define_method, :members) { members } klass.send(:define_method, :is_flags?) { is_flags } klass }.call(type, members, is_flags?) end
Returns the namespaced type for the EnumType
. @return [String]
# File lib/odata4/schema/enum_type.rb, line 23 def type "#{namespace}.#{name}" end
The underlying type of this EnumType
. @return [String]
# File lib/odata4/schema/enum_type.rb, line 35 def underlying_type options['UnderlyingType'] || 'Edm.Int32' end
Private Instance Methods
# File lib/odata4/schema/enum_type.rb, line 86 def collect_members Hash[type_definition.xpath('./Member').map.with_index do |member_xml, index| member_name = member_xml.attributes['Name'].value member_value = member_xml.attributes['Value'].andand.value.andand.to_i [member_value || index, member_name] end] end
# File lib/odata4/schema/enum_type.rb, line 80 def options @options = type_definition.attributes.map do |name, attr| [name, attr.value] end.to_h end
# File lib/odata4/schema/enum_type.rb, line 72 def service @schema.service end
# File lib/odata4/schema/enum_type.rb, line 76 def type_definition @type_definition end