transcriber 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,10 +3,12 @@ module Transcriber
3
3
  module InputParser
4
4
  def to_input(options = {})
5
5
  input = {}
6
+
6
7
  keys_to_input(options).each do |key|
7
8
  value = __send__(key.name)
8
9
  input.merge! key.to_input(value, options.fetch(key.name, {}))
9
10
  end
11
+
10
12
  input.with_indifferent_access
11
13
  end
12
14
 
@@ -8,16 +8,9 @@ module Transcriber
8
8
  @options = options
9
9
  end
10
10
 
11
- def present?(resource)
12
- !options[:if] ? true : resource.instance_eval(&options[:if])
13
- end
14
-
15
- def visible?(resource)
16
- !present?(resource) ? false : case visible = options[:visible]
17
- when nil then true
18
- when Proc then resource.instance_eval(&visible)
19
- else visible
20
- end
11
+ def convert_input_keys
12
+ options.fetch(:convert_input_keys, model.try(:convert_input_keys) ||
13
+ Transcriber.configuration.convert_input_keys)
21
14
  end
22
15
 
23
16
  def input_path
@@ -37,9 +30,16 @@ module Transcriber
37
30
  path_definition.any? and path_definition.first[1].empty?
38
31
  end
39
32
 
40
- def convert_input_keys
41
- options.fetch(:convert_input_keys, model.try(:convert_input_keys) ||
42
- Transcriber.configuration.convert_input_keys)
33
+ def present?(resource)
34
+ !options[:if] ? true : resource.instance_eval(&options[:if])
35
+ end
36
+
37
+ def visible?(resource)
38
+ !present?(resource) ? false : case visible = options[:visible]
39
+ when nil then true
40
+ when Proc then resource.instance_eval(&visible)
41
+ else visible
42
+ end
43
43
  end
44
44
  end
45
45
  end
@@ -2,10 +2,13 @@ module Transcriber
2
2
  class Resource
3
3
  module Parser
4
4
  def parse!(input, options = {})
5
+ fail "invalid input" unless input.kind_of?(Enumerable)
5
6
  parse(input, options.merge(fail: true))
6
7
  end
7
8
 
8
9
  def parse(input, options = {})
10
+ return [] unless input.kind_of?(Enumerable)
11
+
9
12
  prepare_entries(input, options).collect do |item|
10
13
  parse_item(HashWithIndifferentAccess.new(item), options)
11
14
  end
@@ -3,14 +3,14 @@ module Transcriber
3
3
  module Parser
4
4
  module Embeddable
5
5
  def parse(item, resource = nil)
6
- value = nil
7
- if fields
8
- value = convert_input_keys.call(fields).map {|field| item[field]}
9
- else
10
- value = digg(item)
11
- parsed_value = raw? ? value : resource_class.parse(value)
12
- value = one? ? parsed_value.first : parsed_value
13
- end
6
+ return parse_fields(item) if fields
7
+ value = digg(item)
8
+ parsed_value = raw? ? value : resource_class.parse(value)
9
+ one? ? parsed_value.first : parsed_value
10
+ end
11
+
12
+ def parse_fields(item)
13
+ convert_input_keys.call(fields).map {|field| item[field]}
14
14
  end
15
15
 
16
16
  def digg(input)
@@ -22,33 +22,40 @@ module Transcriber
22
22
  end
23
23
 
24
24
  def to_input(value, options = {})
25
- input = {}
26
- return input if value.nil?
25
+ return {} if value.nil?
26
+ fields ? to_multiple_input(value, options)
27
+ : to_single_input(value)
28
+ end
27
29
 
30
+ private
31
+
32
+ def to_single_input(value)
33
+ input = {}
28
34
  path = input_path
29
35
 
30
36
  if path.any?
31
37
  last = path.pop
32
38
  key_input = path.inject(input) {|buffer, key| buffer[key] = {}; buffer[key]}
