fanforce-internal-validations 0.1.0

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
+ MTdjZTk0Y2Y3M2YwZDhjMGFiN2MxMjMyMGE1MGI2MDcwNzNjZjkxMA==
5
+ data.tar.gz: !binary |-
6
+ ODJkMzlhM2VmYjExMWYwNjYwYWEzY2JjNWU5NDcyZDdlODkzOTNkMg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MTFjNGNhMTA2MmJlMjk0ZWJiMGMxMjdmM2EzODM3OGNiNjMxNWYxZDFhNWFl
10
+ NDNiZTNlYmIxMGY3MWFkN2U2MmQ1ZTNiNTM3ZmNlZWMwMzNiYTU4OTZhZmE4
11
+ NGYyNTlkMjg1MDZmMzc4NTMwOGI5MjRmNGJhZmU0YzhlNDAyNTc=
12
+ data.tar.gz: !binary |-
13
+ MWRmZTExYTQ4YmEyZGZlM2EzNWJkMGZkYTU0YTZhYzUzMjY4NjdlYTIwNmRl
14
+ NmQxZmQ2MDgzMTZiNGIwNDQ0ODg1NmRlOTdkODc4N2VjOWE2Yzc2N2E3ODhh
15
+ Mzc2ZmUzNjA5MjQxN2I0MDY0ZjllMmUwNGFhMDQzNWIxYWVlMTA=
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-internal-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,21 @@
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-internal-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/mlabs/fanforce-internal-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 'fanforce-internal-utils'
20
+ gem.add_runtime_dependency 'fanforce-internal-errors'
21
+ end
@@ -0,0 +1,106 @@
1
+ require 'fanforce/errors'
2
+ require 'fanforce/utils'
3
+
4
+ module Fanforce::Validations
5
+ include Fanforce::Utils
6
+ extend Fanforce::Validations
7
+ def self.included(base) base.extend(self) end
8
+
9
+ def extract_validation_params(opts)
10
+ return opts.delete(:params) if is_present?(opts[:params])
11
+ return self.params if self.respond_to?(:params)
12
+ Fanforce.env[:request_params] || {}
13
+ end
14
+
15
+ def parse_validation_field_name(field, i, opts, params)
16
+ name = opts[:fields][i] if is_present?(opts[:fields])
17
+ name = field if is_blank?(name) and field.is_a?(Symbol)
18
+ name = :unknown if is_blank?(name)
19
+ name.to_sym
20
+ end
21
+
22
+ def validates_presence_of(*args)
23
+ opts = extract_options!(args)
24
+ params = extract_validation_params(opts)
25
+ fields_to_validate = args.is_a?(Array) ? args : [args]
26
+
27
+ invalid_fields = []
28
+ fields_to_validate.each_with_index do |field_to_validate, i|
29
+ if !(field_to_validate.is_a?(Symbol) ? is_present?(params[field_to_validate]) : is_present?(field_to_validate))
30
+ invalid_fields << parse_validation_field_name(field_to_validate, i, opts, params)
31
+ end
32
+ end
33
+ return true if invalid_fields.size == 0
34
+
35
+ errors = invalid_fields.inject([]) {|result, f| result << format_validation_error(opts, f, :missing_field) }
36
+ raise_validation_error(opts, errors, Fanforce::Error::CorruptRequest)
37
+ end
38
+
39
+ def validates_presence_of_any(*args)
40
+ opts = extract_options!(args)
41
+ params = extract_validation_params(opts)
42
+ fields_to_validate = args.is_a?(Array) ? args : [args]
43
+
44
+ invalid_fields = []
45
+ fields_to_validate.each_with_index do |field_to_validate, i|
46
+ if !(field_to_validate.is_a?(Symbol) ? is_present?(params[field_to_validate]) : is_present?(field_to_validate))
47
+ invalid_fields << parse_validation_field_name(field_to_validate, i, opts, params)
48
+ end
49
+ end
50
+ return true if invalid_fields.size < fields_to_validate.size
51
+
52
+ errors = [format_validation_error(opts, invalid_fields, :invalid_data)]
53
+ raise_validation_error(opts, errors, Fanforce::Error::CorruptRequest)
54
+ end
55
+
56
+ def validates_presence_of_all(*args)
57
+ validates_presence_of(args)
58
+ end
59
+
60
+ def validates_true(*args)
61
+ opts = extract_options!(args)
62
+ params = extract_validation_params(opts)
63
+ bools_to_validate = args.is_a?(Array) ? args : [args]
64
+
65
+ invalid_fields = []
66
+ bools_to_validate.each_with_index do |bool_is_true, i|
67
+ if !bool_is_true
68
+ invalid_fields << parse_validation_field_name(nil, i, opts, nil)
69
+ end
70
+ end
71
+ return true if invalid_fields.size == 0
72
+
73
+ errors = invalid_fields.inject([]) {|result, f| result << format_validation_error(opts, f, :invalid_data) }
74
+ raise_validation_error(opts, errors, Fanforce::Error::RequestFailed)
75
+ end
76
+
77
+ def raise_validation_error(opts, errors, default_error_class)
78
+ raise (opts[:error_class] || default_error_class).new(errors)
79
+ end
80
+
81
+ def format_validation_error(opts, fields, default_error_code=nil)
82
+ error = {
83
+ resource: is_present?(opts[:resource]) ? opts[:resource] : self.class.name.demodulize.tableize.singularize,
84
+ code: is_present?(opts[:code]) ? opts[:code] : default_error_code,
85
+ message: opts[:message]
86
+ }
87
+ fields.is_a?(Array) ? error.update(fields: fields) : error.update(field: fields)
88
+ end
89
+
90
+ def extract_options!(args)
91
+ if args.last.is_a?(::Hash)
92
+ opts = args.pop.symbolize_keys
93
+ if is_present?(opts[:field]) and opts[:fields].is_a?(Array)
94
+ opts[:fields] << opts.delete(:field)
95
+ elsif is_present?(opts[:field])
96
+ opts[:fields] = [opts.delete(:field)]
97
+ end
98
+ return opts
99
+ elsif args.last.is_a?(::String)
100
+ {msg: args.pop.to_s}
101
+ else
102
+ {}
103
+ end
104
+ end
105
+
106
+ end
@@ -0,0 +1,5 @@
1
+ class Fanforce
2
+ module Validations
3
+ VERSION = '0.1.0'
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,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fanforce-internal-validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
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: fanforce-internal-utils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fanforce-internal-errors
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - cclark@fanforce.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - fanforce-internal-validations.gemspec
53
+ - lib/fanforce/validations.rb
54
+ - lib/fanforce/validations/validations.rb
55
+ - lib/fanforce/validations/version.rb
56
+ homepage: http://github.com/mlabs/fanforce-internal-validations
57
+ licenses: []
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.0.3
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Internal validations library used by the Fanforce codebase.
79
+ test_files: []