soft_validate 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/soft_validate.rb CHANGED
@@ -1,70 +1,36 @@
1
1
  require 'active_record'
2
- require 'pry'
3
2
 
4
3
  module SoftValidate
5
-
6
- def self.included(base)
7
- base.send :extend, ClassMethods
8
- end
4
+ extend ActiveSupport::Concern
9
5
 
10
6
  module ClassMethods
11
- # any method placed here will apply to classes, like Hickwall
12
-
13
7
  def soft_validates_presence_of(attr)
14
8
  cattr_accessor :soft_attributes
15
9
  self.soft_attributes ||= Array.new
16
10
  self.soft_attributes << attr
17
- send :include, InstanceMethods
18
11
  end
19
12
 
20
-
21
-
22
13
  end
23
14
 
24
- module InstanceMethods
25
- # any method placed here will apply to instaces, like @hickwall
26
-
27
- def progress_complete_count
28
- self.soft_attributes.length
29
- end
30
-
31
- def soft_valid?
32
-
33
- self.soft_attributes.each do |a|
34
- return false if self.attributes[a.to_s].nil?
35
- end
36
-
37
- true
38
- end
39
-
40
- def soft_errors
41
- errors = {}
42
- self.soft_attributes.each do |a|
43
- if self.attributes[a.to_s].nil?
44
- errors[a] = "shouldn't be blank"
45
- end
46
- end
47
-
48
- errors
49
- end
50
-
51
- def progress_count
52
- count = 0
15
+ def progress_complete_count
16
+ self.soft_attributes.length
17
+ end
53
18
 
54
- self.soft_attributes.each do |a|
55
- if !self.attributes[a.to_s].nil?
56
- count += 1
57
- end
58
- end
19
+ def soft_valid?
20
+ self.soft_attributes.all? { |a| !self.attributes[a.to_s].nil? }
21
+ end
59
22
 
60
- count
61
- end
23
+ def soft_errors
24
+ Hash[ self.soft_attributes.map { |a| [a, "shouldn't be blank"] if self.attributes[a.to_s].nil? } ]
25
+ end
62
26
 
63
- def progress_percent
64
- self.progress_count.to_f / self.progress_complete_count.to_f
65
- end
27
+ def progress_count
28
+ self.soft_attributes.count { |a| !self.attributes[a.to_s].nil? }
66
29
  end
67
30
 
31
+ def progress_percent
32
+ self.progress_count.to_f / self.progress_complete_count.to_f
33
+ end
68
34
  end
69
35
 
70
36
  ActiveRecord::Base.send :include, SoftValidate
@@ -1,3 +1,3 @@
1
1
  module SoftValidate
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/test/debug.log CHANGED
@@ -23,3 +23,19 @@
23
23
   (0.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
24
24
   (0.1ms) SELECT version FROM "schema_migrations"
25
25
   (0.1ms) INSERT INTO "schema_migrations" (version) VALUES ('0')
26
+  (1.2ms) select sqlite_version(*)
27
+  (0.4ms) CREATE TABLE "dumb_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255), "first_name" varchar(255), "last_name" varchar(255))
28
+  (0.2ms) CREATE TABLE "non_validated_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255)) 
29
+  (0.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
30
+  (0.0ms) PRAGMA index_list("schema_migrations")
31
+  (0.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
32
+  (0.1ms) SELECT version FROM "schema_migrations"
33
+  (0.1ms) INSERT INTO "schema_migrations" (version) VALUES ('0')
34
+  (2.2ms) select sqlite_version(*)
35
+  (0.4ms) CREATE TABLE "dumb_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255), "first_name" varchar(255), "last_name" varchar(255))
36
+  (0.2ms) CREATE TABLE "non_validated_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar(255)) 
37
+  (0.1ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
38
+  (0.0ms) PRAGMA index_list("schema_migrations")
39
+  (0.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
40
+  (0.1ms) SELECT version FROM "schema_migrations"
41
+  (0.1ms) INSERT INTO "schema_migrations" (version) VALUES ('0')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soft_validate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2012-04-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
17
- requirement: &70095288178760 !ruby/object:Gem::Requirement
17
+ requirement: &70180042847580 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 3.2.3
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70095288178760
25
+ version_requirements: *70180042847580
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: sqlite3
28
- requirement: &70095288177280 !ruby/object:Gem::Requirement
28
+ requirement: &70180042845940 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70095288177280
36
+ version_requirements: *70180042845940
37
37
  description: Add softs_validate_presence_of :attr to your model and it will give you
38
38
  methods to figure out percent complete, missing fields, etc without preventing the
39
39
  record from being saved.