diff --git a/CHANGELOG.md b/CHANGELOG.md index fbb24f6..6c46179 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Not released - Add `.to_json` and `.as_json` to `SObject` to allow JSON serialization (https://github.com/Beyond-Finance/active_force/pull/37) +- Memoize the `ActiveForce::Mapping#mappings` Hash since it is based on the fields and those are generally only set when the class is loaded. Also use `Hash#key` which returns the key for a value rather than `Hash#invert` which creates a new Hash with key/value inverted. (https://github.com/Beyond-Finance/active_force/pull/41) ## 0.13.0 diff --git a/lib/active_force/mapping.rb b/lib/active_force/mapping.rb index c6f10b4..32e0827 100644 --- a/lib/active_force/mapping.rb +++ b/lib/active_force/mapping.rb @@ -13,7 +13,7 @@ def initialize model end def mappings - Hash[fields.map { |field, attr| [field, attr.sfdc_name] }] + @mappings ||= Hash[fields.map { |field, attr| [field, attr.sfdc_name] }] end def sfdc_names diff --git a/lib/active_force/sobject.rb b/lib/active_force/sobject.rb index a6511e7..5253dbf 100644 --- a/lib/active_force/sobject.rb +++ b/lib/active_force/sobject.rb @@ -180,7 +180,7 @@ def write_value key, value field = key else # Assume key is an SFDC column - field = mappings.invert[key] + field = mappings.key(key) end send "#{field}=", value if field && respond_to?(field) end