@@ -8,72 +8,73 @@ module Elements
8
8
class Style
9
9
include Docx ::SimpleInspect
10
10
11
- def self . attributes
12
- @@attributes
13
- end
14
-
15
- def self . required_attributes
16
- @@attributes . select { |a | a [ :required ] }
17
- end
11
+ @attributes = [ ]
18
12
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
22
15
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 ] }
29
18
end
30
19
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 }
34
22
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
37
30
38
- encoded_value = converter . encode ( value )
31
+ define_method ( "#{ name } =" ) do |value |
32
+ ( required && value . nil? ) &&
33
+ raise ( Errors ::StyleRequiredPropertyValue , "Required value #{ name } " )
39
34
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 } '" )
47
37
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
50
50
51
- next encoded_value if encoded_value . nil?
51
+ next encoded_value if encoded_value . nil?
52
52
53
- node_xpath , attribute = attribute_xpath . split ( "/@" )
53
+ node_xpath , attribute = attribute_xpath . split ( "/@" )
54
54
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
65
65
66
- created_node . set_attribute ( attribute , encoded_value )
66
+ created_node . set_attribute ( attribute , encoded_value )
67
+ end
68
+ . first
67
69
end
68
- . first
69
70
end
70
- end
71
71
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 )
75
75
76
- Elements ::Style . new ( configuration , node , **attributes )
76
+ Elements ::Style . new ( configuration , node , **attributes )
77
+ end
77
78
end
78
79
79
80
def initialize ( configuration , node , **attributes )
0 commit comments