Module: Shutl::Rails::Converter

Defined in:
lib/shutl/rails/converter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.class_for(resource_class) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/shutl/rails/converter.rb', line 35

def self.class_for resource_class
  begin
    "#{resource_class}Converter".constantize
  rescue NameError => e
    Shutl::Rails::NoConverter
  end
end

Instance Method Details

#convert(*attr_names, options) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/shutl/rails/converter.rb', line 2

def convert *attr_names, options
  @converters ||= {}

  attr_names.map(&:to_sym).each do |attr_name|
    @converters[attr_name] = {
      converter_classes: Array(options[:with]),
      methods: Array(options[:only] || [:to_back_end, :to_front_end])
    }
  end
end

#convert_attribute(attr_name, method, converter_classes, attrs) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/shutl/rails/converter.rb', line 26

def convert_attribute attr_name, method, converter_classes, attrs
  converter_classes.each do |converter_class|
    if attrs.has_key?(attr_name)
      attrs[attr_name] = converter_class.send(method, attrs[attr_name])
    end
  end
  attrs
end