yamo 0.1.0

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,23 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+ vendor/gems
21
+ vendor/bin
22
+
23
+ ## PROJECT::SPECIFIC
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ gem "jeweler"
2
+ gem "kwalify"
3
+ gem "rake"
4
+ gem "yard"
5
+
6
+ bin_path "vendor/bin"
7
+ disable_system_gems
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Kenneth Vestergaard Schmidt
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = yamo
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2009 Kenneth Vestergaard Schmidt. See LICENSE for details.
@@ -0,0 +1,53 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ gem.name = "yamo"
7
+ gem.summary = %Q{YAML Model Objects}
8
+ gem.description = %Q{Classes for loading schema-validated YAML, and creating nice Ruby-objects with accessors}
9
+ gem.email = "kvs@binarysolutions.dk"
10
+ gem.homepage = "http://github.com/kvs/yamo"
11
+ gem.authors = ["Kenneth Vestergaard Schmidt"]
12
+ gem.add_development_dependency "yard", ">= 0"
13
+ gem.add_dependency('kwalify', '>=0.7.1')
14
+ gem.required_ruby_version = '>= 1.9.1'
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ begin
47
+ require 'yard'
48
+ YARD::Rake::YardocTask.new
49
+ rescue LoadError
50
+ task :yardoc do
51
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
52
+ end
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,6 @@
1
+ # -*- mode: ruby; tab-width: 2; indent-tabs-mode: nil; -*-
2
+
3
+ module Yamo; end
4
+
5
+ require 'yamo/collection'
6
+ require 'yamo/singleton'
@@ -0,0 +1,41 @@
1
+ # -*- mode: ruby; tab-width: 2; indent-tabs-mode: nil; -*-
2
+
3
+ require 'yamo/object'
4
+ require 'singleton'
5
+
6
+ module Yamo; end
7
+
8
+ class Yamo::Collection < Yamo::Object
9
+ extend Enumerable
10
+
11
+ attr_reader :name
12
+
13
+ def initialize(name, data)
14
+ @name = name
15
+ @data = data
16
+ end
17
+
18
+ def self.objectdir(objectdir)
19
+ @objectdir = objectdir
20
+ end
21
+
22
+ def self.get(name)
23
+ begin
24
+ self.new(name, load_and_validate_file("#{@objectdir}/#{name}"))
25
+ rescue Errno::ENOENT
26
+ nil
27
+ end
28
+ end
29
+
30
+ def self.each
31
+ Dir.foreach(@objectdir) do |o|
32
+ next if o.match(/^\./)
33
+ yield get(o)
34
+ end
35
+ end
36
+
37
+ # Include name in +to_s+
38
+ def to_s
39
+ return '#<' + self.class.to_s + ":" + self.name + '>'
40
+ end
41
+ end
@@ -0,0 +1,49 @@
1
+ # -*- mode: ruby; tab-width: 2; indent-tabs-mode: nil; -*-
2
+
3
+ require 'yamo/validator'
4
+
5
+ module Yamo; end
6
+
7
+ # Yamo::Object is a base-class for a data-object, externally stored in YAML,
8
+ # with a Kwalify-schema and optional validation-hooks.
9
+ class Yamo::Object
10
+ attr_reader :data
11
+
12
+ def self.schema(schema)
13
+ @validator = Yamo::Validator.new(schema)
14
+ @validator.hook = self
15
+
16
+ create_accessors(@validator.schema["mapping"])
17
+ end
18
+
19
+ def self.validator
20
+ @validator
21
+ end
22
+
23
+ def self.create_accessors(schema, obj=nil)
24
+ # Generate instance accessor methods
25
+ schema.each_pair do |key, value|
26
+ # We either generate methods on the Class instance of this object, or on a simple instance-object.
27
+ receiver = (obj.nil?) ? self : (class << obj ; self; end)
28
+ receiver.send :define_method, key.gsub('-', '_') do
29
+ retval = (obj ? obj[key] : @data[key]) || value["default"]
30
+ if value["type"] == "map"
31
+ retval = retval.nil? ? Hash.new : retval.dup
32
+ Yamo::Object.create_accessors(value["mapping"], retval)
33
+ end
34
+ retval
35
+ end
36
+ end
37
+ end
38
+
39
+ # Load and validate +file+. Source is either eval'd or YAML, depending on +yaml+
40
+ def self.load_and_validate_file(file, yaml=false); load_and_validate_source(file, yaml, true); end
41
+
42
+ # Validate +data+. Source is either eval'd or YAML, depending on +yaml+
43
+ def self.validate_data(data, yaml=false); load_and_validate_source(data, yaml, false); end
44
+
45
+ # Validate +src+, interpreting it as a file if +file+ is true, or as a String to eval if +file+ is false.
46
+ def self.load_and_validate_source(src, yaml, file)
47
+ @validator.load_and_validate_source(src, yaml, file)
48
+ end
49
+ end
@@ -0,0 +1,19 @@
1
+ # -*- mode: ruby; tab-width: 2; indent-tabs-mode: nil; -*-
2
+
3
+ require 'yamo/object'
4
+ require 'singleton'
5
+
6
+ module Yamo; end
7
+
8
+ class Yamo::Singleton < Yamo::Object
9
+ include Singleton
10
+
11
+ def self.object(file)
12
+ @@data = self.load_and_validate_file(file)
13
+ @object = file
14
+ end
15
+
16
+ def initialize
17
+ @data = @@data
18
+ end
19
+ end
@@ -0,0 +1,69 @@
1
+ # -*- mode: ruby; tab-width: 2; indent-tabs-mode: nil; -*-
2
+
3
+ require 'kwalify'
4
+ require 'shellwords'
5
+
6
+ module Yamo; end
7
+
8
+ class Yamo::ValidateError < Exception; end
9
+
10
+ class Yamo::Validator
11
+ attr_reader :schema
12
+ attr_reader :hook
13
+
14
+ def initialize(schema)
15
+ @hook = nil
16
+
17
+ metavalidator = Kwalify::MetaValidator.instance
18
+ parser = Kwalify::Yaml::Parser.new(metavalidator)
19
+ @schema = parser.parse_file(schema)
20
+
21
+ if parser.errors and !parser.errors.empty?
22
+ raise Yamo::ValidateError, parser.errors
23
+ end
24
+
25
+ @validator = Kwalify::Validator.new(@schema)
26
+ end
27
+
28
+ def hook=(klass)
29
+ @hook = klass
30
+
31
+ @validator.define_singleton_method :validate_hook do |v,r,p,e|
32
+ klass.validate_hook(v,r,p,e) if klass.respond_to? :validate_hook
33
+ end
34
+ end
35
+
36
+ def load_and_validate_source(src, yaml, file)
37
+ errors = nil
38
+ doc = nil
39
+
40
+ if yaml
41
+ parser = Kwalify::Yaml::Parser.new(validator)
42
+ if file
43
+ doc = parser.parse_file(src)
44
+ else
45
+ doc = parser.parse(src)
46
+ end
47
+ errors = parser.errors()
48
+ else
49
+ if file
50
+ doc = eval(IO.readlines(src).join(""))
51
+ else
52
+ doc = eval(src)
53
+ end
54
+
55
+ if doc.class != Hash
56
+ raise Yamo::ValidateError, "data did not produce a Hash"
57
+ end
58
+
59
+ doc = if @hook.respond_to?(:pre_validate) then @hook.pre_validate(doc) else doc end
60
+ errors = @validator.validate(doc)
61
+ end
62
+
63
+ if errors and !errors.empty?
64
+ raise Yamo::ValidateError, "\n#{file ? src : 'input'} did not validate:\n" + errors.join("\n")
65
+ end
66
+
67
+ if @hook.respond_to?(:post_validate) then @hook.post_validate(doc) else doc end
68
+ end
69
+ end
@@ -0,0 +1,5 @@
1
+ {
2
+ 'a' => 'a',
3
+ 'c' => 'b',
4
+ 'd' => 'Foo',
5
+ };
@@ -0,0 +1,9 @@
1
+ {
2
+ 'a' => 'a',
3
+ 'b' => {
4
+ 'a' => [ 'a', 'b', 'c' ],
5
+ 'b' => 'a',
6
+ },
7
+ 'c' => 'b',
8
+ 'd' => 'Bar',
9
+ };
@@ -0,0 +1,5 @@
1
+ {
2
+ 'a' => 'a',
3
+ 'c' => 'b',
4
+ 'd' => 'Foo',
5
+ };
@@ -0,0 +1,8 @@
1
+ {
2
+ 'a' => 'a',
3
+ 'b' => {
4
+ 'a' => [ 'a', 'b', 'c' ],
5
+ 'b' => 'a',
6
+ },
7
+ 'c' => 'b',
8
+ };
@@ -0,0 +1,34 @@
1
+ ---
2
+ class: Collection
3
+ type: map
4
+ mapping:
5
+ "a":
6
+ type: str
7
+ enum:
8
+ - a
9
+ - b
10
+ default: "a"
11
+ "b":
12
+ type: map
13
+ mapping:
14
+ "a":
15
+ type: seq
16
+ required: no
17
+ sequence:
18
+ - { type: str, unique: yes }
19
+ "b":
20
+ type: any
21
+ default: true
22
+ "c":
23
+ type: seq
24
+ required: no
25
+ sequence:
26
+ - { type: str, unique: yes }
27
+ "c":
28
+ type: str
29
+ pattern: /^(a|b|c)$/
30
+ required: no
31
+ "d":
32
+ type: str
33
+ name: HookOne
34
+ required: yes
@@ -0,0 +1,33 @@
1
+ ---
2
+ class: Collection
3
+ mapping:
4
+ "a":
5
+ type: str
6
+ enum:
7
+ - a
8
+ - b
9
+ default: "a"
10
+ "b":
11
+ type: map
12
+ mapping:
13
+ "a":
14
+ type: seq
15
+ required: no
16
+ sequence:
17
+ - { type: str, unique: yes }
18
+ "b":
19
+ type: any
20
+ default: true
21
+ "c":
22
+ type: seq
23
+ required: no
24
+ sequence:
25
+ - { type: str, unique: yes }
26
+ "c":
27
+ type: str
28
+ pattern: /^(a|b|c)$/
29
+ required: no
30
+ "d":
31
+ type: str
32
+ name: HookOne
33
+ required: yes
@@ -0,0 +1,18 @@
1
+ # -*- mode: yaml; tab-width: 2; indent-tabs-mode: nil; -*-
2
+ ---
3
+ class: Config
4
+ type: map
5
+ mapping:
6
+ "a":
7
+ type: map
8
+ mapping:
9
+ "b": { type: int, required: yes }
10
+ "c": { type: int, required: yes }
11
+ "d": { type: int, required: yes }
12
+ "e": { type: int, required: yes }
13
+ "f":
14
+ type: seq
15
+ required: no
16
+ sequence:
17
+ - type: str
18
+ unique: yes
@@ -0,0 +1,12 @@
1
+ {
2
+ 'a' => {
3
+ 'b' => 8,
4
+ 'c' => 6,
5
+ 'd' => 4,
6
+ 'e' => 3,
7
+ 'f' => [ 'a',
8
+ 'b',
9
+ 'c',
10
+ ],
11
+ },
12
+ };
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ TESTDIR = File.dirname(__FILE__)
5
+ $LOAD_PATH.unshift(TESTDIR)
6
+ $LOAD_PATH.unshift(File.join(TESTDIR, '..', 'lib'))
7
+
8
+ require 'yamo'
9
+
10
+ class CollectionTest < Yamo::Collection
11
+ schema "#{TESTDIR}/data/schemas/collection.yaml"
12
+ objectdir "#{TESTDIR}/data/collection"
13
+ end
14
+
15
+ class SingletonTest < Yamo::Singleton
16
+ schema "#{TESTDIR}/data/schemas/singleton.yaml"
17
+ object "#{TESTDIR}/data/singleton.conf"
18
+ end
@@ -0,0 +1,11 @@
1
+ require 'helper'
2
+
3
+ class TestCollection < Test::Unit::TestCase
4
+ def test_load
5
+ assert_equal(2, CollectionTest.entries.count)
6
+ assert_equal('obj1', CollectionTest.get("obj1").name)
7
+ assert_equal(1, CollectionTest.find_all { |c| c.d == "Bar" }.count)
8
+ obj = CollectionTest.entries.first
9
+ assert_equal(true, obj.b.b)
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ require 'helper'
2
+
3
+ class TestSingleton < Test::Unit::TestCase
4
+ def test_load
5
+ assert_equal(8, SingletonTest.instance.a.b)
6
+ assert_equal(3, SingletonTest.instance.a.f.count)
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ require 'helper'
2
+
3
+ class TestValidation < Test::Unit::TestCase
4
+ def test_load
5
+ CollectionTest.objectdir "#{TESTDIR}/data/collection_bogus"
6
+ assert_raise Yamo::ValidateError do
7
+ CollectionTest.entries
8
+ end
9
+
10
+ assert_raise Yamo::ValidateError do
11
+ CollectionTest.schema "#{TESTDIR}/data/schemas/collection_bogus.yaml"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,75 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{yamo}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kenneth Vestergaard Schmidt"]
12
+ s.date = %q{2009-12-03}
13
+ s.description = %q{Classes for loading schema-validated YAML, and creating nice Ruby-objects with accessors}
14
+ s.email = %q{kvs@binarysolutions.dk}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "Gemfile",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/yamo.rb",
28
+ "lib/yamo/collection.rb",
29
+ "lib/yamo/object.rb",
30
+ "lib/yamo/singleton.rb",
31
+ "lib/yamo/validator.rb",
32
+ "test/data/collection/obj1",
33
+ "test/data/collection/obj2",
34
+ "test/data/collection_bogus/obj1",
35
+ "test/data/collection_bogus/obj2",
36
+ "test/data/schemas/collection.yaml",
37
+ "test/data/schemas/collection_bogus.yaml",
38
+ "test/data/schemas/singleton.yaml",
39
+ "test/data/singleton.conf",
40
+ "test/helper.rb",
41
+ "test/test_collection.rb",
42
+ "test/test_singleton.rb",
43
+ "test/test_validation.rb",
44
+ "yamo.gemspec"
45
+ ]
46
+ s.homepage = %q{http://github.com/kvs/yamo}
47
+ s.rdoc_options = ["--charset=UTF-8"]
48
+ s.require_paths = ["lib"]
49
+ s.required_ruby_version = Gem::Requirement.new(">= 1.9.1")
50
+ s.rubygems_version = %q{1.3.5}
51
+ s.summary = %q{YAML Model Objects}
52
+ s.test_files = [
53
+ "test/helper.rb",
54
+ "test/test_collection.rb",
55
+ "test/test_singleton.rb",
56
+ "test/test_validation.rb"
57
+ ]
58
+
59
+ if s.respond_to? :specification_version then
60
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
61
+ s.specification_version = 3
62
+
63
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
64
+ s.add_development_dependency(%q<yard>, [">= 0"])
65
+ s.add_runtime_dependency(%q<kwalify>, [">= 0.7.1"])
66
+ else
67
+ s.add_dependency(%q<yard>, [">= 0"])
68
+ s.add_dependency(%q<kwalify>, [">= 0.7.1"])
69
+ end
70
+ else
71
+ s.add_dependency(%q<yard>, [">= 0"])
72
+ s.add_dependency(%q<kwalify>, [">= 0.7.1"])
73
+ end
74
+ end
75
+
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yamo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kenneth Vestergaard Schmidt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-03 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: yard
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: kwalify
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.1
34
+ version:
35
+ description: Classes for loading schema-validated YAML, and creating nice Ruby-objects with accessors
36
+ email: kvs@binarysolutions.dk
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - Gemfile
48
+ - LICENSE
49
+ - README.rdoc
50
+ - Rakefile
51
+ - VERSION
52
+ - lib/yamo.rb
53
+ - lib/yamo/collection.rb
54
+ - lib/yamo/object.rb
55
+ - lib/yamo/singleton.rb
56
+ - lib/yamo/validator.rb
57
+ - test/data/collection/obj1
58
+ - test/data/collection/obj2
59
+ - test/data/collection_bogus/obj1
60
+ - test/data/collection_bogus/obj2
61
+ - test/data/schemas/collection.yaml
62
+ - test/data/schemas/collection_bogus.yaml
63
+ - test/data/schemas/singleton.yaml
64
+ - test/data/singleton.conf
65
+ - test/helper.rb
66
+ - test/test_collection.rb
67
+ - test/test_singleton.rb
68
+ - test/test_validation.rb
69
+ - yamo.gemspec
70
+ has_rdoc: true
71
+ homepage: http://github.com/kvs/yamo
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --charset=UTF-8
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 1.9.1
84
+ version:
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.5
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: YAML Model Objects
98
+ test_files:
99
+ - test/helper.rb
100
+ - test/test_collection.rb
101
+ - test/test_singleton.rb
102
+ - test/test_validation.rb