Skip to content

Commit

Permalink
Upgrades to the class generator to support a modern libxml-ruby and a…
Browse files Browse the repository at this point in the history
…ctivesupport.
  • Loading branch information
rubiety committed Jul 30, 2010
1 parent 3bc46fa commit d7124d5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ require 'rake/testtask'
require 'rake/rdoctask'
require 'rake/gempackagetask'
require 'rake/contrib/rubyforgepublisher'
gem 'activesupport'
gem 'libxml-ruby'
require 'xml'

PKG_VERSION = "0.12.0"
PKG_NAME = "ebayapi"
Expand Down
2 changes: 1 addition & 1 deletion lib/ebay/schema/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
require 'mapper/class_definition'

# Remove the broken unpluralization of 'ies' words
Inflector.inflections.plurals.delete_if{|inflection| inflection.first == /([^aeiouy]|qu)ies$/i }
ActiveSupport::Inflector.inflections.plurals.delete_if{|inflection| inflection.first == /([^aeiouy]|qu)ies$/i }
6 changes: 2 additions & 4 deletions lib/ebay/schema/mapper/ebay_schema_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ def initialize(schema, data)
@xml = XML::Parser.string(data).parse.root

@elements = schema.collect_elements
@elements.uniq!
@attributes = schema.collect_attributes
@attributes.uniq!

# Only want the simple types that have an enumeration (codes)
@simple_types = schema.collect_simpletypes
@simple_types.uniq!

@code_types = @simple_types.reject{ |e| e.restriction.enumeration.empty? || Ignores.include?(e.name.name) }

@complex_types = schema.collect_complextypes
@complex_types.uniq!

@fault_types = nil
if schema.respond_to?(:collect_faulttypes)
@fault_types = schema.collect_faulttypes
Expand Down
4 changes: 2 additions & 2 deletions lib/ebay/schema/mapper/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def accessor_name
name = ebay_underscore(@name)
if name =~ /_array$/
name.gsub!(/_array$/, '')
Inflector.pluralize(name)
ActiveSupport::Inflector.pluralize(name)
else
name
end
end

def xml_mapping_node_type
override_type || Inflector.demodulize(self.class.to_s).underscore
override_type || ActiveSupport::Inflector.demodulize(self.class.to_s).underscore
end

def declaration
Expand Down
56 changes: 38 additions & 18 deletions lib/ebay/schema/mapper/ruby_class_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def generate_class

customization = ClassTemplate.new(ebay_underscore(name))
customization.load if customization.exists?

if request? || response?
class_name = ebay_camelize(name).gsub(/(Request|Response)$/, '')
else
Expand Down Expand Up @@ -134,13 +134,22 @@ def select_template
end

def find_base
type = @type.complexcontent.extension.base
@base = @complex_types.find_name(type.name) unless type.nil?
rescue NoMethodError
return nil if @type.nil?

the_base = case
when @type.respond_to?(:complexcontent) && @type.complexcontent.respond_to?(:extension)
@type.complexcontent.extension.base
when @type.respond_to?(:complexcontent) && @type.complexcontent.respond_to?(:base)
@type.complexcontent.base
else
nil
end

@base = @complex_types.find_name(the_base.name) unless the_base.nil?
end

def non_inherited_elements
return @type.elements if @base.nil?
return @type.each_element {} if @base.nil?

@type.elements.reject do |e|
@base.find_element(e.name)
Expand All @@ -158,9 +167,12 @@ def generate_nodes
result = []
result << build_node_for_built_in_type(trim_type(name), content.extension.base.name, :field => '', :min => '0')

result.concat(content.attributes.collect do |a|
TextNode.new(trim_code_type(a.type.name), :field => "@#{a.name.name}", :min => '0')
end)
unless !content.respond_to?(:attributes) or content.attributes.nil?
result.concat(content.attributes.collect do |a|
TextNode.new(trim_code_type(a.type.name), :field => "@#{a.name.name}", :min => '0')
end)
end

result
else
result = nodes_for_complex_elements
Expand All @@ -175,9 +187,15 @@ def nodes_for_complex_elements
end.flatten
rescue NoMethodError
# Hack for when the type doesn't respond to elements
@type.complexcontent.content.content.elements.collect do |e|
node_for(e)
end.flatten
if @type.complexcontent.nil?
[]
elsif @type.complexcontent.content.nil?
[]
else
@type.complexcontent.content.elements.collect do |e|
node_for(e)
end.flatten
end
end
end

Expand Down Expand Up @@ -246,9 +264,15 @@ def build_node_for_complex_type(name, type, options)
if simple_type
TextNode.new(name, options)
elsif element = @complex_types.find_name(type)
if element.elements.size == 1 && element.elements[0].maxoccurs == "unbounded"
element_elements = element.each_element {}

if element_elements.nil? and type == 'AmountType'
MoneyNode.new(name, options)
elsif element_elements.nil?
ObjectNode.new(name, options)
elsif element_elements.size == 1 && element_elements[0].maxoccurs == "unbounded"
# Found a container!
child = element.elements[0]
child = element_elements[0]

ignored = %w( MemberMessage BidApproval PromotionalSaleDetails BidAssistantList )

Expand All @@ -264,11 +288,7 @@ def build_node_for_complex_type(name, type, options)
ArrayNode.new(name, options)
end
else
if type == 'AmountType'
MoneyNode.new(name, options)
else
ObjectNode.new(name, options)
end
ObjectNode.new(name, options)
end
end
else
Expand Down

0 comments on commit d7124d5

Please sign in to comment.