rest_model 0.1.22 → 0.1.23

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,6 +15,7 @@ class Customer < RestModel
15
15
  property :active, type: Boolean
16
16
  property :services, type: Enumerable
17
17
  property :balance, type: Float
18
+ property :sym, type: Symbol
18
19
  end
19
20
 
20
21
  @root = Customer.new(login: 'jackiechan2010',
@@ -23,6 +24,7 @@ end
23
24
  created_at: DateTime.now,
24
25
  active: true,
25
26
  services: ["Hosting", "Email"],
26
- balance: 200.00)
27
+ balance: 200.00,
28
+ sym: :some_symbol)
27
29
 
28
30
  inspect_rest_model(@root)
@@ -19,7 +19,8 @@ class Service < RestModel
19
19
  end
20
20
 
21
21
  class Product < RestModel
22
- property :value, type: Float
22
+ VALUES = [10.00, 20.00, 25.50]
23
+ property :value, type: Float, values: VALUES
23
24
  property :created_at, type: DateTime
24
25
  end
25
26
 
@@ -32,6 +33,10 @@ products = [
32
33
  {
33
34
  value: "non float (P1)",
34
35
  created_at: "2011-11-11T11:11:11"
36
+ },
37
+ {
38
+ value: 20.00,
39
+ created_at: "2011-11-11T11:11:11"
35
40
  }
36
41
  ]
37
42
 
@@ -0,0 +1,15 @@
1
+ class RestModel
2
+ module Serialization
3
+ class Symbol
4
+ def self.serialize(value)
5
+ value.to_s.to_sym
6
+ rescue
7
+ raise SerializationError, "value '#{value}' is not a symbol"
8
+ end
9
+
10
+ def self.desserialize(value)
11
+ serialize(value)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -17,11 +17,7 @@ class RestModel
17
17
  if from_source
18
18
  resource.instance_exec(value, &from_source)
19
19
  elsif values
20
- unless values.has_value?(value)
21
- fail TranslationError, "value '#{value}' doesn't belong to values: #{values.values}"
22
- end
23
-
24
- values.key(value)
20
+ translate_from_source_values(value)
25
21
  else
26
22
  value
27
23
  end
@@ -35,13 +31,47 @@ class RestModel
35
31
  if to_source
36
32
  resource.instance_exec(value, &to_source)
37
33
  elsif values
34
+ translate_to_source_values(value)
35
+ else
36
+ value
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def translate_from_source_values(value)
43
+ if values.kind_of?(Hash)
44
+ unless values.has_value?(value)
45
+ fail TranslationError, "value '#{value}' doesn't belong to values: #{values.values}"
46
+ end
47
+
48
+ values.key(value)
49
+ elsif values.kind_of?(Array)
50
+ unless values.member?(value)
51
+ fail TranslationError, "value '#{value}' doesn't belong to values in: #{values}"
52
+ end
53
+
54
+ value
55
+ else
56
+ fail TranslationError, "not supported values"
57
+ end
58
+ end
59
+
60
+ def translate_to_source_values(value)
61
+ if values.kind_of?(Hash)
38
62
  unless values.has_key?(value)
39
63
  fail TranslationError, "value '#{value}' doesn't belong to values: #{values.keys}"
40
64
  end
41
65
 
42
66
  values[value]
43
- else
67
+ elsif values.kind_of?(Array)
68
+ unless values.member?(value)
69
+ fail TranslationError, "value '#{value}' doesn't belong to values in: #{values}"
70
+ end
71
+
44
72
  value
73
+ else
74
+ fail TranslationError, "not supported values"
45
75
  end
46
76
  end
47
77
  end
@@ -1,3 +1,3 @@
1
1
  class RestModel
2
- VERSION = "0.1.22"
2
+ VERSION = "0.1.23"
3
3
  end
data/lib/rest_model.rb CHANGED
@@ -20,6 +20,7 @@ require "rest_model/serialization/enumerable"
20
20
  require "rest_model/serialization/float"
21
21
  require "rest_model/serialization/integer"
22
22
  require "rest_model/serialization/string"
23
+ require "rest_model/serialization/symbol"
23
24
  require "rest_model/key"
24
25
  require "rest_model/key/property"
25
26
  require "rest_model/key/property/builder"
@@ -44,6 +44,7 @@ describe "to_source" do
44
44
  source["active"].should == "X"
45
45
  source["services"].should == ["Hosting", "Email"]
46
46
  source["balance"].should == 200.00
47
+ source["sym"].should == :some_symbol
47
48
  end
48
49
  end
49
50
  end
@@ -11,10 +11,11 @@ describe "validations" do
11
11
  code: "value 'non integer (S0)' is not an integer",
12
12
  products: {
13
13
  0 => {
14
+ value: "value '100.0' doesn't belong to values in: [10.0, 20.0, 25.5]",
14
15
  created_at: "value 'non datetime (P0)' is an invalid date time"
15
16
  },
16
17
  1 => {
17
- value: "value 'non float (P1)' is not a float"
18
+ value: "value 'non float (P1)' doesn't belong to values in: [10.0, 20.0, 25.5]"
18
19
  }
19
20
  }
20
21
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.22
4
+ version: 0.1.23
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,11 +11,11 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2011-12-18 00:00:00.000000000Z
14
+ date: 2011-12-21 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
18
- requirement: &70283492911180 !ruby/object:Gem::Requirement
18
+ requirement: &70269236026780 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ~>
@@ -23,10 +23,10 @@ dependencies:
23
23
  version: '3.0'
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *70283492911180
26
+ version_requirements: *70269236026780
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: i18n
29
- requirement: &70283492910680 !ruby/object:Gem::Requirement
29
+ requirement: &70269236026280 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ! '>='
@@ -34,10 +34,10 @@ dependencies:
34
34
  version: '0.5'
35
35
  type: :runtime
36
36
  prerelease: false
37
- version_requirements: *70283492910680
37
+ version_requirements: *70269236026280
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: rspec
40
- requirement: &70283492910220 !ruby/object:Gem::Requirement
40
+ requirement: &70269236025820 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ~>
@@ -45,10 +45,10 @@ dependencies:
45
45
  version: '2.6'
46
46
  type: :development
47
47
  prerelease: false
48
- version_requirements: *70283492910220
48
+ version_requirements: *70269236025820
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: guard
51
- requirement: &70283492909760 !ruby/object:Gem::Requirement
51
+ requirement: &70269236025360 !ruby/object:Gem::Requirement
52
52
  none: false
53
53
  requirements:
54
54
  - - ~>
@@ -56,10 +56,10 @@ dependencies:
56
56
  version: '0.5'
57
57
  type: :development
58
58
  prerelease: false
59
- version_requirements: *70283492909760
59
+ version_requirements: *70269236025360
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: guard-rspec
62
- requirement: &70283492909300 !ruby/object:Gem::Requirement
62
+ requirement: &70269227073320 !ruby/object:Gem::Requirement
63
63
  none: false
64
64
  requirements:
65
65
  - - ~>
@@ -67,10 +67,10 @@ dependencies:
67
67
  version: '0.4'
68
68
  type: :development
69
69
  prerelease: false
70
- version_requirements: *70283492909300
70
+ version_requirements: *70269227073320
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: growl
73
- requirement: &70283492908840 !ruby/object:Gem::Requirement
73
+ requirement: &70269227072860 !ruby/object:Gem::Requirement
74
74
  none: false
75
75
  requirements:
76
76
  - - ~>
@@ -78,7 +78,7 @@ dependencies:
78
78
  version: '1.0'
79
79
  type: :development
80
80
  prerelease: false
81
- version_requirements: *70283492908840
81
+ version_requirements: *70269227072860
82
82
  description: ''
83
83
  email:
84
84
  - victorcrodrigues@gmail.com
@@ -179,6 +179,7 @@ files:
179
179
  - lib/rest_model/serialization/float.rb
180
180
  - lib/rest_model/serialization/integer.rb
181
181
  - lib/rest_model/serialization/string.rb
182
+ - lib/rest_model/serialization/symbol.rb
182
183
  - lib/rest_model/source/path.rb
183
184
  - lib/rest_model/source/retriever.rb
184
185
  - lib/rest_model/source/sender.rb