rasti-form 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3dad1f53996c5b3093f6c924b8475b64ae9826d
4
- data.tar.gz: 572457d753e9fd911b3c1ea4780bdfd32b0760f7
3
+ metadata.gz: 09ba062822c3985b958c2be7c6ab804fd3673d1d
4
+ data.tar.gz: c5a2d25a70bb06c81bc1e3198a41246ca9920e0d
5
5
  SHA512:
6
- metadata.gz: b09a249237c1b4b324e67dcad8c19ee504d0ed741098eb0766f99379f0a011088694dc695e5ac3bdb88435135af78a9d0d63ed045294974aae95e7242ed1fa4f
7
- data.tar.gz: 397de77010d7955fadf05b57f5bb189562c17c92c52d2001b43661c06d59f9db1a2d639b55ad0bf354c76f6f46d590a65d6a4dc8b42a8e36ec93a22fd5730c30
6
+ metadata.gz: 438c5305cb38e15abf2fb1c34c4dbf24e1c4baabe848f93d2f743572b3be5ab8738e1b4625d19cb62546bbeb39c3c80210a65e861ba772891f1c1c0640c323dd
7
+ data.tar.gz: 7af935310495c9dcc0012e704441523366d87c288c0f82e57a5ba161457e2e2110399e77976afb5c3ee6c3e89d5c3de49f99180f6c9c7a4afc861616f8ff04bf
data/Rakefile CHANGED
@@ -3,7 +3,8 @@ require 'rake/testtask'
3
3
 
4
4
  Rake::TestTask.new(:spec) do |t|
5
5
  t.libs << 'spec'
6
- t.pattern = 'spec/**/*_spec.rb'
6
+ t.libs << 'lib'
7
+ t.pattern = ENV['DIR'] ? File.join(ENV['DIR'], '**', '*_spec.rb') : 'spec/**/*_spec.rb'
7
8
  t.verbose = false
8
9
  t.warning = false
9
10
  t.loader = nil if ENV['TEST']
data/lib/rasti/form.rb CHANGED
@@ -80,9 +80,13 @@ module Rasti
80
80
  private
81
81
 
82
82
  def assign_attributes(attrs={})
83
- self.class.attribute_names.each do |name|
83
+ attrs.each do |name, value|
84
84
  begin
85
- write_attribute name, attrs[name] if attrs.key? name
85
+ if self.class.attributes.key? name
86
+ write_attribute name, value
87
+ else
88
+ errors[name] << 'Unexpected attribute'
89
+ end
86
90
 
87
91
  rescue CastError => error
88
92
  errors[name] << error.message
@@ -91,7 +95,7 @@ module Rasti
91
95
  error.errors.each do |inner_name, inner_errors|
92
96
  inner_errors.each { |message| errors["#{name}.#{inner_name}"] << message }
93
97
  end
94
- end
98
+ end
95
99
  end
96
100
  end
97
101
 
@@ -20,6 +20,7 @@ module Rasti
20
20
 
21
21
  def initialize(values)
22
22
  @values = values.map(&:to_s)
23
+ define_getters
23
24
  end
24
25
 
25
26
  def valid?(value)
@@ -32,6 +33,15 @@ module Rasti
32
33
  String.cast value
33
34
  end
34
35
 
36
+ def define_getters
37
+ values.each do |value|
38
+ getter_name = value.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z])([A-Z])/, '\1_\2').downcase
39
+ define_singleton_method getter_name do
40
+ value
41
+ end
42
+ end
43
+ end
44
+
35
45
  end
36
46
  end
37
47
  end
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  class Form
3
- VERSION = '0.1.0'
3
+ VERSION = '1.0.0'
4
4
  end
5
5
  end
data/spec/form_spec.rb CHANGED
@@ -38,14 +38,9 @@ describe Rasti::Form do
38
38
  point.assigned?(:y).must_equal false
39
39
  end
40
40
 
41
- it 'Extra attributes' do
42
- point = point_class.new z: 3
43
- point.x.must_be_nil
44
- point.y.must_be_nil
45
- point.assigned?(:x).must_equal false
46
- point.assigned?(:y).must_equal false
47
- proc { point.z }.must_raise NoMethodError
48
- point.attributes.must_be_empty
41
+ it 'Invalid attributes' do
42
+ error = proc { point_class.new z: 3 }.must_raise Rasti::Form::ValidationError
43
+ error.message.must_equal 'Validation error: {"z":["Unexpected attribute"]}'
49
44
  end
50
45
 
51
46
  describe 'Casting' do
@@ -17,4 +17,12 @@ describe Rasti::Form::Types::Enum do
17
17
  end
18
18
  end
19
19
 
20
+ it 'Constants' do
21
+ enum = Rasti::Form::Types::Enum[:first_value, 'SecondValue', 'THIRD_VALUE']
22
+
23
+ enum.first_value.must_equal 'first_value'
24
+ enum.second_value.must_equal 'SecondValue'
25
+ enum.third_value.must_equal 'THIRD_VALUE'
26
+ end
27
+
20
28
  end
@@ -4,7 +4,7 @@ describe Rasti::Form::Types::Form do
4
4
 
5
5
  form = Rasti::Form[x: Rasti::Form::Types::Integer, y: Rasti::Form::Types::Integer]
6
6
 
7
- hash = {x: '1', y: '2', z: '3'}
7
+ hash = {x: '1', y: '2'}
8
8
 
9
9
  it 'Class' do
10
10
  result = Rasti::Form::Types::Form[form].cast hash
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-01 00:00:00.000000000 Z
11
+ date: 2017-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_require