33
-
34
- if fields
35
- mapped_fields = convert_input_keys.call(fields)
36
- value.each_with_index do |item, index|
37
- input[mapped_fields[index]] = item
38
- end
39
- elsif raw?
40
- key_input[last] = value
41
- elsif one?
42
- key_input[last] = value.to_input(options)
43
- else
44
- key_input[last] = value.map {|item| item.to_input(options)}
45
- end
39
+ key_input[last] = raw? ? value
40
+ : one? ? value.to_input(options)
41
+ : value.map {|item| item.to_input(options)}
46
42
  else
47
43
  input.merge!(value.to_input(options))
48
44
  end
49
45
 
50
46
  input
51
47
  end
48
+
49
+ def to_multiple_input(value, options)
50
+ input = {}
51
+ mapped_fields = convert_input_keys.call(fields)
52
+
53
+ value.each_with_index do |item, index|
54
+ input[mapped_fields[index]] = item
55
+ end
56
+
57
+ input
58
+ end
52
59
  end
53
60
  end
54
61
  end
@@ -27,6 +27,7 @@ module Transcriber
27
27
  input = {}
28
28
 
29
29
  path = input_path
30
+
30
31
  if path.any?
31
32
  last = path.pop
32
33
  key_input = path.inject(input) {|buffer, key| buffer[key] = {}; buffer[key]}
@@ -5,10 +5,12 @@ module Transcriber
5
5
 
6
6
  def resource(options = {})
7
7
  root = options.fetch(:root, true)
8
+
8
9
  {}.tap do |resource|
9
10
  resource_keys(options).inject(resource) do |buffer, key|
10
11
  buffer.merge!(key.to_resource(self))
11
12
  end
13
+
12
14
  resource.merge!({link: link}) if root and self.class.relations.any?
13
15
  end
14
16
  end
@@ -18,8 +20,8 @@ module Transcriber
18
20
  end
19
21
 
20
22
  def resource_keys(options)
21
- return self.class.keys unless summarize?(options)
22
- self.class.summarized_keys + [Resource::Href.new]
23
+ summarize?(options) ? self.class.summarized_keys + [Resource::Href.new]
24
+ : self.class.keys
23
25
  end
24
26
 
25
27
  def summarize?(options)
@@ -4,7 +4,6 @@ module Transcriber
4
4
  module Embeddable
5
5
  def to_resource(parent)
6
6
  return {} unless visible?(parent)
7
-
8
7
  resource = nil
9
8
 
10
9
  if embedded = parent.__send__(name)
@@ -1,3 +1,3 @@
1
1
  module Transcriber
2
- VERSION = "0.0.17"
2
+ VERSION = "0.0.18"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transcriber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
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-09-12 00:00:00.000000000Z
14
+ date: 2011-09-20 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
18
- requirement: &70190180161240 !ruby/object:Gem::Requirement
18
+ requirement: &70227590133480 !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: *70190180161240
26
+ version_requirements: *70227590133480
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: i18n
29
- requirement: &70190180160520 !ruby/object:Gem::Requirement
29
+ requirement: &70227590132980 !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: *70190180160520
37
+ version_requirements: *70227590132980
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: rspec
40
- requirement: &70190180160000 !ruby/object:Gem::Requirement
40
+ requirement: &70227590132520 !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: *70190180160000
48
+ version_requirements: *70227590132520
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: guard
51
- requirement: &70190180159300 !ruby/object:Gem::Requirement
51
+ requirement: &70227590132060 !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: *70190180159300
59
+ version_requirements: *70227590132060
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: guard-rspec
62
- requirement: &70190180158520 !ruby/object:Gem::Requirement
62
+ requirement: &70227590131580 !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: *70190180158520
70
+ version_requirements: *70227590131580
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: growl
73
- requirement: &70190180157940 !ruby/object:Gem::Requirement
73
+ requirement: &70227590131060 !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: *70190180157940
81
+ version_requirements: *70227590131060
82
82
  description: ''
83
83
  email:
84
84
  - victorcrodrigues@gmail.com