vexile 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vexile.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alexander Kostrov
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,29 @@
1
+ # Vexile
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'vexile'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install vexile
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/vexile.rb ADDED
@@ -0,0 +1,78 @@
1
+ require "vexile/version"
2
+ require "active_model"
3
+ require "vexile/validators/recursive_validator"
4
+ require "vexile/validators/size_validator"
5
+ require "vexile/validators/url_validator"
6
+ require 'active_support/inflector'
7
+ require 'active_support/concern'
8
+ require 'bzproxies'
9
+
10
+ module Vexile
11
+ class << self
12
+ attr_accessor :klasses
13
+ end
14
+
15
+ class VexileProxy < Proxy
16
+ def __setobj__ value
17
+ klass = @options[:owner_class].vexile_const_lookup(@options[:class_name])
18
+ raise "#{@options[:class_name]} is not a vexile class" unless klass
19
+ @target = klass.new
20
+ load_params value
21
+ end
22
+
23
+ def load_params hash
24
+ hash.each do |key, value|
25
+ if self.respond_to? "#{key}="
26
+ self.__send__("#{key}=", value)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ module DSL
33
+
34
+ extend ActiveSupport::Concern
35
+
36
+ included do
37
+ Vexile.klasses ||= []
38
+ Vexile.klasses << self
39
+ include ActiveModel::Validations
40
+ end
41
+
42
+ module ClassMethods
43
+ def vexile_const_lookup const
44
+ namespaced = [self, self.parents].flatten.map{|i| [i, const.to_s].join "::"}
45
+ .push(const.to_s)
46
+ .sort{|a,b| b.length <=> a.length}
47
+ vexile_class = namespaced.detect{|i| Vexile.klasses.map(&:name).include?(i)}
48
+ vexile_class && vexile_class.constantize
49
+ end
50
+
51
+ def has_many *attribute_names
52
+ if attribute_names.last.kind_of? Hash
53
+ options = attribute_names.pop
54
+ else
55
+ options = {}
56
+ end
57
+ attribute_names.each do |name|
58
+ klass_name = (options[:class_name] || name.to_s.underscore).singularize.classify
59
+ proxy_accessor name.to_s.underscore, :proxy => [ArrayProxy, VexileProxy], :class_name => klass_name, :owner_class => self
60
+ end
61
+ end
62
+
63
+ def has_one *attribute_names
64
+ if attribute_names.last.kind_of? Hash
65
+ options = attribute_names.pop
66
+ else
67
+ options = {}
68
+ end
69
+ attribute_names.each do |name|
70
+ class_name = (options[:class_name] || name.to_s.underscore).singularize.classify
71
+ proxy_accessor name.to_s.underscore, :proxy => VexileProxy, :class_name => class_name, :owner_class => self
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+
@@ -0,0 +1,14 @@
1
+ class RecursiveValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ param_proxy = record.__send__(attribute)
4
+ if param_proxy
5
+ if param_proxy.respond_to? :each
6
+ param_proxy.each do |sub_proxy|
7
+ record.errors.add attribute, "is invalid in #{sub_proxy} : #{sub_proxy.errors.messages}" if !sub_proxy.valid?
8
+ end
9
+ else
10
+ record.errors.add attribute, "is invalid in #{param_proxy} : #{param_proxy.errors.messages}" if !param_proxy.valid?
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ class SizeValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ real_value = record.__send__(attribute)
4
+ if real_value.respond_to? :size
5
+ if @options[:with] and (real_value.size != @options[:with].to_i)
6
+ record.errors.add attribute, "should have #{@options[:with]} element(s)"
7
+ else
8
+ if @options[:min] and (real_value.size < @options[:min].to_i)
9
+ record.errors.add attribute, "should be bigger than #{@options[:min]} element(s)"
10
+ end
11
+ if @options[:max] and (real_value.size > @options[:max].to_i)
12
+ record.errors.add attribute, "should be shorter than #{@options[:max]} element(s)"
13
+ end
14
+ end
15
+ else
16
+ if (@options[:with] and (@options[:with].to_i != 0)) || @options[:min]
17
+ record.errors.add attribute, "is blank or could not be counted"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ require "addressable/uri"
2
+ class UrlValidator < ActiveModel::EachValidator
3
+ def validate_each(record, attribute, value)
4
+ begin
5
+ uri = ::Addressable::URI.parse(value)
6
+ if !uri or !["http","https","ftp", "riak"].include?(uri.scheme)
7
+ raise ::Addressable::URI::InvalidURIError
8
+ end
9
+ rescue ::Addressable::URI::InvalidURIError
10
+ record.errors[attribute] << "Invalid URL"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Vexile
2
+ VERSION = "1.0.0"
3
+ end
data/spec/main_spec.rb ADDED
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vexile do
4
+
5
+ before(:all) do
6
+ class A
7
+ include Vexile::DSL
8
+ attr_accessor :p1, :p2
9
+ end
10
+
11
+ class B
12
+ include Vexile::DSL
13
+ attr_accessor :p3, :p4
14
+ validates :p4, :format => /^test\d+$/
15
+ end
16
+
17
+ class C
18
+ include Vexile::DSL
19
+ attr_accessor :p5, :p6
20
+ validates :bs, :size => {:min => 1, :max => 2}, :recursive => true
21
+ has_many :bs
22
+ end
23
+
24
+ class Test
25
+ include Vexile::DSL
26
+ has_many :as, :bs
27
+ has_one :c
28
+ validates :c, :recursive => true
29
+ end
30
+
31
+ module Namespaced
32
+ class C
33
+ include Vexile::DSL
34
+ attr_accessor :t
35
+ end
36
+ class Test2
37
+ include Vexile::DSL
38
+ has_one :c
39
+ end
40
+ end
41
+ end
42
+
43
+
44
+ it "should add attributes method" do
45
+ t = Test.new
46
+ t.c = {:p5 => 1, :p6 => 2, :bs => [{:p3 => "test1", :p4 => "test2"}]}
47
+ t.c.p5.should == 1
48
+ t.c.p6.should == 2
49
+ t.c.bs.first.p3.should == "test1"
50
+ t.c.bs.first.p4.should == "test2"
51
+ end
52
+
53
+ it "should run validations correctly" do
54
+ t = Test.new
55
+ t.c = {:p5 => 1, :p6 => 2, :bs => [{:p3 => "test1", :p4 => "testfghgfh"}, {:p3 => "test3", :p4 => "testgsdfgdsf"}]}
56
+ t.c.valid?.should be_false
57
+ end
58
+
59
+ it "should raise an exception when initialization is incorrect" do
60
+ t = Test.new
61
+ expect do
62
+ t.c = {:p5 => 1, :p6 => 2, :bs => [{:p3 => "test1", :p4 => "test2"}].to_json}
63
+ end.to raise_error
64
+ end
65
+
66
+ it "should validates every element with array attributes" do
67
+ t = Test.new
68
+ t.c = {:p5 => 1, :p6 => 2, :bs => [{:p3 => "test1", :p4 => "test2"},{:p3 => "test1", :p4 => "tfds"}]}
69
+ t.valid?.should be_false
70
+ t.c = {:p5 => 1, :p6 => 2, :bs => [{:p3 => "test1", :p4 => "test2"},{:p3 => "test1", :p4 => "test2"}]}
71
+ t.valid?.should be_true
72
+ end
73
+
74
+ it "should correctly resolve vexile classes namespaces" do
75
+ t = Namespaced::Test2.new
76
+ expect do
77
+ t.c = {:t => "some_value"}
78
+ end.to_not raise_error
79
+ t.c.class.should == Namespaced::C
80
+ end
81
+ end
@@ -0,0 +1,7 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'vexile'
5
+
6
+ RSpec.configure do |config|
7
+ end
data/vexile.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'base64'
3
+ require File.expand_path('../lib/vexile/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Alexander Kostrov"]
7
+ gem.email = Base64.decode64("Ym9tYmF6b29rQGdtYWlsLmNvbQ==\n")
8
+ gem.description = "Auto generating ActiveModel validations for hashes"
9
+ gem.summary = "ActiveModel validations for hashes"
10
+ gem.homepage = "http://malstream.info"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "vexile"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Vexile::VERSION
18
+
19
+ gem.add_dependency 'activemodel', '~> 3'
20
+ gem.add_dependency 'activesupport', '~> 3'
21
+ gem.add_dependency 'bzproxies'
22
+ gem.add_dependency 'addressable'
23
+ gem.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vexile
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander Kostrov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
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: '3'
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: '3'
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: '3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bzproxies
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: addressable
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
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: rspec
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
+ description: Auto generating ActiveModel validations for hashes
95
+ email: !binary |-
96
+ Ym9tYmF6b29rQGdtYWlsLmNvbQ==
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rspec
103
+ - Gemfile
104
+ - LICENSE
105
+ - README.md
106
+ - Rakefile
107
+ - lib/vexile.rb
108
+ - lib/vexile/validators/recursive_validator.rb
109
+ - lib/vexile/validators/size_validator.rb
110
+ - lib/vexile/validators/url_validator.rb
111
+ - lib/vexile/version.rb
112
+ - spec/main_spec.rb
113
+ - spec/spec_helper.rb
114
+ - vexile.gemspec
115
+ homepage: http://malstream.info
116
+ licenses: []
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 1.8.21
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: ActiveModel validations for hashes
139
+ test_files:
140
+ - spec/main_spec.rb
141
+ - spec/spec_helper.rb
142
+ has_rdoc: