rest_model 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ class Root < RestModel
6
6
  property :key, id: true
7
7
  property :locale, visible: false
8
8
  embeds_many :names, class_name: 'Hash', visible: false
9
- property :name, values: proc {names[locale]}
9
+ property :name, from_source: proc {names[locale]}, to_source: proc {names[locale]}
10
10
  end
11
11
 
12
12
  names = {'en' => 'Woot', 'pt-BR' => 'Úia', 'es' => 'Me gusta'}
@@ -3,22 +3,12 @@ class RestModel
3
3
  module Retriever
4
4
  def from_source(item, resource = nil)
5
5
  value = digg(item)
6
- translate_from_source(serializer.serialize(value), resource)
6
+ translation.translate_from_source(serializer.serialize(value), resource)
7
7
  end
8
8
 
9
9
  def digg(input)
10
10
  source_path.inject(input) {|buffer, key| buffer = buffer[key]}
11
11
  end
12
-
13
- def translate_from_source(value, resource)
14
- case translations
15
- when nil then value
16
- when Proc then resource.instance_eval(&translations)
17
- when Hash
18
- fail "from input error" unless translations.has_value?(value)
19
- translations.key(value)
20
- end
21
- end
22
12
  end
23
13
  end
24
14
  end
@@ -2,14 +2,8 @@ class RestModel
2
2
  class Property
3
3
  module Sender
4
4
  def to_source!(value, resource, options = {})
5
- source_value = nil
6
-
7
- begin
8
- if translations
9
- source_value = translate_to_source(value, resource)
10
- else
11
- source_value = serializer.desserialize(value)
12
- end
5
+ source_value = begin
6
+ translation.translate_to_source(serializer.desserialize(value), resource)
13
7
  rescue => exception
14
8
  raise exception if options[:fail]
15
9
  end
@@ -27,15 +21,6 @@ class RestModel
27
21
 
28
22
  source
29
23
  end
30
-
31
- def translate_to_source(value, resource)
32
- case translations
33
- when Proc then resource.instance_eval(&translations)
34
- when Hash
35
- fail "to input error" unless translations.has_key?(value)
36
- translations[value]
37
- end
38
- end
39
24
  end
40
25
  end
41
26
  end
@@ -7,12 +7,12 @@ class RestModel
7
7
  include Retriever
8
8
  include Response
9
9
 
10
- attr_accessor :serializer, :translations
10
+ attr_accessor :serializer, :translation
11
11
 
12
12
  def initialize(name, options = {})
13
13
  super
14
- @serializer = options.fetch(:type, Serialization::String)
15
- @translations = options[:values]
14
+ @serializer = options.fetch(:type, Serialization::String)
15
+ @translation = Source::Translation.new(options)
16
16
  end
17
17
 
18
18
  def from_hash(value, resource = nil)
@@ -0,0 +1,35 @@
1
+ class RestModel
2
+ module Source
3
+ class Translation
4
+ attr_accessor :values, :from_source, :to_source
5
+
6
+ def initialize(attrs = {})
7
+ @values = attrs[:values]
8
+ @from_source = attrs[:from_source]
9
+ @to_source = attrs[:to_source]
10
+ end
11
+
12
+ def translate_from_source(value, resource)
13
+ if from_source
14
+ resource.instance_exec(value, &from_source)
15
+ elsif values
16
+ fail "from source error" unless values.has_value?(value)
17
+ values.key(value)
18
+ else
19
+ value
20
+ end
21
+ end
22
+
23
+ def translate_to_source(value, resource)
24
+ if to_source
25
+ resource.instance_exec(value, &to_source)
26
+ elsif values
27
+ fail "to source error" unless values.has_key?(value)
28
+ values[value]
29
+ else
30
+ value
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  class RestModel
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/rest_model.rb CHANGED
@@ -9,6 +9,7 @@ require "active_support/core_ext/hash/slice"
9
9
  require "active_support/core_ext/hash/indifferent_access"
10
10
 
11
11
  require "rest_model/source/path"
12
+ require "rest_model/source/translation"
12
13
  require "rest_model/source/retriever"
13
14
  require "rest_model/source/sender"
14
15
  require "rest_model/response"
@@ -32,7 +32,8 @@ describe RestModel::Property do
32
32
  end
33
33
 
34
34
  it "configures values" do
35
- subject.translations.should == values
35
+ subject.translation.should be_a(RestModel::Source::Translation)
36
+ subject.translation.values.should == values
36
37
  end
37
38
  end
38
39
  end
@@ -72,13 +72,21 @@ describe RestModel::Source::Retriever do
72
72
  end
73
73
 
74
74
  it "returns key when value was mapped" do
75
- models = Example.parse({status: '02'})
75
+ models = Example.from_source!({status: '02'})
76
76
  models.first.status.should == :unpaid
77
77
  end
78
78
 
79
- it "returns nil when value wasn't mapped" do
80
- models = Example.parse({status: '03'})
81
- models.first.status.should be_nil
79
+ context "when calling from_source" do
80
+ it "returns nil when value wasn't mapped" do
81
+ models = Example.from_source({status: '03'})
82
+ models.first.status.should be_nil
83
+ end
84
+ end
85
+
86
+ context "when calling from_source!" do
87
+ it "fails when value wasn't mapped" do
88
+ expect {Example.from_source!({status: '03'})}.to raise_error("from source error")
89
+ end
82
90
  end
83
91
  end
84
92
  end
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.4
4
+ version: 0.1.5
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-10-20 00:00:00.000000000Z
14
+ date: 2011-10-24 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
18
- requirement: &70223287802460 !ruby/object:Gem::Requirement
18
+ requirement: &70331537727740 !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: *70223287802460
26
+ version_requirements: *70331537727740
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: i18n
29
- requirement: &70223287801960 !ruby/object:Gem::Requirement
29
+ requirement: &70331537727160 !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: *70223287801960
37
+ version_requirements: *70331537727160
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: rspec
40
- requirement: &70223287801500 !ruby/object:Gem::Requirement
40
+ requirement: &70331537726680 !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: *70223287801500
48
+ version_requirements: *70331537726680
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: guard
51
- requirement: &70223287801040 !ruby/object:Gem::Requirement
51
+ requirement: &70331537726100 !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: *70223287801040
59
+ version_requirements: *70331537726100
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: guard-rspec
62
- requirement: &70223287800580 !ruby/object:Gem::Requirement
62
+ requirement: &70331537725580 !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: *70223287800580
70
+ version_requirements: *70331537725580
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: growl
73
- requirement: &70223287800120 !ruby/object:Gem::Requirement
73
+ requirement: &70331537725060 !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: *70223287800120
81
+ version_requirements: *70331537725060
82
82
  description: ''
83
83
  email:
84
84
  - victorcrodrigues@gmail.com
@@ -171,6 +171,7 @@ files:
171
171
  - lib/rest_model/source/path.rb
172
172
  - lib/rest_model/source/retriever.rb
173
173
  - lib/rest_model/source/sender.rb
174
+ - lib/rest_model/source/translation.rb
174
175
  - lib/rest_model/version.rb
175
176
  - rest_model.gemspec
176
177
  - spec/.DS_Store