Skip to content

Commit

Permalink
#2 SET
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Mar 19, 2016
1 parent 2372733 commit 2e5a329
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ MethodLength:
Max: 30
Style/ClassLength:
Max: 150
Style/CyclomaticComplexity:
Max: 20
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ in file `doc.xml`:
```xml
<books>
<book isbn="0735619654">Object Thinking</book>
<book isbn="1519166915">Elegan Objects</book>
<book isbn="1519166915">Elegant Objects</book>
</books>
```

Expand All @@ -46,7 +46,7 @@ Now, say, you want to add one more book there:
$ xembly --xml doc.xml 'XPATH "/books"; ADD "book"; ATTR "isbn", "0201379430"; SET "Object Design";'
<books>
<book isbn="0735619654">Object Thinking</book>
<book isbn="1519166915">Elegan Objects</book>
<book isbn="1519166915">Elegant Objects</book>
<book isbn="0201379430">Object Design</book>
</books>
```
Expand Down
2 changes: 1 addition & 1 deletion bin/xembly
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require 'slop'
require 'xembly/version'

opts = Slop.parse(ARGV, strict: true, help: true) do |o|
o.banner = "Usage (#{Xembly::VERSION}): xembly [options]"
o.banner = "Usage (#{Xembly::VERSION}): xembly [options] [directives]"
o.on '-h', '--help', 'Print help' do
puts o
exit
Expand Down
3 changes: 3 additions & 0 deletions features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ Feature: Command Line Processing
"""
XPATH "/books";
ADD "book";
ATTR "isbn", "1519166915";
SET "Elegant Objects";
"""
When I run bin/xembly with "-v -d dirs.txt -f out.xml -x text.xml"
Then Exit code is zero
And Stdout contains "reading text.xml"
And XML file "out.xml" matches "/books[count(book) = 3]"
And XML file "out.xml" matches "/books/book[@isbn='1519166915' and .='Elegant Objects']"

Scenario: Rejects unknown options
When I run bin/xembly with "--some-unknown-option"
Expand Down
11 changes: 11 additions & 0 deletions lib/xembly/directives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require 'xembly/add'
require 'xembly/attr'
require 'xembly/xpath'
require 'xembly/set'

module Xembly
# Directives
Expand Down Expand Up @@ -54,8 +55,18 @@ def self.map(text)
case cmd.upcase
when 'ADD'
Add.new(args[0])
when 'ADDIF'
AddIf.new(args[0])
when 'ATTR'
Attr.new(args[0], args[1])
when 'REMOVE'
Remove.new
when 'SET'
Set.new(args[0])
when 'STRICT'
Strict.new(args[0])
when 'UP'
Up.new
when 'XPATH'
Xpath.new(args[0])
else
Expand Down
41 changes: 41 additions & 0 deletions lib/xembly/set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# encoding: utf-8
#
# Copyright (c) 2016 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'nokogiri'

module Xembly
# SET directive
class Set
# Ctor.
# +value+:: Text value to set
def initialize(value)
@value = value
end

def exec(_, cursor)
cursor.each do |node|
node.content = @value
end
cursor
end
end
end
43 changes: 43 additions & 0 deletions test/test_set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# encoding: utf-8
#
# Copyright (c) 2016 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'xembly/set'
require 'test__helper'

# Xembly::Set tests.
# Author:: Yegor Bugayenko (yegor@teamed.io)
# Copyright:: Copyright (c) 2016 Yegor Bugayenko
# License:: MIT
class TestSet < XeTest
def test_sets_values
dom = Nokogiri::XML('<books><book/></books>')
Xembly::Set.new('hello').exec(dom, [dom.xpath('/books/book').first])
matches(
dom.to_xml,
[
'/books',
'/books[count(book)=1]',
'/books/book[.="hello"]'
]
)
end
end
8 changes: 6 additions & 2 deletions test/test_xembly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ def test_reading_from_file
xml = File.join(dir, 'input.xml')
File.write(xml, '<books/>')
dirs = File.join(dir, 'dirs.txt')
File.write(dirs, 'XPATH "/books"; ADD "book"; ATTR "id", "123";')
File.write(
dirs,
'XPATH "/books"; ADD "book"; ATTR "id", "123"; SET "Elegant Objects";'
)
opts = opts(['--xml', xml, '--dirs', dirs])
matches(
Xembly::Base.new(opts).xml,
[
'/books',
'/books[count(book)=1]',
'/books/book[@id=123]'
'/books/book[@id=123]',
'/books/book[@id=123 and .="Elegant Objects"]'
]
)
end
Expand Down

0 comments on commit 2e5a329

Please sign in to comment.