Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotated EnumType Members #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2
2.5.8
2 changes: 1 addition & 1 deletion lib/frodata/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def process_property_from_xml(property_xml)
property_type, value_type = property_type.split(/\(|\)/)
if property_type == 'Collection'
klass = ::FrOData::Properties::Collection
property_options.merge(value_type: value_type)
property_options.merge!(value_type: value_type)
else
klass = ::FrOData::PropertyRegistry[property_type]
end
Expand Down
21 changes: 19 additions & 2 deletions lib/frodata/schema/enum_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,23 @@ def namespace
# @return [Hash]
def members
@members ||= collect_members
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end at 49, 8 is not aligned with def at 47, 6.


def annotated_members
@annotated_members ||= collect_annotated_members
end

# Returns the property class that implements this `EnumType`.
# @return [Class < FrOData::Properties::Enum]
def property_class
@property_class ||= lambda { |type, members, is_flags|
@property_class ||= lambda { |type, members, annotated_members, is_flags|
klass = Class.new ::FrOData::Properties::Enum
klass.send(:define_method, :type) { type }
klass.send(:define_method, :members) { members }
klass.send(:define_method, :annotated_members) { annotated_members }
klass.send(:define_method, :is_flags?) { is_flags }
klass
}.call(type, members, is_flags?)
}.call(type, members, annotated_members, is_flags?)
end

# Returns the value of the requested member.
Expand Down Expand Up @@ -89,6 +94,18 @@ def collect_members
member_value = member_xml.attributes['Value'].andand.value.andand.to_i
[member_value || index, member_name]
end]
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end at 97, 8 is not aligned with def at 91, 6.


def collect_annotated_members
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Complex method FrOData::Schema::EnumType#collect_annotated_members (35.9)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FrOData::Schema::EnumType#collect_annotated_members has approx 6 statements

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for collect_annotated_members is too high. [21.47/15]

Hash[type_definition.xpath('./Member').map.with_index do |member_xml, index|
member_name = member_xml.attributes['Name'].value
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FrOData::Schema::EnumType#collect_annotated_members calls 'member_xml.attributes' 2 times

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FrOData::Schema::EnumType#collect_annotated_members refers to 'member_xml' more than self (maybe move it to another class?)

member_value = member_xml.attributes['Value'].andand.value.andand.to_i
annotation = nil
if member_xml.element_children.count > 0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FrOData::Schema::EnumType#collect_annotated_members calls 'member_xml.element_children' 2 times

annotation = member_xml.element_children.last.attribute('String').value
end
[member_value || index, {name: member_name, annotation: annotation}]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside { missing.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space inside } missing.

end]
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/fixtures/files/metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<Property Name="ID" Type="Edm.Int32" Nullable="false" />
<Property Name="Name" Type="Edm.String" />
<Property Name="Description" Type="Edm.String" />
<Property Name="Vertical" Type="ODataDemo.Vertical" />
<Property Name="EthicalAttributes" Type="Collection(ODataDemo.EthicalAttribute)" />
<Property Name="ReleaseDate" Type="Edm.DateTimeOffset" Nullable="false" />
<Property Name="DiscontinuedDate" Type="Edm.DateTimeOffset" />
<Property Name="Rating" Type="Edm.Int16" Nullable="false" />
Expand All @@ -18,6 +20,22 @@
<NavigationProperty Name="Supplier" Type="ODataDemo.Supplier" Partner="Products" />
<NavigationProperty Name="ProductDetail" Type="ODataDemo.ProductDetail" Partner="Product" />
</EntityType>
<EnumType Name="Vertical" UnderlyingType="Edm.Int64">
<Member Name="OutdoorsAndNature" Value="1">
<Annotation Term="Dontcare.SomeTerm" String="Outdoor and Nature Products"/>
</Member>
<Member Name="HomeAndOffice" Value="2">
<Annotation Term="Dontcare.SomeTerm2" String="Home and Office Products"/>
</Member>
</EnumType>
<EnumType Name="EthicalAttribute" UnderlyingType="Edm.Int64">
<Member Name="CarbonNeutral" Value="1">
<Annotation Term="Dontcare.SomeTerm" String="Carbon Neutral"/>
</Member>
<Member Name="FairTrade" Value="2">
<Annotation Term="Dontcare.SomeTerm2" String="Fair Trade"/>
</Member>
</EnumType>
<EnumType Name="ProductStatus" UnderlyingType="Edm.Byte">
<Member Name="Available" />
<Member Name="LowStock" />
Expand Down
2 changes: 1 addition & 1 deletion spec/frodata/entity/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
it { expect(subject.get_property('ReleaseDate')).to be_a(FrOData::Properties::DateTimeOffset) }
it { expect(subject.get_property('DiscontinuedDate')).to be_a(FrOData::Properties::DateTimeOffset) }
it { expect(subject.get_property('Rating')).to be_a(FrOData::Properties::Integer) }
it { expect(subject.get_property('Price')).to be_a(FrOData::Properties::Double) }
it { expect(subject.get_property('Price')).to be_a(FrOData::Properties::Float) }

