Sha256: 8d46c4b5586d413f9f0c43aaeeefa34febd2dbb3642771e65e99342a4f540cd9
Contents?: true
Size: 1.98 KB
Versions: 7
Compression:
Stored size: 1.98 KB
Contents
class RestModel module Source class Translation attr_accessor :values, :from_source, :to_source def initialize(attrs = {}) @values = attrs[:values] @from_source = attrs[:from_source] @to_source = attrs[:to_source] end def translates_from_source? from_source or values end def translate_from_source(value, resource) if from_source resource.instance_exec(value, &from_source) elsif values translate_from_source_values(value) else value end end def translates_to_source? to_source or values end def translate_to_source(value, resource) if to_source resource.instance_exec(value, &to_source) elsif values translate_to_source_values(value) else value end end private def translate_from_source_values(value) if values.kind_of?(Hash) unless values.has_value?(value) fail TranslationError, "value '#{value}' doesn't belong to values: #{values.values}" end values.key(value) elsif values.kind_of?(Array) unless values.member?(value) fail TranslationError, "value '#{value}' doesn't belong to values in: #{values}" end value else fail TranslationError, "not supported values" end end def translate_to_source_values(value) if values.kind_of?(Hash) unless values.has_key?(value) fail TranslationError, "value '#{value}' doesn't belong to values: #{values.keys}" end values[value] elsif values.kind_of?(Array) unless values.member?(value) fail TranslationError, "value '#{value}' doesn't belong to values in: #{values}" end value else fail TranslationError, "not supported values" end end end end end
Version data entries
7 entries across 7 versions & 1 rubygems