transcriber 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@ require 'json'
3
3
  $:.push 'lib'; require 'transcriber'
4
4
 
5
5
  class Root < Transcriber::Resource
6
- embeds_many :items, class_name: "Array"
6
+ embeds_many :items, class_name: :array
7
7
  end
8
8
 
9
9
  @root = Root.new
@@ -0,0 +1,10 @@
1
+ $:.push 'lib'; require 'transcriber'
2
+
3
+ class Root < Transcriber::Resource
4
+ embeds_many :items, fields: [:item1, :item2, :item3]
5
+ end
6
+
7
+ @root = Root.parse({item1: "i1", item2: "i2", item3: "i3"}).first
8
+
9
+ puts "root: #{@root.inspect}"
10
+ puts "resource: #{@root.resource}"
@@ -2,7 +2,7 @@ $:.push 'lib'; require 'transcriber'
2
2
 
3
3
  class Customer < Transcriber::Resource
4
4
  property :name
5
- embeds_many :phones, class_name: "Array"
5
+ embeds_many :phones, class_name: :array
6
6
  end
7
7
 
8
8
  @root = Customer.new({name: "Jackie Chan", phones: ["1391938", "893271984"]})
@@ -26,4 +26,4 @@ puts "root.to_input: #{@root.to_input}"
26
26
 
27
27
 
28
28
  puts "root: #{@root.inspect}"
29
- puts "resource: #{@root.resource}"
29
+ puts "resource: #{@root.resource}"
@@ -0,0 +1,9 @@
1
+ $:.push 'lib'; require 'transcriber'
2
+
3
+ class Root < Transcriber::Resource
4
+ embeds_many :items, fields: [:item1, :item2, :item3]
5
+ end
6
+
7
+ @root = Root.new(items: %w(i1 i2 i3))
8
+
9
+ puts "input: #{@root.to_input}"
@@ -4,17 +4,8 @@ module Transcriber
4
4
  def to_input(options = {})
5
5
  input = {}
6
6
  keys_to_input(options).each do |key|
7
- value = __send__("#{key.name}")
8
- parsed_value = key.to_input(value, options.fetch(key.name, {}))
9
- path = key.input_path
10
-
11
- if path.any?
12
- last = path.pop
13
- key_input = path.inject(input) {|buffer, key| buffer[key] = {}; buffer[key]}
14
- key_input[last] = parsed_value
15
- else
16
- input.merge!(parsed_value)
17
- end
7
+ value = __send__(key.name)
8
+ input.merge! key.to_input(value, options.fetch(key.name, {}))
18
9
  end
19
10
  input.with_indifferent_access
20
11
  end
@@ -23,7 +14,7 @@ module Transcriber
23
14
 
24
15
  def keys_to_input(options)
25
16
  self.class.keys.clone.tap do |keys|
26
- keys.reject! {|k| k.kind_of?(Relation)} unless options.fetch(:relations, true)
17
+ keys.reject! {|k| k.kind_of?(Relation)}
27
18
  keys.reject! {|k| Array(options[:without]).include?(k.name)}
28
19
  end
29
20
  end
@@ -1,10 +1,7 @@
1
1
  module Transcriber
2
2
  class Resource
3
3
  class Key
4
- attr_accessor :name
5
- attr_accessor :model
6
- attr_accessor :options
7
- attr_accessor :summarize
4
+ attr_accessor :name, :model, :options, :summarize
8
5
 
9
6
  def initialize(name, options = {})
10
7
  @name = name
@@ -12,29 +9,26 @@ module Transcriber
12
9
  end
13
10
 
14
11
  def present?(resource)
15
- return true unless options[:if]
16
- resource.instance_eval &options[:if]
12
+ !options[:if] ? true : resource.instance_eval(&options[:if])
17
13
  end
18
14
 
19
15
  def visible?(resource)
20
- if present? resource
21
- return true if options[:visible].nil?
22
- return options[:visible] unless options[:visible].kind_of?(Proc)
23
- return resource.instance_eval &options[:visible]
24
- end
25
- false
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
26
21
  end
27
22
 
28
23
  def input_path
29
24
  return @input_path.clone if @input_path
30
25
 
31
- path = InputPath.resolve(options, convert_input_keys)
26
+ @input_path = InputPath.resolve(options, convert_input_keys)
32
27
 
33
- if path.empty? and !root_path?
34
- path = convert_input_keys.call([name])
28
+ if @input_path.empty? and !root_path?
29
+ @input_path = convert_input_keys.call([name])
35
30
  end
36
31
 
37
- @input_path = path
38
32
  @input_path.clone
39
33
  end
40
34
 
@@ -22,7 +22,7 @@ module Transcriber
22
22
  end
23
23
 
24
24
  def resource_class
25
- @class_name.constantize
25
+ @class_name.to_s.camelize.constantize
26
26
  end
27
27
 
28
28
  def from_hash(attrs)
@@ -30,11 +30,6 @@ 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, options = {})
35
- return nil if value.nil?
36
- one? ? value.to_input(options) : value.map {|item| item.to_input(options)}
37
- end
38
33
  end
