schash 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 14f23046a5330b40005faa16302f41f180fe76f8
4
+ data.tar.gz: 5e3468253f6bd076dd2680151e19e5ce54031e1f
5
+ SHA512:
6
+ metadata.gz: 5fae3816ab0fccf5b5131091b8657046038093125b19043e235687cb7b4f57257c9fb3d7e2278983d35536771767c9ba55a28f7de61376fa6d75ffd17efd0725
7
+ data.tar.gz: fad4a51c9f7abef9a774171bf89e7f9c6fe51ca304ff422c5a307c7eecc301ce563cf2ad20e50a14e4c762de8537d165a73255d9edad6aded50216bb780c0147
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in schash.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ryota Arai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Schash
2
+
3
+ Pronounciation: the same as "squash"
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/schash`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ TODO: Delete this and the text above, and describe your gem
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'schash'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install schash
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it ( https://github.com/[my-github-username]/schash/fork )
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "schash"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require "schash/schema"
2
+ require "schash/validator"
3
+ require "schash/version"
4
+
5
+ module Schash
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,3 @@
1
+ require 'schash/schema/error'
2
+ require 'schash/schema/rule'
3
+ require 'schash/schema/dsl'
@@ -0,0 +1,46 @@
1
+ module Schash
2
+ module Schema
3
+ class DSL
4
+ def self.evaluate(&block)
5
+ self.new.instance_eval(&block)
6
+ end
7
+
8
+ def array_of(schema)
9
+ Rule::ArrayOf.new(schema)
10
+ end
11
+
12
+ def type(klass)
13
+ Rule::Type.new(klass)
14
+ end
15
+
16
+ def optional(rule)
17
+ Rule::Optional.new(rule)
18
+ end
19
+
20
+ def string
21
+ type(String)
22
+ end
23
+
24
+ def numeric
25
+ type(Numeric)
26
+ end
27
+
28
+ def integer
29
+ type(Integer)
30
+ end
31
+
32
+ def float
33
+ type(Float)
34
+ end
35
+
36
+ def symbol
37
+ type(Symbol)
38
+ end
39
+
40
+ def array
41
+ type(Array)
42
+ end
43
+ end
44
+ end
45
+ end
46
+
@@ -0,0 +1,6 @@
1
+ module Schash
2
+ module Schema
3
+ class Error < Struct.new(:position, :message); end
4
+ end
5
+ end
6
+
@@ -0,0 +1,6 @@
1
+ require 'schash/schema/rule/base'
2
+ require 'schash/schema/rule/array_of'
3
+ require 'schash/schema/rule/hash'
4
+ require 'schash/schema/rule/optional'
5
+ require 'schash/schema/rule/type'
6
+
@@ -0,0 +1,31 @@
1
+ module Schash
2
+ module Schema
3
+ module Rule
4
+ class ArrayOf < Base
5
+ def initialize(rule)
6
+ @rule = if rule.is_a?(::Hash)
7
+ Rule::Hash.new(rule)
8
+ else
9
+ rule
10
+ end
11
+ end
12
+
13
+ def validate(target, position = [])
14
+ errors = []
15
+
16
+ unless target.is_a?(Array)
17
+ errors << Error.new(position, "is not an array")
18
+ return errors
19
+ end
20
+
21
+ errors += target.each_with_index.map do |t, i|
22
+ @rule.validate(t, position + [i])
23
+ end.flatten
24
+
25
+ errors
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,12 @@
1
+ module Schash
2
+ module Schema
3
+ module Rule
4
+ class Base
5
+ def optional?
6
+ false # default
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,32 @@
1
+ module Schash
2
+ module Schema
3
+ module Rule
4
+ class Hash < Base
5
+ def initialize(schema_hash)
6
+ @schema_hash = schema_hash
7
+ end
8
+
9
+ def validate(target, position = [])
10
+ @schema_hash.map do |key, rule|
11
+ if rule.is_a?(::Hash)
12
+ rule = self.class.new(rule)
13
+ end
14
+
15
+ found_key = [key.to_s, key.to_sym].find do |k|
16
+ target.has_key?(k)
17
+ end
18
+
19
+ if found_key
20
+ rule.validate(target[found_key], position + [key.to_s])
21
+ else
22
+ unless rule.optional?
23
+ Error.new(position + [key.to_s], "is required but missing")
24
+ end
25
+ end
26
+ end.flatten.compact
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,20 @@
1
+ module Schash
2
+ module Schema
3
+ module Rule
4
+ class Optional < Base
5
+ def initialize(rule)
6
+ @rule = rule
7
+ end
8
+
9
+ def validate(target, position = [])
10
+ @rule.validate(target, position)
11
+ end
12
+
13
+ def optional?
14
+ true
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,22 @@
1
+ module Schash
2
+ module Schema
3
+ module Rule
4
+ class Type < Base
5
+ def initialize(klass)
6
+ @klass = klass
7
+ end
8
+
9
+ def validate(target, position = [])
10
+ errors = []
11
+
12
+ unless target.is_a?(@klass)
13
+ errors << Error.new(position, "is not #{@klass}")
14
+ end
15
+
16
+ errors
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,12 @@
1
+ module Schash
2
+ class Validator
3
+ def initialize(&schema_block)
4
+ @validator = Schema::Rule::Hash.new(Schema::DSL.evaluate(&schema_block))
5
+ end
6
+
7
+ def validate(target)
8
+ @validator.validate(target)
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,3 @@
1
+ module Schash
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'schash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "schash"
8
+ spec.version = Schash::VERSION
9
+ spec.authors = ["Ryota Arai"]
10
+ spec.email = ["ryota.arai@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby Hash validator}
13
+ spec.homepage = "https://github.com/ryotarai/schash"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryota Arai
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - ryota.arai@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/schash.rb
72
+ - lib/schash/schema.rb
73
+ - lib/schash/schema/dsl.rb
74
+ - lib/schash/schema/error.rb
75
+ - lib/schash/schema/rule.rb
76
+ - lib/schash/schema/rule/array_of.rb
77
+ - lib/schash/schema/rule/base.rb
78
+ - lib/schash/schema/rule/hash.rb
79
+ - lib/schash/schema/rule/optional.rb
80
+ - lib/schash/schema/rule/type.rb
81
+ - lib/schash/validator.rb
82
+ - lib/schash/version.rb
83
+ - schash.gemspec
84
+ homepage: https://github.com/ryotarai/schash
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.4.5
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Ruby Hash validator
108
+ test_files: []