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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/awesome_xml.rb +79 -0
  3. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e0fdacdca25fa723e63830f68a8b2c250c4b1df
4
- data.tar.gz: f49a9d5503da9076d8608e98168267ebf572a304
3
+ metadata.gz: c56c2831ab0626cb308dc5dea679f2f854077101
4
+ data.tar.gz: f25a4b311f2cad636f5deba9797cfd48fb014e8d
5
5
  SHA512:
6
- metadata.gz: 1acd280ece6c36b3aa553e5b12901a8df5bf566363a7c4b0854bf285d23c0b817b74800c57255b885c4742b68b473fa3c53afa6ca78d815f9aef57ee734ac162
7
- data.tar.gz: bf932106ae5e58ff57922688405c50074fd6e7640b3fa1f8ccc1066ad9abd2cc1e0fac1b17aa93affdabd483763ade2b0dd3248d76ace5d7308d8b7b05379bc7
6
+ metadata.gz: 27448af39a7950d1c96a3e1d814037acaeb63dc08ac2cd2d2034889899feefacd3fc1702ffffdbdd6d409262a7034dbc531ea5b7b0e393f75db57eaf70997d85
7
+ data.tar.gz: 84179c85be80e1543a7b53e3c7a1d823086126c99322b7cce5f6916564caedb0b1fdcf6d8d342c4bc5fb626fe5fd5d884db389ec0e935e40c58729ca77324f65
@@ -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.0.0
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