39
34
  end
40
35
  end
@@ -4,18 +4,19 @@ module Transcriber
4
4
  include Parser::Embeddable
5
5
  include Response::Embeddable
6
6
 
7
- def raw?
8
- [Hash, Array].include?(resource_class)
9
- end
7
+ attr_accessor :fields
10
8
 
11
- def from_hash(attrs)
12
- return attrs if raw?
9
+ def initialize(name, options = {})
13
10
  super
11
+
12
+ if fields = options[:fields]
13
+ @class_name = :array
14
+ @fields = fields
15
+ end
14
16
  end
15
17
 
16
- def to_input(value, options = {})
17
- return value if raw?
18
- super
18
+ def raw?
19
+ [Hash, Array].include?(resource_class)
19
20
  end
20
21
  end
21
22
  end
@@ -4,22 +4,15 @@ module Transcriber
4
4
  include Parser::Property
5
5
  include Response::Property
6
6
 
7
- attr_accessor :serializer
8
- attr_accessor :translations
7
+ attr_accessor :serializer, :translations
9
8
 
10
9
  def initialize(name, options = {})
11
10
  super
12
11
  @serializer = options.fetch(:type, Serialization::String)
13
- @translations = options[:values] if options[:values].kind_of?(Proc)
14
- @translations = options[:values].invert if options[:values].kind_of?(Hash)
15
- end
16
-
17
- def from_hash(value)
18
- value
19
- end
20
-
21
- def to_input(value, options = {})
22
- value
12
+ @translations = case values = options[:values]
13
+ when Proc then values
14
+ when Hash then values.invert
15
+ end
23
16
  end
24
17
  end
25
18
  end
@@ -21,9 +21,8 @@ module Transcriber
21
21
  def parse_item(item, options)
22
22
  self.new.tap do |resource|
23
23
  keys.each do |key|
24
- value = digg(item, key.input_path)
25
24
  begin
26
- resource.__send__("#{key.name}=", key.parse(value, resource)) if key.present?(resource)
25
+ resource.__send__("#{key.name}=", key.parse(item, resource)) if key.present?(resource)
27
26
  rescue => exception
28
27
  raise exception if options[:fail]
29
28
  end
@@ -2,9 +2,51 @@ module Transcriber
2
2
  class Resource
3
3
  module Parser
4
4
  module Embeddable
5
- def parse(value, resource = nil)
6
- parsed_value = raw? ? value : resource_class.parse(value)
7
- one? ? parsed_value.first : parsed_value
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
14
+ end
15
+
16
+ def digg(input)
17
+ input_path.inject(input) {|buffer, key| buffer = buffer[key]}
18
+ end
19
+
20
+ def from_hash(attrs)
21
+ raw? ? attrs : super
22
+ end
23
+
24
+ def to_input(value, options = {})
25
+ input = {}
26
+ return input if value.nil?
27
+
28
+ path = input_path
29
+
30
+ if path.any?
31
+ last = path.pop
32
+ key_input = path.inject(input) {|buffer, key| buffer[key] = {}; buffer[key]}
33
+
34
+ if fields
35
+ convert_input_keys.call(fields).each_with_index do |field, index|
36
+ input[field] = value[index]
37
+ end
38
+ elsif raw?
39
+ key_input[last] = value
40
+ elsif one?
41
+ key_input[last] = value.to_input(options)
42
+ else
43
+ key_input[last] = value.map {|item| item.to_input(options)}
44
+ end
45
+ else
46
+ input.merge!(value.to_input(options))
47
+ end
48
+
49
+ input
8
50
  end
9
51
  end
10
52
  end
@@ -2,20 +2,41 @@ module Transcriber
2
2
  class Resource
3
3
  module Parser
4
4
  module Property
5
- def parse(value, resource = nil)
5
+ def parse(item, resource = nil)
6
+ value = digg(item)
6
7
  translate(serializer.serialize(value), resource)
7
8
  end
8
9
 
10
+ def digg(input)
11
+ input_path.inject(input) {|buffer, key| buffer = buffer[key]}
12
+ end
13
+
9
14
  def translate(value, resource)
10
15
  case translations
11
- when nil
12
- value
13
- when Hash
14
- translations[value]
15
- when Proc
16
- resource.instance_eval &translations
16
+ when nil then value
17
+ when Hash then translations[value]
18
+ when Proc then resource.instance_eval(&translations)
17
19
  end
18
20
  end
21
+
22
+ def from_hash(value)
23
+ value
24
+ end
25
+
26
+ def to_input(value, options = {})
27
+ input = {}
28
+
29
+ path = input_path
30
+ if path.any?
31
+ last = path.pop
32
+ key_input = path.inject(input) {|buffer, key| buffer[key] = {}; buffer[key]}
33
+ key_input[last] = value
34
+ else
35
+ input.merge!(value)
36
+ end
37
+
38
+ input
39
+ end
19
40
  end
