scriba 0.0.1.alpha1 → 0.0.1.alpha2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a528ea559e6201610d1acf1c5089f8bd283aa0e
4
- data.tar.gz: 292212ef584125ff89a62ad102b6234ec1662d2c
3
+ metadata.gz: dc46acaf0eb146741ee1eab5e2465410dc6dae8d
4
+ data.tar.gz: 7686ab4fd5581a0536b38e3979bb3bd0473195cb
5
5
  SHA512:
6
- metadata.gz: 91d0bdad1416952999202d06d0473f04c2f89cb426c7c3bc5a54f6c31aed2130bf2c2ae21faa0649fa7c7a418b9531251f3d30c4e6e7a5c9f485856e9fba4e5b
7
- data.tar.gz: b6556347da194c438ce0077533c6bf2289c33aa5cc5b4ec7c942f897c37ec820af5e1f917e43b1afe323fc573ba2208b20d56ad2b14350b74cf96bc87eca164c
6
+ metadata.gz: c4bf2701fd0b99e60045d59726b086cd8d29e4fd80a0acc14a2c4b9728feca5d5fc6217172aca9f51e50496478c9e0d4270e10bb0c15ef57488d5cbcfedd85f7
7
+ data.tar.gz: 0fb347174d7e93a78942cbbde070b8173f0c4e75248e0e74fbc1ae34e9bfa36801e69cacdbbde1bd08ccf8e57a14b4c7552c24d13e764d551935f6ff82ef703c
@@ -1,4 +1,23 @@
1
1
  module Scriba::Assertions
2
+ def valid?
3
+ errors.clear
4
+
5
+ validate
6
+
7
+ return errors.empty?
8
+ end
9
+
10
+ def invalid?
11
+ return !valid?
12
+ end
13
+
14
+ def validate
15
+ end
16
+
17
+ def errors
18
+ @errors ||= Hash.new { |hash, key| hash[key] = [] }
19
+ end
20
+
2
21
  protected
3
22
 
4
23
  EMAIL = /\A([\w\!\#$\%\&\'\*\+\-\/\=\?\^\end\|\}\~]+\.)*
@@ -51,6 +70,6 @@ module Scriba::Assertions
51
70
  end
52
71
 
53
72
  def add_error(att, options)
54
- @errors[att] << generate_message(options)
73
+ errors[att] << generate_message(options)
55
74
  end
56
75
  end
@@ -0,0 +1,9 @@
1
+ module Scriba::PartialUpdate
2
+ def __attributes
3
+ @_attributes ||= @_updates.keys.map(&:to_sym)
4
+ end
5
+
6
+ def assert(value, att, options = {})
7
+ super(value, att, options) if __attributes.include?(att)
8
+ end
9
+ end
data/lib/scriba.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  class Scriba
2
2
  require_relative "scriba/assertions"
3
3
  require_relative "scriba/messages"
4
+ require_relative "scriba/partial_update"
4
5
 
5
6
  include Scriba::Assertions
6
7
  include Scriba::Messages
@@ -18,40 +19,26 @@ class Scriba
18
19
  @attributes ||= []
19
20
  end
20
21
 
21
- attr :errors
22
+ def initialize(updates = {})
23
+ update(@_updates = updates)
24
+ end
22
25
 
23
- def initialize(atts = {})
24
- atts.each do |att, value|
26
+ def update(updates)
27
+ updates.each do |att, value|
25
28
  accessor = "#{att}="
26
-
27
- if respond_to?(accessor)
28
- send(accessor, value)
29
- end
29
+ send(accessor, value) if respond_to?(accessor)
30
30
  end
31
-
32
- @errors = Hash.new { |hash, key| hash[key] = [] }
33
31
  end
34
32
 
35
33
  def attributes
36
- return slice(*self.class.attributes)
34
+ return slice(*__attributes)
37
35
  end
38
36
 
39
- def slice(*atts)
40
- return atts.each_with_object({}) { |att, hash| hash[att] = send(att) }
37
+ def __attributes
38
+ return self.class.attributes
41
39
  end
42
40
 
43
- def valid?
44
- @errors.clear
45
-
46
- validate
47
-
48
- return @errors.empty?
49
- end
50
-
51
- def invalid?
52
- return !valid?
53
- end
54
-
55
- def validate
41
+ def slice(*atts)
42
+ return atts.each_with_object({}) { |att, hash| hash[att] = send(att) }
56
43
  end
57
44
  end
data/scriba.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "scriba"
3
- s.version = "0.0.1.alpha1"
3
+ s.version = "0.0.1.alpha2"
4
4
  s.summary = "Validation frontend for models."
5
5
  s.description = s.summary + " Inspired by the scrivener gem."
6
6
  s.authors = ["Francesco Rodríguez"]
data/test/partial.rb ADDED
@@ -0,0 +1,28 @@
1
+ require_relative "helper"
2
+
3
+ class Signup < Scriba
4
+ include Scriba::PartialUpdate
5
+
6
+ attribute :username
7
+ attribute :password
8
+
9
+ def validate
10
+ assert_present(:username)
11
+ assert_present(:password)
12
+ end
13
+ end
14
+
15
+ test "return only passed attributes" do
16
+ attrs = { username: "me" }
17
+ signup = Signup.new(attrs)
18
+
19
+ assert_equal(attrs, signup.attributes)
20
+ end
21
+
22
+ test "validate given attributes" do
23
+ attrs = { username: "" }
24
+ signup = Signup.new(attrs)
25
+
26
+ assert !signup.valid?
27
+ assert_equal [:username], signup.errors.keys
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scriba
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha1
4
+ version: 0.0.1.alpha2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francesco Rodríguez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-30 00:00:00.000000000 Z
11
+ date: 2015-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest
@@ -37,11 +37,13 @@ files:
37
37
  - lib/scriba.rb
38
38
  - lib/scriba/assertions.rb
39
39
  - lib/scriba/messages.rb
40
+ - lib/scriba/partial_update.rb
40
41
  - makefile
41
42
  - scriba.gemspec
42
43
  - test/assertions.rb
43
44
  - test/attributes.rb
44
45
  - test/helper.rb
46
+ - test/partial.rb
45
47
  homepage: https://github.com/harmoni/scriba
46
48
  licenses:
47
49
  - MIT