restforce-db 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 426c96af98332d2afd82da60f56121eaae4197e2
4
- data.tar.gz: 7d6fdb0c0b304e9ce2cd4149e502a85cf6b98263
3
+ metadata.gz: d8a211ef8c000ea0d937a3a834aa54ba7c535161
4
+ data.tar.gz: a5c4889b4640d94fa9759d11bb4a32036ac60f6c
5
5
  SHA512:
6
- metadata.gz: ed5d4a11b2b27fe9003e81c9ed0b68cd86fd43cc706d77443d43ee182f8726f35030efe030a65d17ba2a72439850da7a03d03e8941276b523cf3af10a451fe17
7
- data.tar.gz: 9226923f587bbefa6e041a0a918e7e36b9e22ddcd5d78f991449a630854cdf1ec9684b3e4c7d37ad6f17fed4913fbce385f9e2f11491e897cc641c1427f12676
6
+ metadata.gz: d37096768b104f436c00a4a0945d61c0918a3bf2bd67ce3310d30ebb2b4d45e9761d3b5384fab1cb0fd447da06e31b9ff4c689da8947816d2cac4b11a8d88984
7
+ data.tar.gz: 9c206305857025aafeb0d93ec9e46366513a9da37e9eefab6a70b38058cf8ae4dfac24b4495d296be1c7820bee4f5c440dbab0354ae484852d82f8bbfec09620
@@ -19,14 +19,9 @@ module Restforce
19
19
  # adapter - An adapter object which should be used to convert
20
20
  # between data formats.
21
21
  def initialize(database_model, salesforce_model, fields = {}, adapter = Adapter.new)