20
41
  end
21
42
  end
@@ -1,3 +1,3 @@
1
1
  module Transcriber
2
- VERSION = "0.0.15"
2
+ VERSION = "0.0.16"
3
3
  end
@@ -10,7 +10,7 @@ describe Resource::Parser::Embeddable do
10
10
 
11
11
  let(:value) {{id: "7000"}}
12
12
 
13
- subject {Resource::Embeddable.new(:example_child, many: false)}
13
+ subject {Resource::Embeddable.new(:example_child, many: false, start_key: "")}
14
14
 
15
15
  it "parses child" do
16
16
  child = subject.parse(value)
@@ -25,7 +25,7 @@ describe Resource::Parser::Embeddable do
25
25
  end
26
26
  end
27
27
 
28
- let(:value) {[{id: "7000"}, {id: "7001"}]}
28
+ let(:value) {{example_children: [{id: "7000"}, {id: "7001"}]}}
29
29
 
30
30
  subject {Resource::Embeddable.new(:example_children, many: true)}
31
31
 
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Resource::Parser::Property do
4
4
  subject {Resource::Property.new(:login)}
5
5
 
6
- let(:value) {"jackiechan2010"}
6
+ let(:item) {{login: "jackiechan2010"}}
7
7
  let(:serializer_mock) {mock :serializer}
8
8
 
9
9
  before do
@@ -11,8 +11,8 @@ describe Resource::Parser::Property do
11
11
  end
12
12
 
13
13
  it "tries to serialize value" do
14
- serializer_mock.should_receive(:serialize).with(value).and_return(value)
15
- subject.parse(value)
14
+ serializer_mock.should_receive(:serialize).with(item[:login]).and_return(item[:login])
15
+ subject.parse(item)
16
16
  end
17
17
 
18
18
  context "when there is a values map" do
@@ -24,8 +24,8 @@ describe Resource::Parser::Property do
24
24
  end
25
25
 
26
26
  it "maps value" do
27
- serializer_mock.should_receive(:serialize).with(value).and_return "02"
28
- subject.parse(value).should == :unpaid
27
+ serializer_mock.should_receive(:serialize).with("02").and_return "02"
28
+ subject.parse({payment_status: "02"}).should == :unpaid
29
29
  end
30
30
  end
31
31
  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.15
4
+ version: 0.0.16
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-30 00:00:00.000000000Z
14
+ date: 2011-09-12 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
18
- requirement: &70237690466580 !ruby/object:Gem::Requirement
18
+ requirement: &70132365829780 !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: *70237690466580
26
+ version_requirements: *70132365829780
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: i18n
29
- requirement: &70237690466060 !ruby/object:Gem::Requirement
29
+ requirement: &70132365829080 !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: *70237690466060
37
+ version_requirements: *70132365829080
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: rspec
40
- requirement: &70237690465600 !ruby/object:Gem::Requirement
40
+ requirement: &70132365828380 !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: *70237690465600
48
+ version_requirements: *70132365828380
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: guard
51
- requirement: &70237690465000 !ruby/object:Gem::Requirement
51
+ requirement: &70132365827720 !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: *70237690465000
59
+ version_requirements: *70132365827720
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: guard-rspec
62
- requirement: &70237690464520 !ruby/object:Gem::Requirement
62
+ requirement: &70132365827200 !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: *70237690464520
70
+ version_requirements: *70132365827200
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: growl
73
- requirement: &70237690463980 !ruby/object:Gem::Requirement
73
+ requirement: &70132365826720 !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: *70237690463980
81
+ version_requirements: *70132365826720
82
82
  description: ''
83
83
  email:
84
84
  - victorcrodrigues@gmail.com
@@ -98,6 +98,7 @@ files:
98
98
  - examples/embeds_many/simple.rb
99
99
  - examples/embeds_many/with_array.rb
100
100
  - examples/embeds_many/with_class_name.rb
101
+ - examples/embeds_many/with_fields.rb
101
102
  - examples/embeds_many/with_if.rb
102
103
  - examples/embeds_many/with_nil_value.rb
103
104
  - examples/embeds_many/with_start_key.rb
@@ -129,8 +130,8 @@ files:
129
130
  - examples/to_input/simple.rb
130
131
  - examples/to_input/with_field.rb
131
132
  - examples/to_input/with_field_path.rb
133
+ - examples/to_input/with_fields.rb
132
134
  - examples/to_input/without_key.rb
133
- - examples/to_input/without_relations.rb
134
135
  - lib/transcriber.rb
135
136
  - lib/transcriber/configuration.rb
136
137
  - lib/transcriber/resource.rb
@@ -1,11 +0,0 @@
1
- $:.push 'lib'; require 'transcriber'
2
-
3
- class Customer < Transcriber::Resource
4
- property :login
5
- has_one :service
6
- end
7
-
8
- @root = Customer.new(login: 'jackiechan2010')
9
-
10
- puts "root.to_input: #{@root.to_input(relations: false)}"
11
- puts "root: #{@root.inspect}"