diff --git a/lib/victor/component.rb b/lib/victor/component.rb index 4f4bfba..7e8bcfc 100644 --- a/lib/victor/component.rb +++ b/lib/victor/component.rb @@ -5,7 +5,7 @@ class Component extend Forwardable include Marshaling - def_delegators :svg, :save, :render, :content, :element, :css, :to_s + def_delegators :svg, :save, :render, :content, :element, :tag, :css, :to_s # Marshaling data def marshaling = %i[width height x y svg merged_css] diff --git a/lib/victor/dsl.rb b/lib/victor/dsl.rb index f666616..478c2ca 100644 --- a/lib/victor/dsl.rb +++ b/lib/victor/dsl.rb @@ -3,7 +3,7 @@ module Victor module DSL extend Forwardable - def_delegators :svg, :setup, :build, :save, :render, :append, :element, :css + def_delegators :svg, :setup, :build, :save, :render, :append, :element, :tag, :css def svg @svg ||= Victor::SVG.new diff --git a/lib/victor/svg.rb b/lib/victor/svg.rb index 71bd64a..064a076 100644 --- a/lib/victor/svg.rb +++ b/lib/victor/svg.rb @@ -1,7 +1,7 @@ module Victor class SVG < SVGBase def method_missing(method_sym, ...) - element(method_sym, ...) + tag(method_sym, ...) end def respond_to_missing?(*) diff --git a/lib/victor/svg_base.rb b/lib/victor/svg_base.rb index 1b9ead8..b14d15e 100644 --- a/lib/victor/svg_base.rb +++ b/lib/victor/svg_base.rb @@ -40,7 +40,7 @@ def build(&block) instance_eval(&block) end - def element(name, value = nil, attributes = {}) + def tag(name, value = nil, attributes = {}) if value.is_a? Hash attributes = value value = nil @@ -68,6 +68,7 @@ def element(name, value = nil, attributes = {}) content.push "<#{name} #{attributes}/>" end end + alias element tag def css(defs = nil) @css ||= {} diff --git a/spec/victor/component_spec.rb b/spec/victor/component_spec.rb index 9be14e8..8fd1d00 100644 --- a/spec/victor/component_spec.rb +++ b/spec/victor/component_spec.rb @@ -37,7 +37,7 @@ context 'when all required methods are implemented' do let(:svg) do - double save: true, render: true, content: true, element: true, to_s: true + double save: true, render: true, content: true, element: true, tag: true, to_s: true end before do @@ -66,6 +66,13 @@ end end + describe '#tag' do + it 'delegates to SVG' do + expect(svg).to receive(:tag).with(:rect) + subject.tag :rect + end + end + describe '#element' do it 'delegates to SVG' do expect(svg).to receive(:element).with(:rect) diff --git a/spec/victor/svg_spec.rb b/spec/victor/svg_spec.rb index 3c47cc3..0fd12fd 100644 --- a/spec/victor/svg_spec.rb +++ b/spec/victor/svg_spec.rb @@ -123,25 +123,25 @@ end end - describe '#element' do + describe '#tag' do it 'generates xml without attributes' do - subject.element 'anything' + subject.tag 'anything' expect(subject.content).to eq [''] end it 'generates xml with attributes' do - subject.element 'anything', at: 'all' + subject.tag 'anything', at: 'all' expect(subject.content).to eq [''] end it 'converts snake attributes to kebabs' do - subject.element 'text', font_family: 'arial' + subject.tag 'text', font_family: 'arial' expect(subject.content).to eq [''] end context 'with hashed attributes' do it 'converts attributes to style syntax' do - subject.element 'cool', style: { color: 'red', anything: 10 } + subject.tag 'cool', style: { color: 'red', anything: 10 } expect(subject.content).to eq [''] end end @@ -163,10 +163,10 @@ # https://github.com/DannyBen/victor/pull/59 it "ignores the block's return value" do subject.build do - element :group do - element :one - element :two - element :three if false + tag :group do + tag :one + tag :two + tag :three if false end end expect(subject.content).to eq ['', '', '', ''] @@ -175,29 +175,29 @@ context 'with a plain text value' do it 'generates a container element' do - subject.element 'prison', 'inmate', number: '6' + subject.tag 'prison', 'inmate', number: '6' expect(subject.content).to eq ['', 'inmate', ''] end it 'escapes XML' do - subject.element 'text', 'For Dumb & Dumber, 2 > 3' + subject.tag 'text', 'For Dumb & Dumber, 2 > 3' expect(subject.content).to eq ['', 'For Dumb & Dumber, 2 > 3', ''] end context 'when the element is an underscore' do it 'generates a tagless element' do - subject.element '_', 'You are (not) surrounded!' + subject.tag '_', 'You are (not) surrounded!' expect(subject.content).to eq ['You are (not) surrounded!'] end it 'escapes XML' do - subject.element '_', 'For Dumb & Dumber, 2 > 3' + subject.tag '_', 'For Dumb & Dumber, 2 > 3' expect(subject.content).to eq ['For Dumb & Dumber, 2 > 3'] end context 'when the element is _!' do it 'does not escape XML' do - subject.element '_!', 'For Dumb & Dumber, 2 > 3' + subject.tag '_!', 'For Dumb & Dumber, 2 > 3' expect(subject.content).to eq ['For Dumb & Dumber, 2 > 3'] end end @@ -205,13 +205,19 @@ context 'when the element name ends with !' do it 'does not escape XML' do - subject.element 'text!', 'For Dumb & Dumber, 2 > 3' + subject.tag 'text!', 'For Dumb & Dumber, 2 > 3' expect(subject.content).to eq ['', 'For Dumb & Dumber, 2 > 3', ''] end end end end + describe '#element' do + it 'is an alias for #tag' do + expect(subject.method(:element)).to eq subject.method(:tag) + end + end + describe '#css' do context 'when no css rules were added' do it 'returns an empty hash' do @@ -243,13 +249,13 @@ end describe '#method_missing' do - it 'calls #element' do - expect(subject).to receive(:element).with(:anything) + it 'calls #tag' do + expect(subject).to receive(:tag).with(:anything) subject.anything end - it 'passes arguments to #element' do - expect(subject).to receive(:element).with(:anything, { at: 'all' }) + it 'passes arguments to #tag' do + expect(subject).to receive(:tag).with(:anything, { at: 'all' }) subject.anything at: 'all' end end