hcast 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NTM3NjVkMmE2YzUwZGJkYzdkMGRlYzZmZjhmMzQ5ZDJiNWYyYTNmMg==
5
+ data.tar.gz: !binary |-
6
+ MDRiN2M2ZTM1Mjc2MzhjMTkyNTBjOTM5YjNhZjE4OTAxZDEzYWRjNw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ N2JkYTdhMDg0ZTg5MDkzYjVjZGJlNjZjMGYzYzY5ZTRiYTAyYmJkYzA4NDYz
10
+ NGE5ZmYyMDIzNzdmZWJjYjg0M2M4NTAzYTU0YTdlMmZjNTg0YzRkYjYwNDQ5
11
+ ZjNlOWFjODEzNjk1YmQ4MDY5MWEzMWM4MGU5NWUyODc3ZGEzMmU=
12
+ data.tar.gz: !binary |-
13
+ MGZhZWU4YTdhNWEyMGYzZjM5OWY4MTI2MWVkNjY5MmRmYzY3Y2U4NTc3MTRm
14
+ Nzc0ZTVhOTgwYmVkODI2YzQxNzE5YTRiNWJmYzM2YjBlYjZhM2Y5OGI4N2Yy
15
+ ZTU1MzRiYzA5OGYwMDQ4MDA2NWI4OGRjZGYwOTYzYjczNGViNjU=
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hcast (0.1.0)
4
+ hcast (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -33,7 +33,7 @@ end
33
33
  ```
34
34
  Instanticate the caster and give your hash for casting:
35
35
  ```ruby
36
- ContactCaster.new.cast({
36
+ ContactCaster.cast({
37
37
  contact: {
38
38
  name: "John Smith",
39
39
  age: "22",
@@ -12,10 +12,15 @@ class HCast::AttributesCaster
12
12
  hash_keys = get_keys(input_hash)
13
13
  attributes.each do |attribute|
14
14
  if hash_keys.include?(attribute.name)
15
- casted_value = cast_attribute(attribute, input_hash)
16
- casted_hash[attribute.name] = casted_value
15
+ begin
16
+ casted_value = cast_attribute(attribute, input_hash)
17
+ casted_hash[attribute.name] = casted_value
18
+ rescue HCast::Errors::AttributeError => e
19
+ e.add_namespace(attribute.name)
20
+ raise e
21
+ end
17
22
  else
18
- raise HCast::Errors::MissingAttributeError, "#{attribute.name} should be given" if attribute.required?
23
+ raise HCast::Errors::MissingAttributeError.new("should be given", attribute.name)if attribute.required?
19
24
  end
20
25
  end
21
26
  check_unexpected_attributes_not_given!(hash_keys, casted_hash.keys)
@@ -75,7 +80,7 @@ class HCast::AttributesCaster
75
80
  def check_unexpected_attributes_not_given!(input_hash_keys, casted_hash_keys)
76
81
  unexpected_keys = input_hash_keys - casted_hash_keys
77
82
  unless unexpected_keys.empty?
78
- raise HCast::Errors::UnexpectedAttributeError, "Unexpected attributes given: #{unexpected_keys}"
83
+ raise HCast::Errors::UnexpectedAttributeError.new("is not valid attribute name", unexpected_keys.first)
79
84
  end
80
85
  end
81
86
 
@@ -29,7 +29,7 @@ class HCast::AttributesParser
29
29
  options = args[1] || {}
30
30
  caster = HCast.casters[caster_name]
31
31
 
32
- check_caster_exists!(caster)
32
+ check_caster_exists!(caster, caster_name)
33
33
  check_attr_name_valid!(attr_name)
34
34
  check_options_is_hash!(options)
35
35
 
@@ -42,9 +42,9 @@ class HCast::AttributesParser
42
42
 
43
43
  private
44
44
 
45
- def check_caster_exists!(caster)
45
+ def check_caster_exists!(caster, caster_name)
46
46
  if !caster
47
- raise HCast::Errors::CasterNotFoundError, "caster with name '#{caster}' is not found"
47
+ raise HCast::Errors::CasterNotFoundError, "caster with name '#{caster_name}' is not found"
48
48
  end
49
49
  end
50
50
 
@@ -8,7 +8,7 @@ class HCast::Casters::ArrayCaster
8
8
  value
9
9
  end
10
10
  else
11
- raise HCast::Errors::CastingError, "#{attr_name} should be an array"
11
+ raise HCast::Errors::CastingError, "should be an array"
12
12
  end
13
13
  end
14
14
 
@@ -8,7 +8,7 @@ class HCast::Casters::BooleanCaster
8
8
  elsif ['0', 'false', 'off', 0].include?(value)
9
9
  false
10
10
  else
11
- raise HCast::Errors::CastingError, "#{attr_name} should be a boolean"
11
+ raise HCast::Errors::CastingError, "should be a boolean"
12
12
  end
13
13
  end
14
14
 
@@ -7,10 +7,10 @@ class HCast::Casters::DateCaster
7
7
  begin
8
8
  Date.parse(value)
9
9
  rescue ArgumentError => e
10
- raise HCast::Errors::CastingError, "#{attr_name} is invalid date"
10
+ raise HCast::Errors::CastingError, "is invalid date"
11
11
  end
12
12
  else
13
- raise HCast::Errors::CastingError, "#{attr_name} should be a date"
13
+ raise HCast::Errors::CastingError, "should be a date"
14
14
  end
15
15
  end
16
16
 
@@ -9,10 +9,10 @@ class HCast::Casters::DateTimeCaster
9
9
  begin
10
10
  DateTime.parse(value)
11
11
  rescue ArgumentError => e
12
- raise HCast::Errors::CastingError, "#{attr_name} is invalid datetime"
12
+ raise HCast::Errors::CastingError, "is invalid datetime"
13
13
  end
14
14
  else
15
- raise HCast::Errors::CastingError, "#{attr_name} should be a datetime"
15
+ raise HCast::Errors::CastingError, "should be a datetime"
16
16
  end
17
17
  end
18
18
 
@@ -7,10 +7,10 @@ class HCast::Casters::FloatCaster
7
7
  begin
8
8
  Float(value)
9
9
  rescue ArgumentError => e
10
- raise HCast::Errors::CastingError, "#{attr_name} is invalid float"
10
+ raise HCast::Errors::CastingError, "is invalid float"
11
11
  end
12
12
  else
13
- raise HCast::Errors::CastingError, "#{attr_name} should be a float"
13
+ raise HCast::Errors::CastingError, "should be a float"
14
14
  end
15
15
  end
16
16
 
@@ -4,7 +4,7 @@ class HCast::Casters::HashCaster
4
4
  if value.is_a?(Hash)
5
5
  value
6
6
  else
7
- raise HCast::Errors::CastingError, "#{attr_name} should be a hash"
7
+ raise HCast::Errors::CastingError, "should be a hash"
8
8
  end
9
9
  end
10
10
 
@@ -7,10 +7,10 @@ class HCast::Casters::IntegerCaster
7
7
  begin
8
8
  Integer(value)
9
9
  rescue ArgumentError => e
10
- raise HCast::Errors::CastingError, "#{attr_name} is invalid integer"
10
+ raise HCast::Errors::CastingError, "is invalid integer"
11
11
  end
12
12
  else
13
- raise HCast::Errors::CastingError, "#{attr_name} should be a integer"
13
+ raise HCast::Errors::CastingError, "should be a integer"
14
14
  end
15
15
  end
16
16
 
@@ -6,7 +6,7 @@ class HCast::Casters::StringCaster
6
6
  elsif value.is_a?(Symbol)
7
7
  value.to_s
8
8
  else
9
- raise HCast::Errors::CastingError, "#{attr_name} should be a string"
9
+ raise HCast::Errors::CastingError, "should be a string"
10
10
  end
11
11
  end
12
12
 
@@ -6,12 +6,12 @@ class HCast::Casters::SymbolCaster
6
6
  value
7
7
  elsif value.is_a?(String)
8
8
  if value.length > MAX_SYMBOL_LENGTH
9
- raise HCast::Errors::CastingError, "#{attr_name} is too long to be a symbol"
9
+ raise HCast::Errors::CastingError, "is too long to be a symbol"
10
10
  else
11
11
  value.to_sym
12
12
  end
13
13
  else
14
- raise HCast::Errors::CastingError, "#{attr_name} should be a symbol"
14
+ raise HCast::Errors::CastingError, "should be a symbol"
15
15
  end
16
16
  end
17
17
 
@@ -7,10 +7,10 @@ class HCast::Casters::TimeCaster
7
7
  begin
8
8
  Time.parse(value)
9
9
  rescue ArgumentError => e
10
- raise HCast::Errors::CastingError, "#{attr_name} is invalid time"
10
+ raise HCast::Errors::CastingError, "is invalid time"
11
11
  end
12
12
  else
13
- raise HCast::Errors::CastingError, "#{attr_name} should be a time"
13
+ raise HCast::Errors::CastingError, "should be a time"
14
14
  end
15
15
  end
16
16
 
@@ -9,14 +9,43 @@ module HCast::Errors
9
9
  # Raised when some of the given to HCast argument is not valid
10
10
  class ArgumentError < HCastError; end
11
11
 
12
+ class AttributeError < HCastError
13
+ attr_reader :namespaces
14
+
15
+ def initialize(message, namespace = nil)
16
+ super(message)
17
+ @namespaces = []
18
+ @namespaces << namespace if namespace
19
+ end
20
+
21
+ def add_namespace(namespace)
22
+ namespaces << namespace
23
+ end
24
+
25
+ def message
26
+ to_s
27
+ end
28
+
29
+ def to_s
30
+ if namespaces.empty?
31
+ super
32
+ else
33
+ reverted_namespaces = namespaces.reverse
34
+ msg = reverted_namespaces.first.to_s
35
+ msg += reverted_namespaces[1..-1].inject("") { |res, item| res += "[#{item}]"}
36
+ msg + " " + super
37
+ end
38
+ end
39
+
40
+ end
12
41
  # Raised when hash attribute can't be casted
13
- class CastingError < HCastError; end
42
+ class CastingError < AttributeError; end
14
43
 
15
44
  # Raised when required hash attribute wasn't given for casting
16
- class MissingAttributeError < HCastError; end
45
+ class MissingAttributeError < AttributeError; end
17
46
 
18
47
  # Raised when unexpected hash attribute was given for casting
19
- class UnexpectedAttributeError < HCastError; end
48
+ class UnexpectedAttributeError < AttributeError; end
20
49
 
21
50
  # Raised when hash has validation errors
22
51
  class ValidationError < StandardError
@@ -1,3 +1,3 @@
1
1
  module HCast
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -108,7 +108,7 @@ describe HCast::Caster do
108
108
 
109
109
  expect do
110
110
  ContactCaster.cast(input_hash)
111
- end.to raise_error(HCast::Errors::CastingError, "name should be a string")
111
+ end.to raise_error(HCast::Errors::CastingError, "contact[name] should be a string")
112
112
  end
113
113
 
114
114
  it "should raise error if some attribute wasn't given" do
@@ -138,7 +138,7 @@ describe HCast::Caster do
138
138
 
139
139
  expect do
140
140
  ContactCaster.cast(input_hash)
141
- end.to raise_error(HCast::Errors::MissingAttributeError, "name should be given")
141
+ end.to raise_error(HCast::Errors::MissingAttributeError, "contact[name] should be given")
142
142
  end
143
143
 
144
144
  it "should not raise error if attribute is optional" do
@@ -173,8 +173,8 @@ describe HCast::Caster do
173
173
 
174
174
  it "should raise error if unexpected attribute was given" do
175
175
  input_hash = {
176
- wrong_attribute: 'foo',
177
176
  contact: {
177
+ wrong_attribute: 'foo',
178
178
  name: "Jim",
179
179
  weight: 65.5,
180
180
  birthday: Date.today,
@@ -199,7 +199,7 @@ describe HCast::Caster do
199
199
 
200
200
  expect do
201
201
  ContactCaster.cast(input_hash)
202
- end.to raise_error(HCast::Errors::UnexpectedAttributeError, "Unexpected attributes given: [:wrong_attribute]")
202
+ end.to raise_error(HCast::Errors::UnexpectedAttributeError, "contact[wrong_attribute] is not valid attribute name")
203
203
  end
204
204
 
205
205
  it "should convert accept hash with string keys and cast them to symbol keys" do
metadata CHANGED
@@ -1,48 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hcast
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.1.0
4
+ version: 0.1.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Albert Gazizov
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-04-08 00:00:00.000000000 Z
11
+ date: 2014-09-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- prerelease: false
16
- name: bundler
17
14
  type: :development
18
- version_requirements: !ruby/object:Gem::Requirement
15
+ requirement: !ruby/object:Gem::Requirement
19
16
  requirements:
20
17
  - - ~>
21
18
  - !ruby/object:Gem::Version
22
19
  version: '1.3'
23
- none: false
24
- requirement: !ruby/object:Gem::Requirement
20
+ name: bundler
21
+ version_requirements: !ruby/object:Gem::Requirement
25
22
  requirements:
26
23
  - - ~>
27
24
  - !ruby/object:Gem::Version
28
25
  version: '1.3'
29
- none: false
30
- - !ruby/object:Gem::Dependency
31
26
  prerelease: false
32
- name: rake
27
+ - !ruby/object:Gem::Dependency
33
28
  type: :development
34
- version_requirements: !ruby/object:Gem::Requirement
29
+ requirement: !ruby/object:Gem::Requirement
35
30
  requirements:
36
31
  - - ! '>='
37
32
  - !ruby/object:Gem::Version
38
33
  version: '0'
39
- none: false
40
- requirement: !ruby/object:Gem::Requirement
34
+ name: rake
35
+ version_requirements: !ruby/object:Gem::Requirement
41
36
  requirements:
42
37
  - - ! '>='
43
38
  - !ruby/object:Gem::Version
44
39
  version: '0'
45
- none: false
40
+ prerelease: false
46
41
  description: Hash Caster and Validator
47
42
  email:
48
43
  - deeper4k@gmail.com
@@ -84,6 +79,7 @@ files:
84
79
  homepage: http://github.com/AlbertGazizov/hcast
85
80
  licenses:
86
81
  - MIT
82
+ metadata: {}
87
83
  post_install_message:
88
84
  rdoc_options: []
89
85
  require_paths:
@@ -92,25 +88,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
88
  requirements:
93
89
  - - ! '>='
94
90
  - !ruby/object:Gem::Version
95
- segments:
96
- - 0
97
- hash: 3528154238926352388
98
91
  version: '0'
99
- none: false
100
92
  required_rubygems_version: !ruby/object:Gem::Requirement
101
93
  requirements:
102
94
  - - ! '>='
103
95
  - !ruby/object:Gem::Version
104
- segments:
105
- - 0
106
- hash: 3528154238926352388
107
96
  version: '0'
108
- none: false
109
97
  requirements: []
110
98
  rubyforge_project:
111
- rubygems_version: 1.8.23
99
+ rubygems_version: 2.4.1
112
100
  signing_key:
113
- specification_version: 3
101
+ specification_version: 4
114
102
  summary: Hash Caster and Validator
115
103
  test_files:
116
104
  - spec/hcast/caster_spec.rb