awesome_xml 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/awesome_xml.rb +79 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c56c2831ab0626cb308dc5dea679f2f854077101
|
4
|
+
data.tar.gz: f25a4b311f2cad636f5deba9797cfd48fb014e8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 27448af39a7950d1c96a3e1d814037acaeb63dc08ac2cd2d2034889899feefacd3fc1702ffffdbdd6d409262a7034dbc531ea5b7b0e393f75db57eaf70997d85
|
7
|
+
data.tar.gz: 84179c85be80e1543a7b53e3c7a1d823086126c99322b7cce5f6916564caedb0b1fdcf6d8d342c4bc5fb626fe5fd5d884db389ec0e935e40c58729ca77324f65
|
data/lib/awesome_xml.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
FILES = %w(
|
4
|
+
native_type
|
5
|
+
node_evaluator
|
6
|
+
node_xpath
|
7
|
+
types/text
|
8
|
+
types/integer
|
9
|
+
types/float
|
10
|
+
types/duration
|
11
|
+
types/date_time
|
12
|
+
type
|
13
|
+
class_methods
|
14
|
+
duration/chunk_parser
|
15
|
+
duration/format
|
16
|
+
duration/format/dynamic_chunk
|
17
|
+
duration/format/static_chunk
|
18
|
+
).freeze
|
19
|
+
|
20
|
+
FILES.each do |file|
|
21
|
+
require File.expand_path("../awesome_xml/#{file}.rb", __FILE__)
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'nokogiri'
|
25
|
+
require 'active_support/time'
|
26
|
+
|
27
|
+
# This module should be included by every class wishing to pose as an `AwesomeXML` node.
|
28
|
+
# It gives access to the methods described in this module and also to class methods in the
|
29
|
+
# `AwesomeXML::ClassMethods` module.
|
30
|
+
module AwesomeXML
|
31
|
+
def self.included(base)
|
32
|
+
base.class_eval do
|
33
|
+
base.extend(AwesomeXML::ClassMethods)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
attr_reader :xml, :parent_node
|
38
|
+
private :xml
|
39
|
+
|
40
|
+
# Pass in a string representing valid XML and an options hash to initialize a class that
|
41
|
+
# includes `AwesomeXML`.
|
42
|
+
def initialize(xml = nil, options = {})
|
43
|
+
@xml = xml
|
44
|
+
@parent_node = options[:parent_node]
|
45
|
+
end
|
46
|
+
|
47
|
+
# This methods runs the parsing operations and assigns the parsed values to the corresponding
|
48
|
+
# attribute of each node defined in the class. Returns the class instance.
|
49
|
+
def parse
|
50
|
+
@xml = Nokogiri::XML(xml).remove_namespaces!
|
51
|
+
@xml = xml&.xpath(self.class.context) if self.class.context.present?
|
52
|
+
parse_values
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
# Call this method to the names and parsed values of the non-private nodes of your class in a
|
57
|
+
# hash, structured as they were defined. Goes down the rabbit hole and calls `evaluate` on child
|
58
|
+
# nodes, too.
|
59
|
+
def evaluate
|
60
|
+
parse_values
|
61
|
+
Hash[self.class.public_nodes.map { |node| [node, public_send(node)] }]
|
62
|
+
end
|
63
|
+
alias_method :to_hash, :evaluate
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def parse_values
|
68
|
+
self.class.nodes.each { |node| public_send("parse_#{node}") }
|
69
|
+
end
|
70
|
+
|
71
|
+
def evaluate_nodes(xpath, type_class, options = {}, &block)
|
72
|
+
evaluated_node = AwesomeXML::NodeEvaluator.new(xml, xpath, type_class, options.merge(parent_node: self)).call
|
73
|
+
if block_given?
|
74
|
+
yield(evaluated_node, self)
|
75
|
+
else
|
76
|
+
evaluated_node
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_xml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felix Lublasser
|
@@ -106,6 +106,7 @@ files:
|
|
106
106
|
- LICENSE
|
107
107
|
- README.md
|
108
108
|
- Rakefile
|
109
|
+
- lib/awesome_xml.rb
|
109
110
|
- lib/awesome_xml/class_methods.rb
|
110
111
|
- lib/awesome_xml/duration/chunk_parser.rb
|
111
112
|
- lib/awesome_xml/duration/format.rb
|