22
- @database_model = database_model
23
- @salesforce_model = salesforce_model
24
- @fields = fields
25
- @adapter = adapter
26
-
27
- @types = {
28
- database_model => :database,
29
- salesforce_model => :salesforce,
22
+ @field_maps = {
23
+ database_model => AttributeMaps::Database.new(fields, adapter),
24
+ salesforce_model => AttributeMaps::Salesforce.new(fields),
30
25
  }
31
26
  end
32
27
 
@@ -36,27 +31,12 @@ module Restforce
36
31
  #
37
32
  # from_format - A String or Class reflecting the record type from which
38
33
  # the attribute Hash is being compiled.
34
+ # record - The underlying record for which attributes should be
35
+ # collected.
39
36
  #
40
- # Yields a series of attribute names.
41
37
  # Returns a Hash.
42
- def attributes(from_format)
43
- case @types[from_format]
44
- when :salesforce
45
- @fields.values.each_with_object({}) do |mapping, values|
46
- values[mapping] = yield(mapping)
47
- end
48
- when :database
49
- attributes = @fields.keys.each_with_object({}) do |attribute, values|
50
- values[attribute] = yield(attribute)
51
- end
52
- attributes = @adapter.from_database(attributes)
53
-
54
- @fields.each_with_object({}) do |(attribute, mapping), final|
55
- final[mapping] = attributes[attribute]
56
- end
57
- else
58
- raise ArgumentError
59
- end
38
+ def attributes(from_format, record)
39
+ @field_maps[from_format].attributes(record)
60
40
  end
61
41
 
62
42
  # Public: Convert a Hash of normalized attributes to a format compatible
@@ -83,19 +63,7 @@ module Restforce
83
63
  #
84
64
  # Returns a Hash.
85
65
  def convert(to_format, attributes)
86
- case @types[to_format]
87
- when :database
88
- attributes = @fields.each_with_object({}) do |(attribute, mapping), converted|
89
- next unless attributes.key?(mapping)
90
- converted[attribute] = attributes[mapping]
91
- end
92
-
93
- @adapter.to_database(attributes)
94
- when :salesforce
95
- attributes.dup
96
- else
97
- raise ArgumentError
98
- end
66
+ @field_maps[to_format].convert(attributes)
99
67
  end
100
68
 
101
69
  end
@@ -0,0 +1,73 @@
1
+ module Restforce
2
+
3
+ module DB
4
+
5
+ module AttributeMaps
6
+
7
+ # Restforce::DB::AttributeMaps::Database encapsulates the logic for
8
+ # compiling and parsing normalized attribute hashes from/for ActiveRecord
9
+ # objects.
10
+ class Database
11
+
12
+ # Public: Initialize a Restforce::DB::AttributeMaps::Database.
13
+ #
14
+ # fields - A Hash of mappings between database columns and fields in
15
+ # Salesforce.
16
+ # adapter - An adapter object which should be used to convert between
17
+ # data formats.
18
+ def initialize(fields, adapter = Adapter.new)
19
+ @fields = fields
20
+ @adapter = adapter
21
+ end
22
+
23
+ # Public: Build a normalized Hash of attributes from the appropriate set
24
+ # of mappings. The keys of the resulting mapping Hash will correspond to
25
+ # the Salesforce field names.
26
+ #
27
+ # record - The underlying ActiveRecord object for which attributes
28
+ # should be collected.
29
+ #
30
+ # Returns a Hash.
31
+ def attributes(record)
32
+ attributes = @fields.keys.each_with_object({}) do |attribute, values|
33
+ values[attribute] = record.send(attribute)
34
+ end
35
+ attributes = @adapter.from_database(attributes)
36
+
37
+ @fields.each_with_object({}) do |(attribute, mapping), final|
38
+ final[mapping] = attributes[attribute]
39
+ end
40
+ end
41
+
42
+ # Public: Convert a Hash of normalized attributes to a format suitable
43
+ # for consumption by an ActiveRecord object.
44
+ #
45
+ # attributes - A Hash of attributes, with keys corresponding to the
46
+ # normalized Salesforce attribute names.
47
+ #
48
+ # Examples
49
+ #
50
+ # attribute_map = AttributeMaps::Database.new(
51
+ # some_key: "SomeField__c",
52
+ # )
53
+ #
54
+ # attribute_map.convert(MyClass, "Some_Field__c" => "some value")
55
+ # # => { some_key: "some value" }
56
+ #
57
+ # Returns a Hash.
58
+ def convert(attributes)
59
+ attributes = @fields.each_with_object({}) do |(attribute, mapping), converted|
60
+ next unless attributes.key?(mapping)
61
+ converted[attribute] = attributes[mapping]
62
+ end
63
+
64
+ @adapter.to_database(attributes)
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,62 @@
1
+ module Restforce
2
+
3
+ module DB
4
+
5
+ module AttributeMaps
6
+
7
+ # Restforce::DB::AttributeMaps::Database encapsulates the logic for
8
+ # compiling and parsing normalized attribute hashes from/for Salesforce
9
+ # objects.
10
+ class Salesforce
11
+
12
+ # Public: Initialize a Restforce::DB::AttributeMaps::Salesforce.
13
+ #
14
+ # fields - A Hash of mappings between database columns and fields in
15
+ # Salesforce.
16
+ def initialize(fields)
17
+ @fields = fields
18
+ end
19
+
20
+ # Public: Build a normalized Hash of attributes from the appropriate set
21
+ # of mappings. The keys of the resulting mapping Hash will correspond to
22
+ # the Salesforce field names.
23
+ #
24
+ # record - The underlying Salesforce object for which attributes should
25
+ # be collected.
26
+ #
27
+ # Returns a Hash.
28
+ def attributes(record)
29
+ @fields.values.each_with_object({}) do |mapping, values|
30
+ values[mapping] = mapping.split(".").inject(record) do |value, portion|
31
+ value[portion]
32
+ end
33
+ end
34
+ end
35
+
36
+ # Public: Convert a Hash of normalized attributes to a format suitable
37
+ # for consumption by an ActiveRecord object.
38
+ #
39
+ # attributes - A Hash of attributes, with keys corresponding to the
40
+ # normalized Salesforce attribute names.
41
+ #
42
+ # Examples
43
+ #
44
+ # attribute_map = AttributeMaps::Salesforce.new(
45
+ # some_key: "SomeField__c",
46
+ # )
47
+ #
48
+ # mapping.convert("Object__c", "Some_Field__c" => "some other value")
49
+ # # => { "Some_Field__c" => "some other value" }
50
+ #
51
+ # Returns a Hash.
52
+ def convert(attributes)
53
+ attributes.dup
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -37,7 +37,7 @@ module Restforce
37
37
  #
38
38
  # Returns a Hash.
39
39
  def attributes
40
- @mapping.attributes(@record_type) { |attribute| record.send(attribute) }
40
+ @mapping.attributes(@record_type, record)
41
41
  end
42
42
 
43
43
  # Public: Has this record been synced with Salesforce?
@@ -3,7 +3,7 @@ module Restforce
3
3
  # :nodoc:
4
4
  module DB
5
5
 
6
- VERSION = "3.3.0"
6
+ VERSION = "3.4.0"
7
7
 
8
8
  end
9
9
 
data/lib/restforce/db.rb CHANGED
@@ -19,6 +19,9 @@ require "restforce/db/associations/foreign_key"
19
19
  require "restforce/db/associations/has_many"
20
20
  require "restforce/db/associations/has_one"
21
21
 
22
+ require "restforce/db/attribute_maps/database"
23
+ require "restforce/db/attribute_maps/salesforce"
24
+
22
25
  require "restforce/db/field_processor"
23
26
  require "restforce/db/instances/base"
24
27
  require "restforce/db/instances/active_record"
@@ -23,23 +23,19 @@ describe Restforce::DB::AttributeMap do
23
23
  end
24
24
 
25
25
  it "builds a normalized Hash of database attribute values" do
26
- attributes = attribute_map.attributes(database_model) do |attribute|
27
- expect(mapping.database_fields.include?(attribute)).to_equal true
28
- attribute
29
- end
26
+ record = Hashie::Mash.new(column_one: "Eenie", column_two: "Meenie")
27
+ attributes = attribute_map.attributes(database_model, record)
30
28
 
31
29
  expect(attributes.keys).to_equal(mapping.salesforce_fields)
32
- expect(attributes.values).to_equal(mapping.database_fields)
30
+ expect(attributes.values).to_equal(%w(Eenie Meenie))
33
31
  end
34
32
 
35
33
  it "builds a normalized Hash of Salesforce field values" do
36
- attributes = attribute_map.attributes(salesforce_model) do |attribute|
37
- expect(mapping.salesforce_fields.include?(attribute)).to_equal true
38
- attribute
39
- end
34
+ record = Hashie::Mash.new("SF_Field_One__c" => "Minie", "SF_Field_Two__c" => "Moe")
35
+ attributes = attribute_map.attributes(salesforce_model, record)
40
36
 
41
37
  expect(attributes.keys).to_equal(mapping.salesforce_fields)
42
- expect(attributes.values).to_equal(mapping.salesforce_fields)
38
+ expect(attributes.values).to_equal(%w(Minie Moe))
43
39
  end
44
40
  end
45
41
 
@@ -55,19 +51,6 @@ describe Restforce::DB::AttributeMap do
55
51
  fields.key(attributes.keys.first) => attributes.values.first,
56
52
  )
