field_mapper 0.3.6 → 0.3.7

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: 326047e4529a353d1c50f839fbc6e2fb1d1f408f
4
- data.tar.gz: 7877b3c18b94f0d5d206ca3c8c6224f0ffd4da06
3
+ metadata.gz: 5787388dd0340f6e40fa4fe6687b899ab4c40b65
4
+ data.tar.gz: bc1e3bac64aaadf91c5def90224ce26bab25d829
5
5
  SHA512:
6
- metadata.gz: 6aec1e1cdbc7ceec8988b2c52f51be45e235920ac178a8625e6abf778a343529f71cd5433735ac53778a5f1217e651b539a8443c197f76249f784365756048bf
7
- data.tar.gz: 36880e7eb5aa03c569614a84f15df51818f788c484ee9429b9a197d09ccecee722fa8be35188f55afced5221f414c577877971a34d15cefb170fce35d77f8ad5
6
+ metadata.gz: a1d18f909bcf83a82190b6faf515e2a4f1c86ea79d6d9e18757ee3cf80bd0dbcb46d379950b1f1cad58a496e5d796f8bfb8df8659f0b4294ba23455da717a7b3
7
+ data.tar.gz: c3c17f2f5807b1c4fbc45e8a3744d40791e398f85cad5aba9ae09d28272878ac7955744ed74b397af8c8edaecf7c2f4083e0e4e570e538e40f8dce5a82842255
@@ -0,0 +1,28 @@
1
+ module FieldMapper
2
+ class FieldAccessByStandardName
3
+
4
+ def initialize(custom_plat)
5
+ @custom_plat = custom_plat
6
+ @fields_by_standard_name = custom_plat.class.fields_by_standard_name
7
+ end
8
+
9
+ def [](standard_name)
10
+ custom_field = field(standard_name)
11
+ custom_plat[custom_field.name]
12
+ end
13
+
14
+ def []=(standard_name, value)
15
+ custom_field = field(standard_name)
16
+ custom_plat[custom_field.name] = value
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :custom_plat, :fields_by_standard_name
22
+
23
+ def field(standard_name)
24
+ fields_by_standard_name[standard_name.to_sym]
25
+ end
26
+
27
+ end
28
+ end
@@ -2,6 +2,7 @@ require_relative "../standard/plat"
2
2
  require_relative "../errors"
3
3
  require_relative "../name_helper"
4
4
  require_relative "field"
5
+ require_relative "field_access_by_standard_name"
5
6
 
6
7
  module FieldMapper
7
8
  module Custom
@@ -52,6 +53,32 @@ module FieldMapper
52
53
  def find_mapped_fields(standard_field)
53
54
  fields.values.select { |field| field.standard_field == standard_field }
54
55
  end
56
+
57
+ def fields_by_standard_name
58
+ @fields_by_standard_name ||= fields.values.reduce({}) do |memo, field|
59
+ memo[field.standard_field.name] = field unless field.standard_field.nil?
60
+ memo
61
+ end
62
+ end
63
+
64
+ # TODO: update to work recursively
65
+ def standard_keys_to_custom_keys(standard_keyed_params)
66
+ standard_keyed_params.reduce({}) do |memo, standard_param|
67
+ key = standard_param.first
68
+ value = standard_param.last
69
+ field = fields_by_standard_name[key.to_sym]
70
+ memo[field.name] = value unless field.nil?
71
+ memo
72
+ end
73
+ end
74
+
75
+ def new_from_standard_keyed_params(standard_keyed_params)
76
+ new standard_keys_to_custom_keys(standard_keyed_params)
77
+ end
78
+ end
79
+
80
+ def standard_name
81
+ @standard_name ||= FieldAccessByStandardName.new(self)
55
82
  end
56
83
 
57
84
  end
@@ -9,16 +9,24 @@ module FieldMapper
9
9
  class_cache: true,
10
10
  escape: :json,
11
11
  time: :unix,
12
- create_id: "natefoo"
12
+ create_id: "field_mapper_json_create"
13
13
  }
14
14
 
15
15
  def marshal(value)
16
- Oj.dump value, OPTIONS
16
+ Oj.dump prep_value(value), OPTIONS
17
17
  end
18
18
 
19
19
  def unmarshal(value)
20
20
  Oj.load value, OPTIONS
21
21
  end
22
22
 
23
+ private
24
+
25
+ def prep_value(value)
26
+ return value.map { |v| prep_value v } if value.is_a?(Array)
27
+ return value.to_hash if value.is_a?(HashWithIndifferentAccess)
28
+ value
29
+ end
30
+
23
31
  end
24
32
  end
@@ -1,3 +1,3 @@
1
1
  module FieldMapper
2
- VERSION = "0.3.6"
2
+ VERSION = "0.3.7"
3
3
  end
@@ -17,6 +17,24 @@ module Custom
17
17
  assert @class.find_field(:name).standard_field == @class.standard_plat.find_field(:name)
18
18
  end
19
19
 
20
+ test "field access by standard_name" do
21
+ @instance.standard_name[:score] = "A"
22
+ assert @instance.standard_name[:score] == "A"
23
+ assert @instance[:rating] == "A"
24
+ end
25
+
26
+ test "standard_keys_to_custom_keys" do
27
+ standard_keyed_params = { score: "A" } # note that "A" is a custom value
28
+ custom_params = Custom::PlatExample.standard_keys_to_custom_keys(standard_keyed_params)
29
+ assert custom_params[:rating] == "A"
30
+ end
31
+
32
+ test "new_from_standard_keyed_params" do
33
+ standard_keyed_params = { score: "A" } # note that "A" is a custom value
34
+ instance = Custom::PlatExample.new_from_standard_keyed_params(standard_keyed_params)
35
+ assert instance[:rating] == "A"
36
+ end
37
+
20
38
  test "field mapping with value mappings" do
21
39
  custom_field = @class.find_field(:color)
22
40
  standard_field = @class.standard_plat.find_field(:color)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: field_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hopkins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-15 00:00:00.000000000 Z
11
+ date: 2014-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 2.7.1
75
+ version: 2.9.4
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 2.7.1
82
+ version: 2.9.4
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: bundler
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -174,6 +174,7 @@ files:
174
174
  - lib/field_mapper.rb
175
175
  - lib/field_mapper/custom/converter.rb
176
176
  - lib/field_mapper/custom/field.rb
177
+ - lib/field_mapper/custom/field_access_by_standard_name.rb
177
178
  - lib/field_mapper/custom/plat.rb
178
179
  - lib/field_mapper/custom/value.rb
179
180
  - lib/field_mapper/errors.rb