jsonmodel 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rvmrc +1 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +94 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/jsonmodel.gemspec +71 -0
- data/lib/config/locales/en.yml +12 -0
- data/lib/jsonmodel.rb +33 -0
- data/lib/jsonmodel/attributes.rb +27 -0
- data/lib/jsonmodel/naming.rb +18 -0
- data/lib/jsonmodel/persistence.rb +5 -0
- data/lib/jsonmodel/types.rb +10 -0
- data/lib/jsonmodel/validations.rb +79 -0
- data/lib/jsonmodel/validations/array_validator.rb +22 -0
- data/test/helper.rb +62 -0
- data/test/test_activemodel_lint.rb +11 -0
- data/test/test_jsonmodel.rb +124 -0
- metadata +126 -0
data/.document
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.2@jsonmodel --create
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionmailer (3.2.0)
|
5
|
+
actionpack (= 3.2.0)
|
6
|
+
mail (~> 2.4.0)
|
7
|
+
actionpack (3.2.0)
|
8
|
+
activemodel (= 3.2.0)
|
9
|
+
activesupport (= 3.2.0)
|
10
|
+
builder (~> 3.0.0)
|
11
|
+
erubis (~> 2.7.0)
|
12
|
+
journey (~> 1.0.0)
|
13
|
+
rack (~> 1.4.0)
|
14
|
+
rack-cache (~> 1.1)
|
15
|
+
rack-test (~> 0.6.1)
|
16
|
+
sprockets (~> 2.1.2)
|
17
|
+
activemodel (3.2.0)
|
18
|
+
activesupport (= 3.2.0)
|
19
|
+
builder (~> 3.0.0)
|
20
|
+
activerecord (3.2.0)
|
21
|
+
activemodel (= 3.2.0)
|
22
|
+
activesupport (= 3.2.0)
|
23
|
+
arel (~> 3.0.0)
|
24
|
+
tzinfo (~> 0.3.29)
|
25
|
+
activeresource (3.2.0)
|
26
|
+
activemodel (= 3.2.0)
|
27
|
+
activesupport (= 3.2.0)
|
28
|
+
activesupport (3.2.0)
|
29
|
+
i18n (~> 0.6)
|
30
|
+
multi_json (~> 1.0)
|
31
|
+
arel (3.0.0)
|
32
|
+
builder (3.0.0)
|
33
|
+
erubis (2.7.0)
|
34
|
+
git (1.2.5)
|
35
|
+
hike (1.2.1)
|
36
|
+
i18n (0.6.0)
|
37
|
+
jeweler (1.6.4)
|
38
|
+
bundler (~> 1.0)
|
39
|
+
git (>= 1.2.5)
|
40
|
+
rake
|
41
|
+
journey (1.0.0)
|
42
|
+
json (1.6.5)
|
43
|
+
mail (2.4.1)
|
44
|
+
i18n (>= 0.4.0)
|
45
|
+
mime-types (~> 1.16)
|
46
|
+
treetop (~> 1.4.8)
|
47
|
+
mime-types (1.17.2)
|
48
|
+
multi_json (1.0.4)
|
49
|
+
polyglot (0.3.3)
|
50
|
+
rack (1.4.0)
|
51
|
+
rack-cache (1.1)
|
52
|
+
rack (>= 0.4)
|
53
|
+
rack-ssl (1.3.2)
|
54
|
+
rack
|
55
|
+
rack-test (0.6.1)
|
56
|
+
rack (>= 1.0)
|
57
|
+
rails (3.2.0)
|
58
|
+
actionmailer (= 3.2.0)
|
59
|
+
actionpack (= 3.2.0)
|
60
|
+
activerecord (= 3.2.0)
|
61
|
+
activeresource (= 3.2.0)
|
62
|
+
activesupport (= 3.2.0)
|
63
|
+
bundler (~> 1.0)
|
64
|
+
railties (= 3.2.0)
|
65
|
+
railties (3.2.0)
|
66
|
+
actionpack (= 3.2.0)
|
67
|
+
activesupport (= 3.2.0)
|
68
|
+
rack-ssl (~> 1.3.2)
|
69
|
+
rake (>= 0.8.7)
|
70
|
+
rdoc (~> 3.4)
|
71
|
+
thor (~> 0.14.6)
|
72
|
+
rake (0.9.2.2)
|
73
|
+
rdoc (3.12)
|
74
|
+
json (~> 1.4)
|
75
|
+
sprockets (2.1.2)
|
76
|
+
hike (~> 1.2)
|
77
|
+
rack (~> 1.0)
|
78
|
+
tilt (~> 1.1, != 1.3.0)
|
79
|
+
thor (0.14.6)
|
80
|
+
tilt (1.3.3)
|
81
|
+
treetop (1.4.10)
|
82
|
+
polyglot
|
83
|
+
polyglot (>= 0.3.1)
|
84
|
+
tzinfo (0.3.31)
|
85
|
+
|
86
|
+
PLATFORMS
|
87
|
+
ruby
|
88
|
+
|
89
|
+
DEPENDENCIES
|
90
|
+
activemodel
|
91
|
+
activesupport
|
92
|
+
bundler (~> 1.0.0)
|
93
|
+
jeweler (~> 1.6.4)
|
94
|
+
rails (~> 3.2.0.rc)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Rodrigo Alvarez
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= jsonmodel
|
2
|
+
|
3
|
+
This gem allows you to create dynamic classes (models) based on a JSON Schema, providing validations and being compliant with ActiveModel.
|
4
|
+
You can check the tests to get an idea about how it works.
|
5
|
+
|
6
|
+
== Contributing to jsonmodel
|
7
|
+
|
8
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
9
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
10
|
+
* Fork the project
|
11
|
+
* Start a feature/bugfix branch
|
12
|
+
* Commit and push until you are happy with your contribution
|
13
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
14
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2011 Rodrigo Alvarez. See LICENSE.txt for
|
19
|
+
further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "jsonmodel"
|
18
|
+
gem.homepage = "http://github.com/Papipo/jsonmodel"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Dynamic Active Model compliant models via JSON Schema}
|
21
|
+
gem.description = %Q{This gem allows you to create dynamic classes (models) based on a JSON Schema, providing validations and being compliant with ActiveModel.}
|
22
|
+
gem.email = "papipo@gmail.com"
|
23
|
+
gem.authors = ["Rodrigo Alvarez"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => :test
|
36
|
+
|
37
|
+
require 'rdoc/task'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "jsonmodel #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/jsonmodel.gemspec
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "jsonmodel"
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Rodrigo Alvarez"]
|
12
|
+
s.date = "2012-01-23"
|
13
|
+
s.description = "This gem allows you to create dynamic classes (models) based on a JSON Schema, providing validations and being compliant with ActiveModel."
|
14
|
+
s.email = "papipo@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rvmrc",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"jsonmodel.gemspec",
|
29
|
+
"lib/config/locales/en.yml",
|
30
|
+
"lib/jsonmodel.rb",
|
31
|
+
"lib/jsonmodel/attributes.rb",
|
32
|
+
"lib/jsonmodel/naming.rb",
|
33
|
+
"lib/jsonmodel/persistence.rb",
|
34
|
+
"lib/jsonmodel/types.rb",
|
35
|
+
"lib/jsonmodel/validations.rb",
|
36
|
+
"lib/jsonmodel/validations/array_validator.rb",
|
37
|
+
"test/helper.rb",
|
38
|
+
"test/test_activemodel_lint.rb",
|
39
|
+
"test/test_jsonmodel.rb"
|
40
|
+
]
|
41
|
+
s.homepage = "http://github.com/Papipo/jsonmodel"
|
42
|
+
s.licenses = ["MIT"]
|
43
|
+
s.require_paths = ["lib"]
|
44
|
+
s.rubygems_version = "1.8.15"
|
45
|
+
s.summary = "Dynamic Active Model compliant models via JSON Schema"
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_runtime_dependency(%q<activemodel>, [">= 0"])
|
52
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
53
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
54
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
55
|
+
s.add_development_dependency(%q<rails>, ["~> 3.2.0.rc"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<activemodel>, [">= 0"])
|
58
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
59
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
60
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
61
|
+
s.add_dependency(%q<rails>, ["~> 3.2.0.rc"])
|
62
|
+
end
|
63
|
+
else
|
64
|
+
s.add_dependency(%q<activemodel>, [">= 0"])
|
65
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
66
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
67
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
68
|
+
s.add_dependency(%q<rails>, ["~> 3.2.0.rc"])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
en:
|
2
|
+
activemodel:
|
3
|
+
errors:
|
4
|
+
messages:
|
5
|
+
too_many_items:
|
6
|
+
one: can't have more than one item
|
7
|
+
other: can't have more than %{count} items
|
8
|
+
too_few_items:
|
9
|
+
one: must have at least one item
|
10
|
+
other: must have more than %{count} items
|
11
|
+
not_an_array: must be an array
|
12
|
+
invalid_item_type: has invalid items
|
data/lib/jsonmodel.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_support/core_ext/class/attribute'
|
3
|
+
require 'i18n'
|
4
|
+
|
5
|
+
I18n.load_path << File.join(File.dirname(__FILE__), "config", "locales", "en.yml")
|
6
|
+
|
7
|
+
module JSONModel
|
8
|
+
require 'jsonmodel/types'
|
9
|
+
require 'jsonmodel/validations'
|
10
|
+
require 'jsonmodel/attributes'
|
11
|
+
require 'jsonmodel/persistence'
|
12
|
+
require 'jsonmodel/naming'
|
13
|
+
|
14
|
+
# The purpose of the Base class is to allow injection of modules in the
|
15
|
+
# dynamic classes generated by self.from_hash
|
16
|
+
class Base
|
17
|
+
class_attribute :schema
|
18
|
+
|
19
|
+
include JSONModel::Persistence
|
20
|
+
include ActiveModel::Conversion
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.from_hash(schema)
|
24
|
+
Class.new(Base) do
|
25
|
+
self.schema = schema
|
26
|
+
|
27
|
+
include JSONModel::Attributes
|
28
|
+
include JSONModel::Validations
|
29
|
+
include JSONModel::Naming
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module JSONModel::Attributes
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
#TODO raise if the method already exists
|
5
|
+
base.send(:attr_accessor, *base.attribute_names)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(attributes = {})
|
9
|
+
attributes.each do |attribute, value|
|
10
|
+
send("#{attribute}=", value)
|
11
|
+
end
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def attributes
|
16
|
+
self.class.attribute_names.inject({}) do |memo,name|
|
17
|
+
memo[name] = send(name)
|
18
|
+
memo
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def attribute_names
|
24
|
+
@attribute_names ||= schema['properties'].keys.map(&:to_sym)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module JSONModel
|
2
|
+
module Naming
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def name
|
7
|
+
unless @name
|
8
|
+
@name = schema['name']
|
9
|
+
this = self
|
10
|
+
@name.define_singleton_method(:constantize) do
|
11
|
+
this
|
12
|
+
end
|
13
|
+
end
|
14
|
+
@name
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module JSONModel::Validations
|
2
|
+
require 'jsonmodel/validations/array_validator'
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
include ActiveModel::Validations
|
7
|
+
include InstanceMethods # Need to do it this way so valid?() can call super from ActiveModel
|
8
|
+
extend ClassMethods
|
9
|
+
|
10
|
+
def self.validators_on(*args)
|
11
|
+
setup_validations!
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.validators(*args)
|
16
|
+
setup_validations!
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module InstanceMethods
|
23
|
+
def valid?(*args)
|
24
|
+
self.class.send(:setup_validations!)
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
protected
|
31
|
+
def setup_validations!
|
32
|
+
return if @_validations_setup
|
33
|
+
schema['properties'].each do |name,data|
|
34
|
+
apply_validations_for(name.to_sym, data)
|
35
|
+
end
|
36
|
+
@_validations_setup = true
|
37
|
+
end
|
38
|
+
|
39
|
+
def apply_validations_for(property, data)
|
40
|
+
validates(property, :presence => true) if data['required']
|
41
|
+
|
42
|
+
case data['type']
|
43
|
+
when 'string'
|
44
|
+
validates property, :format => {:with => data['pattern']} if data['pattern']
|
45
|
+
when 'number', 'integer'
|
46
|
+
validates property, :numericality => options_for_numericality(data)
|
47
|
+
when 'array'
|
48
|
+
validates property, :array => options_for_array(data)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def options_for_array(data)
|
53
|
+
options = {}
|
54
|
+
options[:type] = data['items'] && data['items']['type']
|
55
|
+
options[:max_items] = data['maxItems']
|
56
|
+
options[:min_items] = data['minItems']
|
57
|
+
options
|
58
|
+
end
|
59
|
+
|
60
|
+
def options_for_numericality(data)
|
61
|
+
options = {}
|
62
|
+
if data['minimum']
|
63
|
+
if data['exclusiveMinimum']
|
64
|
+
options[:greater_than] = data['minimum']
|
65
|
+
else
|
66
|
+
options[:greater_than_or_equal_to] = data['minimum']
|
67
|
+
end
|
68
|
+
elsif data['maximum']
|
69
|
+
if data['exclusiveMaximum']
|
70
|
+
options[:less_than] = data['maximum']
|
71
|
+
else
|
72
|
+
options[:less_than_or_equal_to] = data['maximum']
|
73
|
+
end
|
74
|
+
end
|
75
|
+
options[:only_integer] = true if data['type'] == 'integer'
|
76
|
+
options
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class JSONModel::Validations::ArrayValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
unless value.is_a?(Array)
|
4
|
+
record.errors.add(attribute, :not_an_array)
|
5
|
+
return
|
6
|
+
end
|
7
|
+
|
8
|
+
if options[:type]
|
9
|
+
record.errors.add(attribute, :invalid_item_type) unless value.all? do |item|
|
10
|
+
item.is_a?(JSONModel::Types[options[:type]])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
if options[:max_items]
|
15
|
+
record.errors.add(attribute, :too_many_items, :count => options[:max_items]) if value.size > options[:max_items]
|
16
|
+
end
|
17
|
+
|
18
|
+
if options[:min_items]
|
19
|
+
record.errors.add(attribute, :too_few_items, :count => options[:min_items]) if value.size < options[:min_items]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
require 'jsonmodel'
|
15
|
+
|
16
|
+
class Test::Unit::TestCase
|
17
|
+
def product_schema
|
18
|
+
{
|
19
|
+
"name" => "Product",
|
20
|
+
"properties" => {
|
21
|
+
"name" => {
|
22
|
+
"type" => "string",
|
23
|
+
"required" => true
|
24
|
+
},
|
25
|
+
"price" => {
|
26
|
+
"type" => "number",
|
27
|
+
"required" => true,
|
28
|
+
"minimum" => 0
|
29
|
+
},
|
30
|
+
"weight" => {
|
31
|
+
"type" => "number",
|
32
|
+
"required" => true,
|
33
|
+
"minimum" => 0,
|
34
|
+
"exclusiveMinimum" => true
|
35
|
+
},
|
36
|
+
"ghz" => {
|
37
|
+
"type" => "number",
|
38
|
+
"required" => true,
|
39
|
+
"maximum" => 2.4
|
40
|
+
},
|
41
|
+
"cpus" => {
|
42
|
+
"type" => "integer",
|
43
|
+
"required" => true,
|
44
|
+
"maximum" => 3,
|
45
|
+
"exclusiveMaximum" => true
|
46
|
+
},
|
47
|
+
"tags" => {
|
48
|
+
"type" => "array",
|
49
|
+
"maxItems" => 5,
|
50
|
+
"minItems" => 1,
|
51
|
+
"items" => {
|
52
|
+
"type" => "string"
|
53
|
+
}
|
54
|
+
},
|
55
|
+
"ean" => {
|
56
|
+
"type" => "string",
|
57
|
+
"pattern" => /^[0-9]{13}$/
|
58
|
+
}
|
59
|
+
}
|
60
|
+
}
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestJSONModel < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@model = JSONModel.from_hash(product_schema)
|
6
|
+
|
7
|
+
@instance = @model.new(
|
8
|
+
:name => 'MacBook',
|
9
|
+
:price => 999,
|
10
|
+
:weight => 5,
|
11
|
+
:ghz => 2.4,
|
12
|
+
:cpus => 2,
|
13
|
+
:tags => ['apple', 'laptop'],
|
14
|
+
:ean => '0012345678905'
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
def test_attributes
|
20
|
+
assert_equal({
|
21
|
+
:name => 'MacBook',
|
22
|
+
:price => 999,
|
23
|
+
:weight => 5,
|
24
|
+
:ghz => 2.4,
|
25
|
+
:cpus => 2,
|
26
|
+
:tags => ['apple', 'laptop'],
|
27
|
+
:ean => '0012345678905'
|
28
|
+
}, @instance.attributes)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_class_name
|
32
|
+
assert_equal "Product", @model.name
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_name_constantize
|
36
|
+
assert_equal @model, @model.name.constantize
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_validators_on
|
40
|
+
assert_equal 1, @model.validators_on(:name).count
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_validators
|
44
|
+
assert @model.validators.count > 1
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_accessors
|
48
|
+
assert_equal "MacBook", @instance.name
|
49
|
+
@instance.name = ''
|
50
|
+
assert_equal '', @instance.name
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_required
|
54
|
+
@instance.name = ''
|
55
|
+
assert !@instance.valid?
|
56
|
+
assert_equal ["can't be blank"], @instance.errors[:name]
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_number
|
60
|
+
@instance.price = 'g'
|
61
|
+
assert !@instance.valid?
|
62
|
+
assert_equal ["is not a number"], @instance.errors[:price]
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_integer
|
66
|
+
@instance.cpus = 0.5
|
67
|
+
assert !@instance.valid?
|
68
|
+
assert_equal ["must be an integer"], @instance.errors[:cpus]
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_minimum_number
|
72
|
+
@instance.price = -1
|
73
|
+
assert !@instance.valid?
|
74
|
+
assert_equal ["must be greater than or equal to 0"], @instance.errors[:price]
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_exclusive_minimum
|
78
|
+
@instance.weight = 0
|
79
|
+
assert !@instance.valid?
|
80
|
+
assert_equal ["must be greater than 0"], @instance.errors[:weight]
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_maximum
|
84
|
+
@instance.ghz = 2.5
|
85
|
+
assert !@instance.valid?
|
86
|
+
assert_equal ["must be less than or equal to 2.4"], @instance.errors[:ghz]
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_exclusive_maximum
|
90
|
+
@instance.cpus = 3
|
91
|
+
assert !@instance.valid?
|
92
|
+
assert_equal ["must be less than 3"], @instance.errors[:cpus]
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_array_type
|
96
|
+
@instance.tags = 'test'
|
97
|
+
assert !@instance.valid?
|
98
|
+
assert_equal ["must be an array"], @instance.errors[:tags]
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_array_items_type
|
102
|
+
@instance.tags = [{"some" => "hash"}]
|
103
|
+
assert !@instance.valid?
|
104
|
+
assert_equal ["has invalid items"], @instance.errors[:tags]
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_array_maxitems
|
108
|
+
@instance.tags = ['one', 'two', 'three', 'four', 'five', 'toomany']
|
109
|
+
assert !@instance.valid?
|
110
|
+
assert_equal ["can't have more than 5 items"], @instance.errors[:tags]
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_array_minitems
|
114
|
+
@instance.tags = []
|
115
|
+
assert !@instance.valid?
|
116
|
+
assert_equal ["must have at least one item"], @instance.errors[:tags]
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_pattern
|
120
|
+
@instance.ean = '34'
|
121
|
+
assert !@instance.valid?
|
122
|
+
assert_equal ["is invalid"], @instance.errors[:ean]
|
123
|
+
end
|
124
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonmodel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rodrigo Alvarez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-23 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: &2177271740 !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: *2177271740
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
requirement: &2177269660 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2177269660
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
requirement: &2177268940 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2177268940
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: jeweler
|
49
|
+
requirement: &2177268100 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.6.4
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2177268100
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rails
|
60
|
+
requirement: &2177266940 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 3.2.0.rc
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *2177266940
|
69
|
+
description: This gem allows you to create dynamic classes (models) based on a JSON
|
70
|
+
Schema, providing validations and being compliant with ActiveModel.
|
71
|
+
email: papipo@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files:
|
75
|
+
- LICENSE.txt
|
76
|
+
- README.rdoc
|
77
|
+
files:
|
78
|
+
- .document
|
79
|
+
- .rvmrc
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.rdoc
|
84
|
+
- Rakefile
|
85
|
+
- VERSION
|
86
|
+
- jsonmodel.gemspec
|
87
|
+
- lib/config/locales/en.yml
|
88
|
+
- lib/jsonmodel.rb
|
89
|
+
- lib/jsonmodel/attributes.rb
|
90
|
+
- lib/jsonmodel/naming.rb
|
91
|
+
- lib/jsonmodel/persistence.rb
|
92
|
+
- lib/jsonmodel/types.rb
|
93
|
+
- lib/jsonmodel/validations.rb
|
94
|
+
- lib/jsonmodel/validations/array_validator.rb
|
95
|
+
- test/helper.rb
|
96
|
+
- test/test_activemodel_lint.rb
|
97
|
+
- test/test_jsonmodel.rb
|
98
|
+
homepage: http://github.com/Papipo/jsonmodel
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
hash: -1318207715464196030
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.8.15
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: Dynamic Active Model compliant models via JSON Schema
|
126
|
+
test_files: []
|