@@ -8,72 +8,73 @@ module Elements
88 class Style
99 include Docx ::SimpleInspect
1010
11- def self . attributes
12- @@attributes
13- end
14-
15- def self . required_attributes
16- @@attributes . select { |a | a [ :required ] }
17- end
11+ @attributes = [ ]
1812
19- def self . attribute ( name , *selectors , required : false , converter : Converters ::DefaultValueConverter , validator : Validators ::DefaultValidator )
20- @@attributes ||= [ ]
21- @@attributes << { name : name , selectors : selectors , required : required , converter : converter , validator : validator }
13+ class << self
14+ attr_accessor :attributes
2215
23- define_method ( name ) do
24- selectors
25- . lazy
26- . filter_map { |node_xpath | node . at_xpath ( node_xpath ) &.value }
27- . map { |value | converter . decode ( value ) }
28- . first
16+ def required_attributes
17+ attributes . select { |a | a [ :required ] }
2918 end
3019
31- define_method ( "#{ name } =" ) do |value |
32- ( required && value . nil? ) &&
33- raise ( Errors ::StyleRequiredPropertyValue , "Required value #{ name } " )
20+ def attribute ( name , *selectors , required : false , converter : Converters ::DefaultValueConverter , validator : Validators ::DefaultValidator )
21+ attributes << { name : name , selectors : selectors , required : required , converter : converter , validator : validator }
3422
35- validator . validate ( value ) ||
36- raise ( Errors ::StyleInvalidPropertyValue , "Invalid value for #{ name } : '#{ value . nil? ? "nil" : value } '" )
23+ define_method ( name ) do
24+ selectors
25+ . lazy
26+ . filter_map { |node_xpath | node . at_xpath ( node_xpath ) &.value }
27+ . map { |value | converter . decode ( value ) }
28+ . first
29+ end
3730
38- encoded_value = converter . encode ( value )
31+ define_method ( "#{ name } =" ) do |value |
32+ ( required && value . nil? ) &&
33+ raise ( Errors ::StyleRequiredPropertyValue , "Required value #{ name } " )
3934
40- selectors . map do |attribute_xpath |
41- if ( existing_attribute = node . at_xpath ( attribute_xpath ) )
42- if encoded_value . nil?
43- existing_attribute . remove
44- else
45- existing_attribute . value = encoded_value . to_s
46- end
35+ validator . validate ( value ) ||
36+ raise ( Errors ::StyleInvalidPropertyValue , "Invalid value for #{ name } : '#{ value . nil? ? "nil" : value } '" )
4737
48- next encoded_value
49- end
38+ encoded_value = converter . encode ( value )
39+
40+ selectors . map do |attribute_xpath |
41+ if ( existing_attribute = node . at_xpath ( attribute_xpath ) )
42+ if encoded_value . nil?
43+ existing_attribute . remove
44+ else
45+ existing_attribute . value = encoded_value . to_s
46+ end
47+
48+ next encoded_value
49+ end
5050
51- next encoded_value if encoded_value . nil?
51+ next encoded_value if encoded_value . nil?
5252
53- node_xpath , attribute = attribute_xpath . split ( "/@" )
53+ node_xpath , attribute = attribute_xpath . split ( "/@" )
5454
55- created_node =
56- node_xpath
57- . split ( "/" )
58- . reduce ( node ) do |parent_node , child_xpath |
59- # find the child node
60- parent_node . at_xpath ( child_xpath ) ||
61- # or create the child node
62- Nokogiri ::XML ::Node . new ( child_xpath , parent_node )
63- . tap { |created_child_node | parent_node << created_child_node }
64- end
55+ created_node =
56+ node_xpath
57+ . split ( "/" )
58+ . reduce ( node ) do |parent_node , child_xpath |
59+ # find the child node
60+ parent_node . at_xpath ( child_xpath ) ||
61+ # or create the child node
62+ Nokogiri ::XML ::Node . new ( child_xpath , parent_node )
63+ . tap { |created_child_node | parent_node << created_child_node }
64+ end
6565
66- created_node . set_attribute ( attribute , encoded_value )
66+ created_node . set_attribute ( attribute , encoded_value )
67+ end
68+ . first
6769 end
68- . first
6970 end
70- end
7171
72- def self . create ( configuration , attributes = { } )
73- node = Nokogiri ::XML ::Node . new ( "w:style" , configuration . styles_parent_node )
74- configuration . styles_parent_node . add_child ( node )
72+ def create ( configuration , attributes = { } )
73+ node = Nokogiri ::XML ::Node . new ( "w:style" , configuration . styles_parent_node )
74+ configuration . styles_parent_node . add_child ( node )
7575
76- Elements ::Style . new ( configuration , node , **attributes )
76+ Elements ::Style . new ( configuration , node , **attributes )
77+ end
7778 end
7879
7980 def initialize ( configuration , node , **attributes )
0 commit comments