fanforce-validations 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWIzNWUwOTdkMzM5YjFlZDYwNmRiOGY4NjBjZDRhNjVkYzMzYzU0Nw==
5
+ data.tar.gz: !binary |-
6
+ OTExYjI1MzZkZTcwY2VmZjIyZjY0MzhmZTZmOTc2MjZmY2Q4MTAzOQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NjQzOWViOTA0N2NjOTNlYmFlMTQ5MjFiM2ZjZmY2MjUyNWIwYjU5OWY0MDll
10
+ OGY5NWY0ZmZmNmJkZjBkZTZkMTRiMTg4Nzg3Yzg0NDk1NmQ1YjA1OGVjY2Q3
11
+ ZmFkOTA4ZmNjODMxZTZiNmRkYzVjNDI2ODBmMTUxYmNiNWVkZTU=
12
+ data.tar.gz: !binary |-
13
+ OGFjMWUyYmQ2NTZmNjYxODUwZDUyMDQ4N2YxYWJlNmNiMzg3OGUxZGFiNTky
14
+ NDM3MWRjNWMxNjQ5ZTdiYmQ3MjlhMWZmNjc2NGNjYmY1ZTc0NmRlZTU0YTg1
15
+ NDgwNTgyYzFkYjIxNmFkNmQxN2UzYTIwYWZiMTJiNmJlYzY1MTY=
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+ .DS_Store
20
+ .idea/
21
+ .idea/*
22
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ fanforce-validations
2
+ ==
3
+
4
+ Internal validations library used by the Fanforce codebase.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'fileutils'
4
+ include FileUtils
5
+
6
+ # Default Rake task is compile
7
+ #task :default => :compile
8
+
9
+ ########################################################################
10
+
11
+ Rake::TestTask.new do |t|
12
+ t.libs.push "lib"
13
+ t.libs.push "test"
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = false
16
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fanforce/validations/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'fanforce-validations'
8
+ gem.version = Fanforce::Validations::VERSION
9
+ gem.date = '2012-10-19'
10
+ gem.summary = %q{Internal validations library used by the Fanforce codebase.}
11
+ gem.authors = ['Caleb Clark']
12
+ gem.email = ['cclark@fanforce.com']
13
+ gem.homepage = 'http://github.com/fanforce/fanforce-validations'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ['lib']
18
+
19
+ gem.add_runtime_dependency 'activesupport-inflector', '~> 0.1'
20
+ gem.add_runtime_dependency 'fanforce-utils', '~> 0.1'
21
+ gem.add_runtime_dependency 'fanforce-errors', '~> 0.1'
22
+ end
@@ -0,0 +1,104 @@
1
+ require 'active_support/inflector'
2
+ require 'fanforce/errors'
3
+ require 'fanforce/utils'
4
+
5
+ module Fanforce::Validations
6
+ include Fanforce::Utils
7
+ extend Fanforce::Validations
8
+ def self.included(base) base.extend(self) end
9
+
10
+ def extract_validation_params(opts)
11
+ return opts.delete(:params) if is_present?(opts[:params])
12
+ return self.params if self.respond_to?(:params)
13
+ Fanforce.env[:request_params] || {}
14
+ end
15
+
16
+ def parse_validation_field_name(field, i, opts, params)
17
+ name = opts[:fields][i] if is_present?(opts[:fields])
18
+ name = field if is_blank?(name) and field.is_a?(Symbol)
19
+ name = :unknown if is_blank?(name)
20
+ name.to_sym
21
+ end
22
+
23
+ def validates_presence_of_all(*args)
24
+ opts = extract_options!(args)
25
+ params = extract_validation_params(opts)
26
+ fields_to_validate = args.is_a?(Array) ? args : [args]
27
+
28
+ invalid_fields = []
29
+ fields_to_validate.each_with_index do |field_to_validate, i|
30
+ if !(field_to_validate.is_a?(Symbol) ? is_present?(params[field_to_validate]) : is_present?(field_to_validate))
31
+ invalid_fields << parse_validation_field_name(field_to_validate, i, opts, params)
32
+ end
33
+ end
34
+ return true if invalid_fields.size == 0
35
+
36
+ errors = invalid_fields.inject([]) {|result, f| result << format_validation_error(opts, f, :missing_field) }
37
+ raise_validation_errors(opts, errors, Fanforce::Error::BadRequest)
38
+ end
39
+ alias validates_presence_of validates_presence_of_all
40
+
41
+ def validates_presence_of_any(*args)
42
+ opts = extract_options!(args)
43
+ params = extract_validation_params(opts)
44
+ fields_to_validate = args.is_a?(Array) ? args : [args]
45
+
46
+ invalid_fields = []
47
+ fields_to_validate.each_with_index do |field_to_validate, i|
48
+ if !(field_to_validate.is_a?(Symbol) ? is_present?(params[field_to_validate]) : is_present?(field_to_validate))
49
+ invalid_fields << parse_validation_field_name(field_to_validate, i, opts, params)
50
+ end
51
+ end
52
+ return true if invalid_fields.size < fields_to_validate.size
53
+
54
+ errors = [format_validation_error(opts, invalid_fields, :invalid_data)]
55
+ raise_validation_errors(opts, errors, Fanforce::Error::BadRequest)
56
+ end
57
+
58
+ def validates_true(*args)
59
+ opts = extract_options!(args)
60
+ params = extract_validation_params(opts)
61
+ bools_to_validate = args.is_a?(Array) ? args : [args]
62
+
63
+ invalid_fields = []
64
+ bools_to_validate.each_with_index do |bool_is_true, i|
65
+ if !bool_is_true
66
+ invalid_fields << parse_validation_field_name(nil, i, opts, nil)
67
+ end
68
+ end
69
+ return true if invalid_fields.size == 0
70
+
71
+ errors = invalid_fields.inject([]) {|result, f| result << format_validation_error(opts, f, :invalid_data) }
72
+ raise_validation_errors(opts, errors, Fanforce::Error::RequestFailed)
73
+ end
74
+
75
+ def format_validation_error(opts, fields, default_error_code=nil)
76
+ error = {
77
+ resource: is_present?(opts[:resource]) ? opts[:resource] : (self.class==Class ? self.name.demodulize.tableize.singularize : self.class.name.demodulize.tableize.singularize),
78
+ code: is_present?(opts[:code]) ? opts[:code] : default_error_code,
79
+ message: opts[:message]
80
+ }
81
+ fields.is_a?(Array) ? error.update(fields: fields) : error.update(field: fields)
82
+ end
83
+
84
+ def raise_validation_errors(opts, errors, default_error_class)
85
+ raise (opts[:error_class] || default_error_class).new(*errors)
86
+ end
87
+
88
+ def extract_options!(args)
89
+ if args.last.is_a?(::Hash)
90
+ opts = args.pop.symbolize_keys
91
+ if is_present?(opts[:field]) and opts[:fields].is_a?(Array)
92
+ opts[:fields] << opts.delete(:field)
93
+ elsif is_present?(opts[:field])
94
+ opts[:fields] = [opts.delete(:field)]
95
+ end
96
+ return opts
97
+ elsif args.last.is_a?(::String)
98
+ {message: args.pop.to_s}
99
+ else
100
+ {}
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,5 @@
1
+ class Fanforce
2
+ module Validations
3
+ VERSION = '0.2'
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Fanforce;
2
+ require_relative 'validations/version'
3
+ require_relative 'validations/validations'
4
+ end
5
+
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fanforce-validations
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - Caleb Clark
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2012-10-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport-inflector
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fanforce-utils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fanforce-errors
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
55
+ description:
56
+ email:
57
+ - cclark@fanforce.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - README.md
65
+ - Rakefile
66
+ - fanforce-validations.gemspec
67
+ - lib/fanforce/validations.rb
68
+ - lib/fanforce/validations/validations.rb
69
+ - lib/fanforce/validations/version.rb
70
+ homepage: http://github.com/fanforce/fanforce-validations
71
+ licenses: []
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.0.3
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Internal validations library used by the Fanforce codebase.
93
+ test_files: []