Skip to content

Commit

Permalink
1.2.1: fixes attributes support
Browse files Browse the repository at this point in the history
  • Loading branch information
julbouln committed Jan 8, 2021
1 parent 1ed1924 commit 14d2819
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 49 deletions.
2 changes: 1 addition & 1 deletion bin/onix_pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
end
end
if msg.sent_date_time
puts "Sent date: #{msg.sent_date_time}"
puts "Sent date: #{msg.sent_date_time.human}"
end

msg.products.each do |product|
Expand Down
28 changes: 8 additions & 20 deletions lib/onix/contributor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'onix/subset'
require 'onix/website'
require 'onix/contributor_place'

module ONIX
class Contributor < SubsetDSL
Expand Down Expand Up @@ -36,23 +37,23 @@ class Contributor < SubsetDSL
# flatten person name (firstname lastname)
# @return [String]
def name
return @person_name if @person_name
return person_name if person_name

if @key_names
if @names_before_key
return "#{@names_before_key} #{@key_names}"
if key_names
if names_before_key
return "#{names_before_key} #{key_names}"
else
return @key_names
return key_names
end
end

@corporate_name
corporate_name
end

# inverted flatten person name
# @return [String]
def inverted_name
@person_name_inverted || @corporate_name_inverted
person_name_inverted || corporate_name_inverted
end

# biography string with HTML
Expand Down Expand Up @@ -91,17 +92,4 @@ def death_date

# @!endgroup
end

class ContributorPlace < SubsetDSL
element "ContributorPlaceRelator", :subset
element "CountryCode", :subset

def relator
@contributor_place_relator
end

def country_code
@country_code.code
end
end
end
12 changes: 12 additions & 0 deletions lib/onix/contributor_place.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module ONIX
class ContributorPlace < SubsetDSL
element "ContributorPlaceRelator", :subset, :shortcut => :relator, :cardinality => 1
element "CountryCode", :subset, :cardinality => 0..1
element "RegionCode", :subset, :cardinality => 0..1
elements "LocationName", :text, :cardinality => 0..n

def country_code
@country_code.code
end
end
end
4 changes: 2 additions & 2 deletions lib/onix/product_part.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def reflowable?
# raw part file description string without HTML
# @return [String]
def raw_file_description
if @product_form_description
Helper.strip_html(@product_form_description).gsub(/\s+/, " ").strip
if product_form_description
Helper.strip_html(product_form_description).gsub(/\s+/, " ").strip
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/onix/resource_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ResourceVersion < SubsetDSL

def filename
if @resource_form.human == "DownloadableFile"
@resource_links.first
resource_links.first
end
end

Expand Down
47 changes: 28 additions & 19 deletions lib/onix/subset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,11 @@ def shortcut
end

def parse_lambda(v)
if @parse_lambda
@parse_lambda.call(v)
else
v
end
@parse_lambda ? @parse_lambda.call(v) : v
end

def serialize_lambda(v)
if @serialize_lambda
@serialize_lambda.call(v)
else
v
end
@serialize_lambda ? @serialize_lambda.call(v) : v
end

def is_array?
Expand Down Expand Up @@ -292,12 +284,25 @@ def self.element(name, type, options = {})
@elements[short_name] = @elements[name].dup
@elements[short_name].short = true
end

attr_accessor @elements[name].to_sym

alias_method "#{@elements[name].underscore_name}_with_attributes".to_sym, @elements[name].to_sym

current_element = @elements[name]
define_method current_element.to_sym do |args = nil|
val = instance_variable_get(current_element.to_instance)
if val.respond_to?(:__getobj__)
val.__getobj__
else
val
end
end

if @elements[name].shortcut
current_element = @elements[name]
define_method current_element.shortcut do |args = nil|
instance_variable_get(current_element.to_instance)
end
alias_method "#{current_element.shortcut.to_s}_with_attributes".to_sym, "#{@elements[name].underscore_name}_with_attributes".to_sym
alias_method current_element.shortcut, @elements[name].to_sym
end
end

Expand Down Expand Up @@ -376,20 +381,17 @@ def parse(n)
name = t.name
e = self.get_registered_element(name)
if e
primitive = true
case e.type
when :subset
klass = self.get_class(e.class_name)
unless klass
raise UnknownElement, e.class_name
end
val = klass.parse(t)
primitive = false
when :text
if t.attributes.length > 0
val = TextWithAttributes.new(t.attributes["textformat"] ? t.children.map { |x| x.to_s }.join.strip : t.text)
val.parse(t.attributes)
else
val = t.text
end
val = t.text
when :integer
val = t.text.to_i
when :float
Expand All @@ -413,13 +415,20 @@ def parse(n)
else
val = t.text
end

if val
if primitive && t.attributes.length > 0
val = TextWithAttributes.new(t.attributes["textformat"] ? t.children.map { |x| x.to_s }.join.strip : val)
val.parse(t.attributes)
end

if e.is_array?
instance_variable_get(e.to_instance).send(:push, val)
else
instance_variable_set(e.to_instance, e.parse_lambda(val))
end
end

else
unsupported(t)
end
Expand Down
12 changes: 6 additions & 6 deletions lib/onix/title_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class TitleElement < SubsetDSL
# @!group High level
# flatten title string
def title
if @title_text
@title_text
if title_text
title_text
else
if @title_without_prefix
if @title_prefix
"#{@title_prefix} #{@title_without_prefix}"
if title_without_prefix
if title_prefix
"#{title_prefix} #{title_without_prefix}"
else
@title_without_prefix
title_without_prefix
end
end
end
Expand Down

0 comments on commit 14d2819

Please sign in to comment.