jordi-xml_struct 0.2.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/README.rdoc +152 -0
  2. data/WHATSNEW +10 -0
  3. data/lib/xml_struct/adapters/hpricot.rb +47 -0
  4. data/lib/xml_struct/adapters/rexml.rb +34 -0
  5. data/lib/xml_struct/array_notation.rb +44 -0
  6. data/lib/xml_struct/blankish_slate.rb +1 -1
  7. data/lib/xml_struct/collection_proxy.rb +7 -3
  8. data/lib/xml_struct/default_adapter.rb +15 -0
  9. data/lib/xml_struct/method_missing_dispatchers.rb +43 -0
  10. data/lib/xml_struct/string.rb +6 -2
  11. data/lib/xml_struct.rb +31 -52
  12. data/xml_struct.gemspec +38 -0
  13. metadata +17 -49
  14. data/README.markdown +0 -151
  15. data/Rakefile +0 -36
  16. data/lib/xml_struct/common_behaviours.rb +0 -53
  17. data/test/samples/lorem.xml +0 -63
  18. data/test/samples/recipe.xml +0 -16
  19. data/test/samples/weird_characters.xml +0 -2
  20. data/test/test_helper.rb +0 -39
  21. data/test/vendor/test-spec/README +0 -378
  22. data/test/vendor/test-spec/ROADMAP +0 -1
  23. data/test/vendor/test-spec/Rakefile +0 -146
  24. data/test/vendor/test-spec/SPECS +0 -161
  25. data/test/vendor/test-spec/TODO +0 -2
  26. data/test/vendor/test-spec/bin/specrb +0 -107
  27. data/test/vendor/test-spec/examples/stack.rb +0 -38
  28. data/test/vendor/test-spec/examples/stack_spec.rb +0 -119
  29. data/test/vendor/test-spec/lib/test/spec/dox.rb +0 -148
  30. data/test/vendor/test-spec/lib/test/spec/rdox.rb +0 -25
  31. data/test/vendor/test-spec/lib/test/spec/should-output.rb +0 -49
  32. data/test/vendor/test-spec/lib/test/spec/version.rb +0 -8
  33. data/test/vendor/test-spec/lib/test/spec.rb +0 -660
  34. data/test/vendor/test-spec/test/spec_dox.rb +0 -39
  35. data/test/vendor/test-spec/test/spec_flexmock.rb +0 -209
  36. data/test/vendor/test-spec/test/spec_mocha.rb +0 -104
  37. data/test/vendor/test-spec/test/spec_nestedcontexts.rb +0 -26
  38. data/test/vendor/test-spec/test/spec_new_style.rb +0 -80
  39. data/test/vendor/test-spec/test/spec_should-output.rb +0 -26
  40. data/test/vendor/test-spec/test/spec_testspec.rb +0 -699
  41. data/test/vendor/test-spec/test/spec_testspec_order.rb +0 -26
  42. data/test/vendor/test-spec/test/test_testunit.rb +0 -22
  43. data/test/xml_struct_test.rb +0 -185
@@ -0,0 +1,38 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'xml_struct'
3
+ s.version = '0.9.0'
4
+ s.date = '2008-10-13'
5
+
6
+ s.author = 'Jordi Bunster'
7
+ s.email = 'jordi@bunster.org'
8
+ s.homepage = 'http://github.com/jordi/xml_struct'
9
+
10
+ s.add_dependency 'activesupport'
11
+
12
+ s.summary = "The Rubyista's way to do quick XML sit-ups"
13
+ s.description = %{ This is a library for reading (not writing) XML. It is
14
+ particularly suited for cases where one is dealing with
15
+ small documents of a known structure.
16
+
17
+ It is slow and not devoid of caveats, but has a very
18
+ pleasant, Ruby-like syntax. }.strip!.gsub! /\s+/, ' '
19
+
20
+ s.files = %w[ MIT-LICENSE
21
+ README.rdoc
22
+ TODO
23
+ WHATSNEW
24
+ lib
25
+ lib/jordi-xml_struct.rb
26
+ lib/xml_struct
27
+ lib/xml_struct/adapters
28
+ lib/xml_struct/adapters/hpricot.rb
29
+ lib/xml_struct/adapters/rexml.rb
30
+ lib/xml_struct/array_notation.rb
31
+ lib/xml_struct/blankish_slate.rb
32
+ lib/xml_struct/collection_proxy.rb
33
+ lib/xml_struct/default_adapter.rb
34
+ lib/xml_struct/method_missing_dispatchers.rb
35
+ lib/xml_struct/string.rb
36
+ lib/xml_struct.rb
37
+ xml_struct.gemspec ]
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jordi-xml_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordi Bunster
@@ -19,7 +19,7 @@ dependencies:
19
19
  requirements:
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 2.1.1
22
+ version: "0"
23
23
  version:
