transcriber 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ $:.push 'lib'; require 'transcriber'
2
+
3
+ class Customer < Transcriber::Resource
4
+ property :login, field: 'hidden.login'
5
+ embeds_many :phones
6
+ end
7
+
8
+ class Phone < Transcriber::Resource
9
+ properties :number, :extension
10
+ end
11
+
12
+ @root = Customer.new({
13
+ login: 'jackiechan2010',
14
+ phones: [
15
+ {
16
+ number: "123123123",
17
+ extension: "111"
18
+ },
19
+ {
20
+ number: "098098098",
21
+ extension: "999"
22
+ }
23
+ ]})
24
+
25
+ puts "root.to_input: #{@root.to_input}"
26
+
27
+
28
+ puts "root: #{@root.inspect}"
29
+ puts "resource: #{@root.resource}"
@@ -0,0 +1,18 @@
1
+ $:.push 'lib'; require 'transcriber'
2
+
3
+ class Customer < Transcriber::Resource
4
+ property :login, field: 'hidden.login'
5
+ embeds_one :address
6
+ end
7
+
8
+ class Address < Transcriber::Resource
9
+ properties :street, :number
10
+ end
11
+
12
+ @root = Customer.new(login: 'jackiechan2010', address: {street: "Aurora St", number: 666})
13
+
14
+ puts "root.to_input: #{@root.to_input}"
15
+
16
+
17
+ puts "root: #{@root.inspect}"
18
+ puts "resource: #{@root.resource}"
@@ -0,0 +1,15 @@
1
+ $:.push 'lib'; require 'transcriber'
2
+
3
+ class Root < Transcriber::Resource
4
+ embeds_one :item, start_key: ""
5
+ end
6
+
7
+ class Item < Transcriber::Resource
8
+ property :id
9
+ end
10
+
11
+ @root = Root.new(item: {id: 2000})
12
+
13
+ puts "root.to_input: #{@root.to_input}"
14
+ puts "root: #{@root.inspect}"
15
+ puts "resource: #{@root.resource}"
@@ -0,0 +1,12 @@
1
+ $:.push 'lib'; require 'transcriber'
2
+
3
+ class Customer < Transcriber::Resource
4
+ property :login
5
+ end
6
+
7
+ @root = Customer.new(login: 'jackiechan2010')
8
+
9
+ puts "root.to_input: #{@root.to_input}"
10
+
11
+ puts "root: #{@root.inspect}"
12
+ puts "resource: #{@root.resource}"
@@ -0,0 +1,12 @@
1
+ $:.push 'lib'; require 'transcriber'
2
+
3
+ class Customer < Transcriber::Resource
4
+ property :login, field: 'customer_login'
5
+ end
6
+
7
+ @root = Customer.new(login: 'jackiechan2010')
8
+
9
+ puts "root.to_input: #{@root.to_input}"
10
+
11
+ puts "root: #{@root.inspect}"
12
+ puts "resource: #{@root.resource}"
@@ -0,0 +1,13 @@
1
+ $:.push 'lib'; require 'transcriber'
2
+
3
+ class Customer < Transcriber::Resource
4
+ property :login, field: 'hidden.login'
5
+ end
6
+
7
+ @root = Customer.new(login: 'jackiechan2010')
8
+
9
+ puts "root.to_input: #{@root.to_input}"
10
+
11
+
12
+ puts "root: #{@root.inspect}"
13
+ puts "resource: #{@root.resource}"
data/lib/transcriber.rb CHANGED
@@ -5,6 +5,7 @@ require 'active_support/core_ext/class/attribute_accessors'
5
5
  require 'active_support/core_ext/object/try'
6
6
  require 'active_support/core_ext/array/wrap'
7
7
  require 'active_support/core_ext/hash/slice'
8
+ require 'active_support/hash_with_indifferent_access'
8
9
 
9
10
  require 'transcriber/resource/input_path'
10
11
  require 'transcriber/resource/parser'
@@ -12,7 +12,7 @@ module Transcriber
12
12
  return if attrs.nil? or attrs.empty?
13
13
 
14
14
  self.class.keys.each do |key|
15
- __send__("#{key.name}=", key.from_hash(attrs[key.name])) if key.present? self
15
+ __send__("#{key.name}=", key.from_hash(attrs[key.name])) if key.present?(self)
16
16
  end
17
17
  end
18
18
 
@@ -49,5 +49,23 @@ module Transcriber
49
49
  def self.not_allowed_names
50
50
  %w(resource_id resource link)
