Class: OData4::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/odata4/schema.rb,
lib/odata4/schema/enum_type.rb,
lib/odata4/schema/complex_type.rb

Defined Under Namespace

Classes: ComplexType, EnumType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_definition, service) ⇒ Schema

Creates a new schema.

Parameters:

  • schema_definition (Nokogiri::XML)

    The schema’s XML definition

  • service (OData4::Service)

    The schema’s parent service



15
16
17
18
# File 'lib/odata4/schema.rb', line 15

def initialize(schema_definition, service)
  @metadata = schema_definition
  @service = service
end

Instance Attribute Details

#metadataObject (readonly)

The schema’s metadata (i.e its XML definition)



9
10
11
# File 'lib/odata4/schema.rb', line 9

def 
  @metadata
end

#serviceObject (readonly)

The schema’s parent service



7
8
9
# File 'lib/odata4/schema.rb', line 7

def service
  @service
end

Instance Method Details

#actionsArray<String>

Returns a list of actions defined by the schema.

Returns:

  • (Array<String>)


28
29
30
31
32
# File 'lib/odata4/schema.rb', line 28

def actions
  @actions ||= .xpath('//Action').map do |action|
    action.attributes['Name'].value
  end
end

#complex_typesHash<String, OData4::Schema::ComplexType>

Returns a list of ‘ComplexType`s defined by the schema.

Returns:



44
45
46
47
48
49
50
51
# File 'lib/odata4/schema.rb', line 44

def complex_types
  @complex_types ||= .xpath('//ComplexType').map do |entity|
    [
      entity.attributes['Name'].value,
      ComplexType.new(entity, self)
    ]
  end.to_h
end

#entity_typesArray<String>

Returns a list of entities defined by the schema.

Returns:

  • (Array<String>)


36
37
38
39
40
# File 'lib/odata4/schema.rb', line 36

def entity_types
  @entity_types ||= .xpath('//EntityType').map do |entity|
    entity.attributes['Name'].value
  end
end

#enum_typesHash<String, OData4::Schema::EnumType>

Returns a list of EnumTypes defined by the schema.

Returns:



55
56
57
58
59
60
61
62
# File 'lib/odata4/schema.rb', line 55

def enum_types
  @enum_types ||= .xpath('//EnumType').map do |entity|
    [
      entity.attributes['Name'].value,
      EnumType.new(entity, self)
    ]
  end.to_h
end

#functionsArray<String>

Returns a list of functions defined by the schema.

Returns:

  • (Array<String>)


66
67
68
69
70
# File 'lib/odata4/schema.rb', line 66

def functions
  @functions ||= .xpath('//Function').map do |function|
    function.attributes['Name'].value
  end
end

#get_property_type(entity_name, property_name) ⇒ String

Get the property type for an entity from metadata.

Parameters:

  • entity_name (to_s)

    the name of the relevant entity

  • property_name (to_s)

    the property name needed

Returns:

  • (String)

    the name of the property’s type



102
103
104
# File 'lib/odata4/schema.rb', line 102

def get_property_type(entity_name, property_name)
  .xpath("//EntityType[@Name='#{entity_name}']/Property[@Name='#{property_name}']").first.attributes['Type'].value
end

#namespaceString

Returns the schema’s ‘Namespace` attribute (mandatory).

Returns:

  • (String)


22
23
24
# File 'lib/odata4/schema.rb', line 22

def namespace
  @namespace ||= .attributes['Namespace'].value
end

Returns a hash for finding an association through an entity type’s defined NavigationProperty elements.

Returns:



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/odata4/schema.rb', line 83

def navigation_properties
  @navigation_properties ||= .xpath('//EntityType').map do |entity_type_def|
    [
      entity_type_def.attributes['Name'].value,
      entity_type_def.xpath('./NavigationProperty').map do |nav_property_def|
        [
          nav_property_def.attributes['Name'].value,
          ::OData4::NavigationProperty.build(nav_property_def)
        ]
      end.to_h
    ]
  end.to_h
end

#primary_key_for(entity_name) ⇒ String

Get the primary key for the supplied Entity.

Parameters:

  • entity_name (to_s)

Returns:

  • (String)


110
111
112
# File 'lib/odata4/schema.rb', line 110

def primary_key_for(entity_name)
  .xpath("//EntityType[@Name='#{entity_name}']/Key/PropertyRef").first.attributes['Name'].value
end

#properties_for_entity(entity_name) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the list of properties and their various options for the supplied Entity name.

Parameters:

  • entity_name (to_s)

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


119
120
121
122
123
124
125
126
127
128
# File 'lib/odata4/schema.rb', line 119

def properties_for_entity(entity_name)
  type_definition = .xpath("//EntityType[@Name='#{entity_name}']").first
  raise ArgumentError, "Unknown EntityType: #{entity_name}" if type_definition.nil?
  properties_to_return = {}
  type_definition.xpath('./Property').each do |property_xml|
    property_name, property = process_property_from_xml(property_xml)
    properties_to_return[property_name] = property
  end
  properties_to_return
end

#type_definitionsArray<String>

Returns a list of type definitions defined by the schema.

Returns:

  • (Array<String>)


74
75
76
77
78
# File 'lib/odata4/schema.rb', line 74

def type_definitions
  @typedefs ||= .xpath('//TypeDefinition').map do |typedef|
    typedef.attributes['Name'].value
  end
end