lib/symmetric_encryption/symmetric_encryption.rb in symmetric-encryption-3.3 vs lib/symmetric_encryption/symmetric_encryption.rb in symmetric-encryption-3.4.0

- old
+ new

@@ -385,16 +385,18 @@ # Name of file containing symmetric key encrypted using the public # key from the private_rsa_key # # :encrypted_key # Symmetric key encrypted using the public key from the private_rsa_key + # and then Base64 encoded # # :iv # Optional: The actual iv to use for encryption/decryption purposes # # :encrypted_iv # Initialization vector encrypted using the public key from the private_rsa_key + # and then Base64 encoded # # :iv_filename # Optional: Name of file containing symmetric key initialization vector # encrypted using the public key from the private_rsa_key # @@ -453,10 +455,27 @@ # Decrypt Symmetric Keys Cipher.new(config) end + # Coerce given value into given type + # Does not coerce json or yaml values + def self.coerce(value, type, from_type=nil) + return if value.nil? + + from_type ||= value.class + case type + when :json + value + when :yaml + value + else + coercer = Coercible::Coercer.new + coercer[from_type].send("to_#{type}".to_sym, value) + end + end + # Uses coercible gem to coerce values from strings into the target type # Note: if the type is :string, then the value is returned as is, and the # coercible gem is not used at all. def self.coerce_from_string(value, type) return if value.nil? @@ -466,13 +485,11 @@ when :json JSON.load(value) when :yaml YAML.load(value) else - coercer = Coercible::Coercer.new - coercion_method = "to_#{type}".to_sym - coercer[String].send(coercion_method, value) + self.coerce(value, type, String) end end # Uses coercible gem to coerce values to strings from the specified type # Note: if the type is :string, and value is not nil, then #to_s is called @@ -486,11 +503,10 @@ when :json value.to_json when :yaml value.to_yaml else - coercer = Coercible::Coercer.new - coercer[coercion_type(type, value)].to_string(value) + self.coerce(value, :string, coercion_type(type, value)) end end # Returns the correct coercion type to use for the specified symbol and value def self.coercion_type(symbol, value) \ No newline at end of file