lib/jini.rb in jini-0.0.4 vs lib/jini.rb in jini-0.0.5

- old
+ new

@@ -21,15 +21,17 @@ # 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. # It's a simple XPATH builder. +# ``` # require 'jini' -# xpath = Jini.new('body') +# xpath = Jini.new('parent') # .add_path(node: 'child') -# .add_path(node: 'child') -# .to_s // /body/child/child +# .add_path(node: 'toy') +# .to_s // body/child/toy +# ``` class Jini # When path not valid class InvalidPath < StandardError; end # @param head [String] @@ -85,18 +87,19 @@ # Access by index. # Addition '[index]' to xpath # @param position [Integer] number # @return [Jini] object def at(position) + raise InvalidPath, 'Cant use at after selection' if @head.include? '::' Jini.new("#{@head}[#{position}]") end # Replace all '/' to '::' symbols # if path doesn't contain invalid symbols for selection # @return [Jini] selection def selection - if @head.include?('[') || @head.include?(']') || @head.include?('@') + if @head.include?('[') || @head.include?(']') || @head.include?('@') || @head.include?('//') raise InvalidPath, 'Cannot select, path contains bad symbols' end Jini.new(@head.gsub('/', '::').to_s) end @@ -120,7 +123,14 @@ .gsub( /(\[@?#{name}="[^"]+"(\[\]+|\]))/, '' ) ) + end + + # Adds '|' to head + # @param [String] node + # @return [Jini] with '|node' on tail + def or(node) + Jini.new("#{@head}|#{node}") end end