# Navigation property proxies
it { expect(subject.get_property('Categories')).to be_a(FrOData::NavigationProperty::Proxy)}
Expand Down
18 changes: 18 additions & 0 deletions spec/frodata/property_collection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

describe FrOData::Property do
let(:service) do
FrOData::Service.new('http://services.odata.org/V4/OData/OData.svc', metadata_file: metadata_file)
end
let(:metadata_file) { 'spec/fixtures/files/metadata.xml' }

describe '#type' do
it 'returns the right type' do
t = service.schemas['ODataDemo'].properties_for_entity('Product')['ProductStatus'].type
expect(t).to eq('ODataDemo.ProductStatus')

t = service.schemas['ODataDemo'].properties_for_entity('Product')['EthicalAttributes'].type
expect(t).to eq('Collection(ODataDemo.EthicalAttribute)')
end
end
end
37 changes: 37 additions & 0 deletions spec/frodata/schema/annotated_enum_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'spec_helper'

describe FrOData::Schema::EnumType, vcr: {cassette_name: 'schema/enum_type_specs'} do
before(:example) do
FrOData::Service.new('http://services.odata.org/V4/OData/OData.svc', name: 'ODataDemo', metadata_file: metadata_file)
end

let(:metadata_file) { 'spec/fixtures/files/metadata.xml' }
let(:service) { FrOData::ServiceRegistry['ODataDemo'] }

let(:enum_type) { service.enum_types['ODataDemo.Vertical'] }
let(:subject) { enum_type.property_class.new('Vertical', nil) }

describe 'is properly parsed from service metadata' do
it { expect(enum_type.name).to eq('Vertical') }
it { expect(enum_type.namespace).to eq('ODataDemo') }
it { expect(enum_type.type).to eq('ODataDemo.Vertical') }
it { expect(enum_type.is_flags?).to eq(false) }
it { expect(enum_type.underlying_type).to eq('Edm.Int64') }
it { expect(enum_type.members.values).to eq(%w{OutdoorsAndNature HomeAndOffice}) }
it {
expect(enum_type.annotated_members).to eq({
1 => { name: 'OutdoorsAndNature', annotation: 'Outdoor and Nature Products' },
2 => { name: 'HomeAndOffice', annotation: 'Home and Office Products' }
})
}
end

# Check property instance inheritance hierarchy
it { expect(subject).to be_a(FrOData::Property) }
it { expect(subject).to be_a(FrOData::Properties::Enum) }

it { expect(subject).to respond_to(:name) }
it { expect(subject).to respond_to(:type) }
it { expect(subject).to respond_to(:members) }

end
4 changes: 2 additions & 2 deletions spec/frodata/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

let(:entity_types) { %w{Product FeaturedProduct ProductDetail Category Supplier Person Customer Employee PersonDetail Advertisement} }
let(:complex_types) { %w{Address} }
let(:enum_types) { %w{ProductStatus} }
let(:enum_types) { %w{Vertical EthicalAttribute ProductStatus} }

describe '#namespace' do
it { expect(subject).to respond_to(:namespace) }
Expand Down Expand Up @@ -43,7 +43,7 @@

describe '#enum_types' do
it { expect(subject).to respond_to(:enum_types) }
it { expect(subject.enum_types.size).to eq(1) }
it { expect(subject.enum_types.size).to eq(3) }
it { expect(subject.enum_types.keys).to eq(enum_types)}
end

Expand Down
6 changes: 4 additions & 2 deletions spec/frodata/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@

describe '#enum_types' do
it { expect(subject).to respond_to(:enum_types) }
it { expect(subject.enum_types.size).to eq(1) }
it { expect(subject.enum_types.keys).to eq(['ODataDemo.ProductStatus'])}
it { expect(subject.enum_types.size).to eq(3) }
it { expect(subject.enum_types.keys).to eq(['ODataDemo.Vertical', 'ODataDemo.EthicalAttribute', 'ODataDemo.ProductStatus'])}
end

describe '#namespace' do
Expand Down Expand Up @@ -190,6 +190,8 @@
ID
Name
Description
Vertical
EthicalAttributes
ReleaseDate
DiscontinuedDate
Rating
Expand Down