51
51
  end
52
+
53
+ def to_input
54
+ input = {}
55
+ self.class.keys.each do |key|
56
+ value = __send__("#{key.name}")
57
+ parsed_value = key.to_input(value)
58
+ path = key.input_path
59
+
60
+ if path.any?
61
+ last = path.pop
62
+ key_input = path.inject(input) {|buffer, key| buffer[key] = {}; buffer[key]}
63
+ key_input[last] = parsed_value
64
+ else
65
+ input.merge!(parsed_value)
66
+ end
67
+ end
68
+ input
69
+ end
52
70
  end
53
71
  end
@@ -26,7 +26,7 @@ module Transcriber
26
26
  end
27
27
 
28
28
  def input_path
29
- return @input_path if @input_path
29
+ return @input_path.clone if @input_path
30
30
 
31
31
  path = InputPath.resolve(options, convert_input_keys)
32
32
 
@@ -35,6 +35,7 @@ module Transcriber
35
35
  end
36
36
 
37
37
  @input_path = path
38
+ @input_path.clone
38
39
  end
39
40
 
40
41
  def root_path?
@@ -30,6 +30,11 @@ module Transcriber
30
30
  one? ? resource_class.new(attrs)
31
31
  : Array(attrs).map {|item| resource_class.new(item)}
32
32
  end
33
+
34
+ def to_input(value)
35
+ return nil if value.nil?
36
+ one? ? value.to_input : value.map(&:to_input)
37
+ end
33
38
  end
34
39
  end
35
40
  end
@@ -12,6 +12,11 @@ module Transcriber
12
12
  return hash if raw?
13
13
  super
14
14
  end
15
+
16
+ def to_input(value)
17
+ return value if raw?
18
+ super
19
+ end
15
20
  end
16
21
  end
17
22
  end
@@ -17,6 +17,10 @@ module Transcriber
17
17
  def from_hash(value)
18
18
  value
19
19
  end
20
+
21
+ def to_input(value)
22
+ value
23
+ end
20
24
  end
21
25
  end
22
26
  end
@@ -2,7 +2,7 @@ module Transcriber
2
2
  class Resource
3
3
  module Parser
4
4
  def parse!(input, options = {})
5
- parse(input, options.merge(fail: true))
5
+ parse(input.with_different_access, options.merge(fail: true))
6
6
  end
7
7
 
8
8
  def parse(input, options = {})
@@ -1,3 +1,3 @@
1
1
  module Transcriber
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
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.10
4
+ version: 0.0.11
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-08-29 00:00:00.000000000Z
14
+ date: 2011-08-30 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
18
- requirement: &70248985347520 !ruby/object:Gem::Requirement
18
+ requirement: &70143490997700 !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: *70248985347520
26
+ version_requirements: *70143490997700
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: i18n
29
- requirement: &70248985346960 !ruby/object:Gem::Requirement
29
+ requirement: &70143490997140 !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: *70248985346960
37
+ version_requirements: *70143490997140
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: rspec
40
- requirement: &70248985346440 !ruby/object:Gem::Requirement
40
+ requirement: &70143490996560 !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: *70248985346440
48
+ version_requirements: *70143490996560
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: guard
51
- requirement: &70248985345860 !ruby/object:Gem::Requirement
51
+ requirement: &70143490995980 !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: *70248985345860
59
+ version_requirements: *70143490995980
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: guard-rspec
62
- requirement: &70248985345280 !ruby/object:Gem::Requirement
62
+ requirement: &70143490995400 !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: *70248985345280
70
+ version_requirements: *70143490995400
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: growl
73
- requirement: &70248985344720 !ruby/object:Gem::Requirement
73
+ requirement: &70143490994840 !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: *70248985344720
81
+ version_requirements: *70143490994840
82
82
  description: ''
83
83
  email:
84
84
  - victorcrodrigues@gmail.com
@@ -120,6 +120,12 @@ files:
120
120
  - examples/properties/with_key_converter.rb
121
121
  - examples/properties/with_two_key_converters.rb
122
122
  - examples/summarization/simple.rb
123
+ - examples/to_input/embeds_many.rb
124
+ - examples/to_input/embeds_one.rb
125
+ - examples/to_input/flattened.rb
126
+ - examples/to_input/simple.rb
127
+ - examples/to_input/with_field.rb
128
+ - examples/to_input/with_field_path.rb
123
129
  - lib/transcriber.rb
124
130
  - lib/transcriber/configuration.rb
125
131
  - lib/transcriber/resource.rb