57
53
  end
58
-
59
- describe "when an adapter has been specified" do
60
- let(:adapter) { boolean_adapter }
61
-
62
- it "uses the adapter to convert attributes to a database-compatible form" do
63
- expect(attribute_map.convert(database_model, "SF_Field_One__c" => "Yes")).to_equal(
64
- column_one: true,
65
- )
66
- expect(attribute_map.convert(database_model, "SF_Field_One__c" => "No")).to_equal(
67
- column_one: false,
68
- )
69
- end
70
- end
71
54
  end
72
55
 
73
56
  end
@@ -0,0 +1,51 @@
1
+ require_relative "../../../../test_helper"
2
+
3
+ describe Restforce::DB::AttributeMaps::Database do
4
+
5
+ configure!
6
+
7
+ let(:adapter) { Restforce::DB::Adapter.new }
8
+ let(:attribute_map) { Restforce::DB::AttributeMaps::Database.new(fields, adapter) }
9
+ let(:fields) do
10
+ {
11
+ column_one: "SF_Field_One__c",
12
+ column_two: "SF_Field_Two__c",
13
+ }
14
+ end
15
+
16
+ describe "#attributes" do
17
+
18
+ it "builds a normalized Hash of database attribute values" do
19
+ record = Hashie::Mash.new(column_one: "Winkin", column_two: "Blinkin")
20
+ attributes = attribute_map.attributes(record)
21
+
22
+ expect(attributes.keys).to_equal(fields.values)
23
+ expect(attributes.values).to_equal(%w(Winkin Blinkin))
24
+ end
25
+ end
26
+
27
+ describe "#convert" do
28
+ let(:attributes) { { "SF_Field_One__c" => "some value" } }
29
+
30
+ it "converts an attribute Hash to a database-compatible form" do
31
+ expect(attribute_map.convert(attributes)).to_equal(
32
+ fields.key(attributes.keys.first) => attributes.values.first,
33
+ )
34
+ end
35
+
36
+ describe "when an adapter has been specified" do
37
+ let(:adapter) { boolean_adapter }
38
+
39
+ it "uses the adapter to convert attributes to a database-compatible form" do
40
+ expect(attribute_map.convert("SF_Field_One__c" => "Yes")).to_equal(
41
+ column_one: true,
42
+ )
43
+ expect(attribute_map.convert("SF_Field_One__c" => "No")).to_equal(
44
+ column_one: false,
45
+ )
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,54 @@
1
+ require_relative "../../../../test_helper"
2
+
3
+ describe Restforce::DB::AttributeMaps::Salesforce do
4
+
5
+ configure!
6
+
7
+ let(:attribute_map) { Restforce::DB::AttributeMaps::Salesforce.new(fields) }
8
+ let(:fields) do
9
+ {
10
+ column_one: "SF_Field_One__c",
11
+ column_two: "SF_Field_Two__c",
12
+ }
13
+ end
14
+
15
+ describe "#attributes" do
16
+
17
+ it "builds a normalized Hash of Salesforce attribute values" do
18
+ record = Hashie::Mash.new("SF_Field_One__c" => "Winkin", "SF_Field_Two__c" => "Blinkin")
19
+ attributes = attribute_map.attributes(record)
20
+
21
+ expect(attributes.keys).to_equal(fields.values)
22
+ expect(attributes.values).to_equal(%w(Winkin Blinkin))
23
+ end
24
+
25
+ describe "for a mapping requiring Salesforce association traversal" do
26
+ let(:fields) do
27
+ {
28
+ name: "Name",
29
+ friend_name: "Friend__r.Name",
30
+ }
31
+ end
32
+
33
+ it "builds a flattened normalized Hash of Salesforce attribute values" do
34
+ record = Hashie::Mash.new(
35
+ "Name" => "Turner",
36
+ "Friend__r" => { "Name" => "Hooch" },
37
+ )
38
+ attributes = attribute_map.attributes(record)
39
+
40
+ expect(attributes.keys).to_equal(fields.values)
41
+ expect(attributes.values).to_equal(%w(Turner Hooch))
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "#convert" do
47
+ let(:attributes) { { "SF_Field_One__c" => "some value" } }
48
+
49
+ it "doesn't perform any special modification of the passed attribute Hash" do
50
+ expect(attribute_map.convert(attributes)).to_equal(attributes)
51
+ end
52
+ end
53
+
54
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restforce-db
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Horner
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-24 00:00:00.000000000 Z
11
+ date: 2015-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -218,6 +218,8 @@ files:
218
218
  - lib/restforce/db/associator.rb
219
219
  - lib/restforce/db/attacher.rb
220
220
  - lib/restforce/db/attribute_map.rb
221
+ - lib/restforce/db/attribute_maps/database.rb
222
+ - lib/restforce/db/attribute_maps/salesforce.rb
221
223
  - lib/restforce/db/cleaner.rb
222
224
  - lib/restforce/db/client.rb
223
225
  - lib/restforce/db/collector.rb
@@ -341,6 +343,8 @@ files:
341
343
  - test/lib/restforce/db/associator_test.rb
342
344
  - test/lib/restforce/db/attacher_test.rb
343
345
  - test/lib/restforce/db/attribute_map_test.rb
346
+ - test/lib/restforce/db/attribute_maps/database_test.rb
347
+ - test/lib/restforce/db/attribute_maps/salesforce_test.rb
344
348
  - test/lib/restforce/db/cleaner_test.rb
345
349
  - test/lib/restforce/db/collector_test.rb
346
350
  - test/lib/restforce/db/configuration_test.rb