saxomattic 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
19
+ .ruby-version
20
+ .ruby-gemset
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in saxomattr.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brandon Dewitt
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # Saxomattic
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'saxomattic'
8
+
9
+ Or install it yourself as:
10
+
11
+ $ gem install saxomattic
12
+
13
+ ## Usage
14
+
15
+ Saxomattic is a combination of [sax-machine](https://github.com/pauldix/sax-machine) and [active_attr](https://github.com/cgriego/active_attr)
16
+ If you want to know more about those probjects, check them out!
17
+
18
+ Saxomattic allows for a declarative syntax to define "models" that are mappable to xml documents so xml parsing
19
+ is declarative (through sax-machine semantics) and typecasting/attribute presence/default values are provided by
20
+ active_attr. So.......how about an example? (let's walk through the spec example)
21
+
22
+ ```ruby
23
+ class SaxTesterEmbedception
24
+ include Saxomattic
25
+
26
+ attribute :type, :attribute => true
27
+ attribute :value, :value => true
28
+ attribute :not_even_used
29
+ end
30
+
31
+ class SaxTesterEmbedded
32
+ include Saxomattic
33
+
34
+ attribute :embed
35
+ attribute :foo, :type => Integer
36
+ attribute :embedception, :class => SaxTesterEmbedception
37
+ end
38
+
39
+ class SaxTesterSomething
40
+ include Saxomattic
41
+
42
+ attribute :baz
43
+ attribute :foo, :type => Integer
44
+ attribute :iso_8701, :type => Date
45
+ attribute :date, :type => Date
46
+ attribute :datetime, :type => DateTime
47
+ attribute :embedded, :elements => true, :class => SaxTesterEmbedded
48
+ end
49
+
50
+ xml = <<-XML
51
+ <test>
52
+ <baz>baz</baz>
53
+ <iso_8701>2013-01-13</iso_8701>
54
+ <datetime>2013-08-13T14:45:55-06:00</datetime>
55
+ <embedded>
56
+ <foo>2</foo>
57
+ <embed>HERE!</embed>
58
+ <embedception type="TYPE">VALUE</embedception>
59
+ </embedded>
60
+ <date>2013-08-13</date>
61
+ <foo>2</foo>
62
+ </test>
63
+ XML
64
+ ```
65
+
66
+ The classes above `SaxTesterEmbedception`, `SaxTesterEmbedded`, `SaxTesterSomething` are the data models that map to the xml assigned to variable `xml`.
67
+ Instead of the `sax-machine` `element` syntax we have standardized on using `attribute` to declare the attributes and how a model maps to the xml document. Any override of the attribute that would typically be handled in `sax-machine` by using `elements`, `attribute`, `value` need to be accessed through the hash syntax ... ie `attribute :type, :attribute => true` tells active_attr that the attribute `type` should have setters/getters and tells `sax-machine` that the value should come from the attribute on the xml node `type`.
68
+
69
+ Parsing xml:
70
+
71
+ ```ruby
72
+ something = SaxTesterSomething.parse(xml)
73
+
74
+ something.baz # => baz
75
+ something.foo # => 2
76
+ something.foo_before_type_cast # => "2"
77
+ something.embedded.size # => 1
78
+ something.embedded.first.foo # => 2
79
+ something.embedded.first.embedception.type # => "TYPE"
80
+ something.embedded.first.embedception.value # => "VALUE"
81
+ ```
82
+
83
+ Based on this short example, hopefully you can see that declaring your data models is a great way to make data models based on xml documents easier to work with. And if you don't .... do something else!
84
+
85
+ Enjoy!
86
+
87
+ ## Contributing
88
+
89
+ 1. Fork it
90
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
91
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
92
+ 4. Push to the branch (`git push origin my-new-feature`)
93
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Run specs"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ desc "Run specs (default)"
8
+ task :default => :spec
data/lib/saxomattic.rb ADDED
@@ -0,0 +1,85 @@
1
+ require "saxomattic/version"
2
+ require "sax-machine"
3
+ require "active_attr"
4
+ require "active_support/core_ext/hash/slice"
5
+ require "active_support/core_ext/array/extract_options"
6
+ require "active_support/core_ext/string/conversions"
7
+
8
+ module Saxomattic
9
+ ACTIVE_ATTR_ATTRIBUTES = [
10
+ :default,
11
+ :type
12
+ ].freeze
13
+
14
+ SAX_MACHINE_ATTRIBUTES = [
15
+ :as,
16
+ :class,
17
+ :lazy,
18
+ :value,
19
+ :with
20
+ ].freeze
21
+
22
+ def self.included(klass)
23
+ klass.extend(HookManagementMethods)
24
+ klass.__send__(:include, ::ActiveAttr::Model)
25
+ klass._capture_active_attr_methods(klass)
26
+ klass.__send__(:include, ::SAXMachine)
27
+ klass._capture_sax_machine_methods(klass)
28
+ klass.extend(ClassMethods)
29
+ end
30
+
31
+ module HookManagementMethods
32
+ def _capture_active_attr_methods(klass)
33
+ class << klass
34
+ alias_method :_active_attr_attribute, :attribute
35
+ end
36
+ end
37
+
38
+ def _capture_sax_machine_methods(klass)
39
+ class << klass
40
+ alias_method :_sax_machine_ancestor, :ancestor
41
+ alias_method :_sax_machine_attribute, :attribute
42
+ alias_method :_sax_machine_element, :element
43
+ alias_method :_sax_machine_elements, :elements
44
+ alias_method :_sax_machine_value, :value
45
+ end
46
+ end
47
+ end
48
+
49
+ module ClassMethods
50
+ def attribute(*args)
51
+ options = args.extract_options!
52
+ field = args.first
53
+
54
+ # If you want to setup a default set of elements, you can!
55
+ # :default => [ true, false, false, true ]
56
+ if options[:elements] && !options[:elements].kind_of?(Array)
57
+ options.merge!(:default => [])
58
+ end
59
+
60
+ _active_attr_attribute(field, _active_attr_attributes(options.dup))
61
+
62
+ case
63
+ when options[:ancestor] then
64
+ _sax_machine_ancestor(field, _sax_machine_attributes(options.dup))
65
+ when options[:attribute] then
66
+ _sax_machine_attribute(field, _sax_machine_attributes(options.dup))
67
+ when options[:elements] then
68
+ _sax_machine_elements(field, _sax_machine_attributes(options.dup))
69
+ when options[:value] then
70
+ _sax_machine_value(field, _sax_machine_attributes(options.dup))
71
+ else # Default state is an element
72
+ _sax_machine_element(field, _sax_machine_attributes(options.dup))
73
+ end
74
+ end
75
+
76
+ def _active_attr_attributes(options_hash = {})
77
+ options_hash.slice!(::Saxomattic::ACTIVE_ATTR_ATTRIBUTES)
78
+ end
79
+
80
+ def _sax_machine_attributes(options_hash = {})
81
+ options_hash.slice!(::Saxomattic::SAX_MACHINE_ATTRIBUTES)
82
+ end
83
+ end
84
+
85
+ end
@@ -0,0 +1,3 @@
1
+ module Saxomattic
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'saxomattic/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "saxomattic"
8
+ gem.version = Saxomattic::VERSION
9
+ gem.authors = ["Brandon Dewitt"]
10
+ gem.email = ["brandonsdewitt+saxomattic@gmail.com"]
11
+ gem.description = %q{ A gem to combine all the wonderful that is sax-machine with the magic that is active_attr }
12
+ gem.summary = %q{ By including a module "Saxomattic" you can declare xml structured documents with active_attr goodies }
13
+ gem.homepage = "https://github.com/abrandoned/saxomattic.git"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "active_attr"
21
+ gem.add_dependency "activesupport"
22
+ gem.add_dependency "sax-machine"
23
+
24
+ gem.add_development_dependency "pry"
25
+ gem.add_development_dependency "rake"
26
+ gem.add_development_dependency "rspec"
27
+ gem.add_development_dependency "simplecov"
28
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ class SaxTesterEmbedception
4
+ include Saxomattic
5
+
6
+ attribute :type, :attribute => true
7
+ attribute :value, :value => true
8
+ attribute :not_even_used
9
+ end
10
+
11
+ class SaxTesterEmbedded
12
+ include Saxomattic
13
+
14
+ attribute :embed
15
+ attribute :foo, :type => Integer
16
+ attribute :embedception, :class => SaxTesterEmbedception
17
+ end
18
+
19
+ class SaxTesterSomething
20
+ include Saxomattic
21
+
22
+ attribute :baz
23
+ attribute :foo, :type => Integer
24
+ attribute :iso_8701, :type => Date
25
+ attribute :date, :type => Date
26
+ attribute :datetime, :type => DateTime
27
+ attribute :embedded, :elements => true, :class => SaxTesterEmbedded
28
+ end
29
+
30
+ describe ::Saxomattic do
31
+
32
+ let(:embedded_message) { "HERE!" }
33
+ let(:embedception_type) { "TYPE" }
34
+ let(:embedception_value) { "VALUE" }
35
+ let(:foo) { 2 }
36
+ let(:baz) { "baz" }
37
+ let(:xml) {
38
+ <<-XML
39
+ <test>
40
+ <baz>#{baz}</baz>
41
+ <iso_8701>2013-01-13</iso_8701>
42
+ <datetime>#{DateTime.current}</datetime>
43
+ <embedded>
44
+ <foo>2</foo>
45
+ <embed>#{embedded_message}</embed>
46
+ <embedception type="#{embedception_type}">#{embedception_value}</embedception>
47
+ </embedded>
48
+ <date>#{Date.today}</date>
49
+ <foo>2</foo>
50
+ </test>
51
+ XML
52
+ }
53
+
54
+ subject { SaxTesterSomething.parse(xml) }
55
+
56
+ it "extracts elements when declared as attribute" do
57
+ subject.baz.should eq(baz)
58
+ end
59
+
60
+ context "TypeCasting" do
61
+ it "typecasts integers when declared with type => Integer" do
62
+ subject.foo.should eq(foo)
63
+ end
64
+
65
+ it "typecasts Dates when declared with type => Date" do
66
+ subject.date.should be_a(Date)
67
+ subject.date.should eq(Date.today)
68
+ end
69
+
70
+ it "embedded" do
71
+ subject.embedded.should be_a(Array)
72
+ subject.embedded.first.embed.should eq(embedded_message)
73
+ end
74
+
75
+ it "typecasts embedded fields" do
76
+ subject.embedded.first.foo.should eq(foo)
77
+ end
78
+
79
+ it "embeds values further" do
80
+ subject.embedded.first.embedception?.should be_true
81
+ subject.embedded.first.embedception.type.should eq(embedception_type)
82
+ subject.embedded.first.embedception.value.should eq(embedception_value)
83
+ subject.embedded.first.embedception.not_even_used?.should be_false
84
+ end
85
+ end
86
+
87
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ require 'simplecov'
5
+ SimpleCov.start do
6
+ add_filter '/spec/'
7
+ end
8
+
9
+ Bundler.require(:default, :development, :test)
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: saxomattic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brandon Dewitt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: active_attr
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sax-machine
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: simplecov
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: ! ' A gem to combine all the wonderful that is sax-machine with the magic
127
+ that is active_attr '
128
+ email:
129
+ - brandonsdewitt+saxomattic@gmail.com
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - .gitignore
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - lib/saxomattic.rb
140
+ - lib/saxomattic/version.rb
141
+ - saxomattic.gemspec
142
+ - spec/saxomattic_spec.rb
143
+ - spec/spec_helper.rb
144
+ homepage: https://github.com/abrandoned/saxomattic.git
145
+ licenses: []
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ! '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ segments:
157
+ - 0
158
+ hash: -4328511533199913678
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ none: false
161
+ requirements:
162
+ - - ! '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ segments:
166
+ - 0
167
+ hash: -4328511533199913678
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 1.8.24
171
+ signing_key:
172
+ specification_version: 3
173
+ summary: By including a module "Saxomattic" you can declare xml structured documents
174
+ with active_attr goodies
175
+ test_files:
176
+ - spec/saxomattic_spec.rb
177
+ - spec/spec_helper.rb