24
24
  description: This is a library for reading (not writing) XML. It is particularly suited for cases where one is dealing with small documents of a known structure. It is slow and not devoid of caveats, but has a very pleasant, Ruby-like syntax.
25
25
  email: jordi@bunster.org
@@ -27,28 +27,32 @@ executables: []
27
27
 
28
28
  extensions: []
29
29
 
30
- extra_rdoc_files:
31
- - README.markdown
30
+ extra_rdoc_files: []
31
+
32
32
  files:
33
33
  - MIT-LICENSE
34
- - README.markdown
35
- - Rakefile
34
+ - README.rdoc
36
35
  - TODO
37
36
  - WHATSNEW
38
37
  - lib
39
38
  - lib/jordi-xml_struct.rb
40
39
  - lib/xml_struct
40
+ - lib/xml_struct/adapters
41
+ - lib/xml_struct/adapters/hpricot.rb
42
+ - lib/xml_struct/adapters/rexml.rb
43
+ - lib/xml_struct/array_notation.rb
41
44
  - lib/xml_struct/blankish_slate.rb
42
45
  - lib/xml_struct/collection_proxy.rb
43
- - lib/xml_struct/common_behaviours.rb
46
+ - lib/xml_struct/default_adapter.rb
47
+ - lib/xml_struct/method_missing_dispatchers.rb
44
48
  - lib/xml_struct/string.rb
45
49
  - lib/xml_struct.rb
46
- has_rdoc: true
50
+ - xml_struct.gemspec
51
+ has_rdoc: false
47
52
  homepage: http://github.com/jordi/xml_struct
48
53
  post_install_message:
49
- rdoc_options:
50
- - --main
51
- - README.markdown
54
+ rdoc_options: []
55
+
52
56
  require_paths:
53
57
  - lib
54
58
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -70,41 +74,5 @@ rubygems_version: 1.2.0
70
74
  signing_key:
71
75
  specification_version: 2
72
76
  summary: The Rubyista's way to do quick XML sit-ups
73
- test_files:
74
- - test
75
- - test/samples
76
- - test/samples/lorem.xml
77
- - test/samples/recipe.xml
78
- - test/samples/weird_characters.xml
79
- - test/test_helper.rb
80
- - test/vendor
81
- - test/vendor/test-spec
82
- - test/vendor/test-spec/bin
83
- - test/vendor/test-spec/bin/specrb
84
- - test/vendor/test-spec/examples
85
- - test/vendor/test-spec/examples/stack.rb
86
- - test/vendor/test-spec/examples/stack_spec.rb
87
- - test/vendor/test-spec/lib
88
- - test/vendor/test-spec/lib/test
89
- - test/vendor/test-spec/lib/test/spec
90
- - test/vendor/test-spec/lib/test/spec/dox.rb
91
- - test/vendor/test-spec/lib/test/spec/rdox.rb
92
- - test/vendor/test-spec/lib/test/spec/should-output.rb
93
- - test/vendor/test-spec/lib/test/spec/version.rb
94
- - test/vendor/test-spec/lib/test/spec.rb
95
- - test/vendor/test-spec/Rakefile
96
- - test/vendor/test-spec/README
97
- - test/vendor/test-spec/ROADMAP
98
- - test/vendor/test-spec/SPECS
99
- - test/vendor/test-spec/test
100
- - test/vendor/test-spec/test/spec_dox.rb
101
- - test/vendor/test-spec/test/spec_flexmock.rb
102
- - test/vendor/test-spec/test/spec_mocha.rb
103
- - test/vendor/test-spec/test/spec_nestedcontexts.rb
104
- - test/vendor/test-spec/test/spec_new_style.rb
105
- - test/vendor/test-spec/test/spec_should-output.rb
106
- - test/vendor/test-spec/test/spec_testspec.rb
107
- - test/vendor/test-spec/test/spec_testspec_order.rb
108
- - test/vendor/test-spec/test/test_testunit.rb
109
- - test/vendor/test-spec/TODO
110
- - test/xml_struct_test.rb
77
+ test_files: []
78
+
data/README.markdown DELETED
@@ -1,151 +0,0 @@
1
- # XML Struct
2
-
3
- (This is inspired by Python's `xml_objectify`)
4
-
5
- XML Struct attempts to make accessing small, well-formed XML structures
6
- convenient, by using dot notation (`foo.bar`) to represent both attributes
7
- and child elements whenever possible.
8
-
9
- XML parsing libraries (in general) have interfaces that are useful when
10
- one is using XML for its intended purpose, but cumbersome when one always
11
- sends the same XML structure, and always process all of it in the same
12
- way. This one aims to be a bit different.
13
-
14
- ## Example usage
15
-
16
- <recipe name="bread" prep_time="5 mins" cook_time="3 hours">
17
- <title>Basic bread</title>
18
- <ingredient amount="8" unit="dL">Flour</ingredient>
19
- <ingredient amount="10" unit="grams">Yeast</ingredient>
20
- <ingredient amount="4" unit="dL" state="warm">Water</ingredient>
21
- <ingredient amount="1" unit="teaspoon">Salt</ingredient>
22
- <instructions easy="yes" hard="false">
23
- <step>Mix all ingredients together.</step>
24
- <step>Knead thoroughly.</step>
25
- <step>Cover with a cloth, and leave for one hour in warm room.</step>
26
- <step>Knead again.</step>
27
- <step>Place in a bread baking tin.</step>
28
- <step>Cover with a cloth, and leave for one hour in warm room.</step>
29
- <step>Bake in the oven at 180(degrees)C for 30 minutes.</step>
30
- </instructions>
31
- </recipe>
32
-
33
- require 'xml_struct'
34
- recipe = XMLStruct.new io_with_recipe_xml_shown_above
35
-
36
- recipe.name => "bread"
37
- recipe.title => "Basic bread"
38
-
39
- recipe.ingredients.is_a?(Array) => true
40
- recipe.ingredients.first.amount => "8" # Not a Fixnum. Too hard. :(
41
-
42
- recipe.instructions.easy? => true
43
-
44
- recipe.instructions.first.upcase => "MIX ALL INGREDIENTS TOGETHER."
45
- recipe.instructions.steps.size => 7
46
-
47
- ## Installation instructions
48
-
49
- sudo gem install jordi-xml_struct --source http://gems.github.com
50
-
51
- ## Motivation
52
-
53
- XML is an **extensible** markup language. It is extensible because it is
54
- meant to define markup languages for **any** type of document, so new tags
55
- are needed depending on the problem domain.
56
-
57
- Sometimes, however, XML ends up being used to solve a much simpler problem:
58
- the issue of passing a data-structure over the network, and/or between two
59
- different languages. Tools like JSON or YAML are a much better fit for
60
- this kind of job, but one doesn't always have that luxury.
61
-
62
- ## Caveats
63
-
64
- The dot notation is used as follows. For the given file:
65
-
66
- <outer id="root" name="foo">
67
- <name>Outer Element</name>
68
- </outer>
69
-
70
- `outer.name` is the `<name>` element. Child elements are always looked up
71
- first, then attributes. To access the attribute in the case of ambiguity,
72
- use `outer[:attr => 'name']`.
73
-
74
- `outer.id` is really `Object#id`, because all of the object methods are
75
- preserved (this is on purpose). To access the attribute `id`, use
76
- `outer[:attr => 'id']`, or `outer['id']` since there's no element/attribute
77
- ambiguity.
78
-
79
- ## Features & Problems
80
-
81
- ### Collection auto-folding
82
-
83
- Similar to XML::Simple, XML Struct folds same named elements at the same
84
- level. For example:
85
-
86
- <student>
87
- <name>Bob</name>
88
- <course>Math</course>
89
- <course>Biology</course>
90
- </student>
91
-
92
- student = XMLStruct.new(xml_file)
93
-
94
- student.course.is_a? Array => true
95
- student.course.first == 'Math' => true
96
- student.course.last == 'Biology => true
97
-
98
- ### Collection pluralization:
99
-
100
- With the same file from the `Collection auto-folding` section above, you
101
- also get this (courtesy of `ActiveSupport`'s `Inflector`):
102
-
103
- student.courses.first == student.course.first => true
104
-
105
- ### Collection proxy
106
-
107
- Sometimes, collections are expressed with a container element in XML:
108
-
109
- <student>
110
- <name>Bob</name>
111
- <courses>
112
- <course>Math</course>
113
- <course>Biology</course>
114
- </courses>
115
- </student>
116
-
117
- In this case, since the container element `courses` has no text element
118
- of its own, and it only has elements of one name under it, it delegates
119
- all methods it doesn't contain to the collection below, so you get:
120
-
121
- student.courses.collect { |c| c.downcase.to_sym } => [:math, :biology]
122
-
123
- ### Question mark notation
124
-
125
- Strings that look like booleans are "booleanized" if called by their
126
- question mark names (such as `enabled?`)
127
-
128
- ### Slow
129
-
130
- So far, XML Struct uses REXML to do the actual XML parsing. Support
131
- for using faster parsers is planned, leaving REXML as a fallback option.
132
-
133
- ### Recursive
134
-
135
- The entire XML file is parsed using REXML right now, and then converted to
136
- an XMLStruct of XMLStruct objects recursively. Deep files are bound to
137
- throw `SystemStackError`, but for the kinds of files I need to read, things
138
- are working fine so far. In any case, stream parsing is on the TODO list.
139
-
140
- ### Incomplete
141
-
142
- It most likely doesn't work with a ton of features of complex XML files. I
143
- will always try to accomodate those, as long as they don't make the basic
144
- usage more complex. As usual, patches welcome.
145
-
146
- ## Legal
147
-
148
- Copyright (c) 2008 Jordi Bunster, released under the MIT license
149
-
150
-
151
-
data/Rakefile DELETED
@@ -1,36 +0,0 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
-
5
- desc 'Default: run unit tests.'
6
- task :default => :test
7
-
8
- desc 'Run the unit tests.'
9
- Rake::TestTask.new(:test) do |t|
10
- t.libs << 'lib'
11
- t.pattern = 'test/**/*_test.rb'
12
- t.verbose = true
13
- end
14
-
15
- desc 'Generate documentation'
16
- Rake::RDocTask.new(:rdoc) do |rdoc|
17
- rdoc.rdoc_dir = 'rdoc'
18
- rdoc.title = 'XMLStruct'
19
- rdoc.options << '--line-numbers' << '--inline-source'
20
- rdoc.rdoc_files.include 'README.markdown'
21
- rdoc.rdoc_files.include 'lib/**/*.rb'
22
- end
23
-
24
- desc 'Measures test coverage using rcov'
25
- task :rcov do
26
- rm_f 'coverage'
27
- rm_f 'coverage.data'
28
-
29
- rcov = %{ rcov --aggregate coverage.data --text-summary
30
- --include lib
31
- --exclude /Library/Ruby
32
- }.strip!.gsub! /\s+/, ' '
33
-
34
- system("#{rcov} --html #{Dir.glob('test/**/*_test.rb').join(' ')}")
35
- system('open coverage/index.html') if PLATFORM['darwin']
36
- end
@@ -1,53 +0,0 @@
1
- module XMLStruct::CommonBehaviours
2
-
3
- # Detect an existing method being called in question form:
4
- def __question_answer(method, *args, &block)
5
- if method.to_s.match(/\?$/) && args.empty? && block.nil?
6
- boolish = __send__(method.to_s.chomp('?').to_sym).to_s
7
-
8
- return true if %w[ true yes t y ].include? boolish.downcase
9
- return false if %w[ false no f n ].include? boolish.downcase
10
- end
11
- end
12
-
13
- # Array-notation access to elements and attributes. It comes handy when
14
- # the element or attribute you need to reach is not reachable via dot
15
- # notation (because it's not a valid method name, or because the method
16
- # exists, such as 'id' or 'class').
17
- #
18
- # It also supports hash keys, which are useful to reach attributes named
19
- # the same as elements in the same level (which otherwise go first)
20
- #
21
- # All of this is a lot easier to exampling by example:
22
- #
23
- # <article id="main_article" author="j-random">
24
- # <author>J. Random Hacker</author>
25
- # </article>
26
- #
27
- # article.id => 9314390 # Object#id gets called
28
- # article[:id] => "main_article" # id attribute
29
- # article[:author] => <XMLStruct ...> # <author> element
30
- # article[:attr => 'author'] => "j-random" # author attribute
31
- #
32
- # Valid keys for the hash notation in the example above are :attr,
33
- # :attribute, :child, and :element.
34
- def [](name)
35
- return @__target[name] if @__target && name.is_a?(Numeric)
36
-
37
- unless name.is_a? Hash
38
- return @__children[name.to_sym] if @__children[name.to_sym]
39
- return @__attributes[name.to_sym] if @__attributes[name.to_sym]
40
- end
41
-
42
- raise 'one and only one key allowed' if name.size != 1
43
-
44
- case (param = name.keys.first.to_sym)
45
- when :element : @__children[name.values.first.to_sym]
46
- when :child : @__children[name.values.first.to_sym]
47
- when :attr : @__attributes[name.values.first.to_sym]
48
- when :attribute : @__attributes[name.values.first.to_sym]
49
- else raise %{ Invalid key :#{param.to_s}.
50
- Use one of :element, :child, :attr, or :attribute }.squish!
51
- end
52
- end
53
- end
@@ -1,63 +0,0 @@
1
- <!-- Invalid method name test -->
2
- <lorem id="root" name="ipsum dolor sit amet" _tempor="incididunt">
3
-
4
- <_minim>veniam</_minim> <!-- Invalid method name test -->
5
-
6
- <!-- unambiguous element/attribute access test -->
7
- <sed do="eiusmod attributus">
8
- <do price="$8">eiusmod elementus</do>
9
- </sed>
10
-
11
- <!-- Blank test -->
12
- <ipsum></ipsum>
13
- <dolor foo="bar"></dolor>
14
- <sit><amet></amet></sit>
15
- <ut>quis</ut>
16
-
17
- <!-- To test the array folding feature, as well as the pluralization.
18
- The attributes help test float, integer, and boolean recognition,
19
- as well as method name clashing behaviour (id) -->
20
- <consectetur id="1" capacity="2.13" enabled="yes" />
21
- <consectetur id="2" capacity="-1.9" enabled="No" />
22
- <consectetur id="3" capacity="13.0" enabled="True" />
23
- <consectetur id="4" capacity="0.00" enabled="false" />
24
-
25
- <!-- Test boolean recognition -->
26
- <brutus>true</brutus>
27
-
28
- <consecteturs> <!-- To test that we can still reach this, even though
29
- the array of singular ones above will trap the call
30
- to this when using dot notation -->
31
- <culpa>officia</culpa>
32
- </consecteturs>
33
-
34
- <cupidatats> <!-- Test for the empty element == single array child
35
- scenario, as well as date recognition -->
36
- <cupidatat activated_at="2903-02-12">Aliqua</cupidatat>
37
- <cupidatat activated_at="2903-02-13">Occaecat</cupidatat>
38
- <cupidatat activated_at="2904-10-18">Cupidatat</cupidatat>
39
- <cupidatat activated_at="2905-03-21">Mollit</cupidatat>
40
- </cupidatats>
41
-
42
- <!-- Non-empty element, should not be array proxy -->
43
- <voluptate>
44
- velit
45
- <esse>cillum</esse>
46
- <esse>cillum dolore</esse>
47
- </voluptate>
48
-
49
- <!-- Test for both text value and CDATA value -->
50
- <ullamco>Laboris<![CDATA[nisi]]></ullamco>
51
-
52
- <!-- Test for multiple CDATA values -->
53
- <deserunt><![CDATA[mollit]]><![CDATA[anim]]></deserunt>
54
-
55
- <!-- Test for whitespace preservation / squishing -->
56
- <irure metadata="
57
- dolor ">
58
- reprehenderit </irure>
59
-
60
- <!-- CDATA whitespace preservation test -->
61
- <![CDATA[ foo
62
- ]]>
63
- </lorem>