Sha256: bd3b999a6fceb1a98b305e41a503eadfcfbbbb5fbd32a5294650e954fdc9f457

Contents?: true

Size: 1.21 KB

Versions: 9

Compression:

Stored size: 1.21 KB

Contents

module Sniff
  module Emitter
    def self.included(target)
      target.instance_eval do
        extend ClassMethods
      end
    end
    
    def parent_class
      Emitter
    end

    module ClassMethods
      def from_params_hash(params = Hash.new)
        resolved_params = Hash.new
        associations = reflect_on_all_associations
        params.each do |k, v|
          next if v.blank?
          c = characterization[k.to_sym]
          next unless c
          if associations.map(&:name).include?(c.name.to_sym)
            association = associations.find { |a| a.name == c.name.to_sym }
            klass = association.options[:class_name] || association.name.to_s.pluralize.classify
            klass = klass.constantize
            if v.is_a?(Hash)
              # h[:origin_airport][:iata_code] => 'MIA'
              attr_name, attr_value = v.to_a.flatten[0, 2]
              resolved_params[k] = klass.send "find_by_#{attr_name}", attr_value
            else
              # h[:origin_airport] => 'MIA'
              resolved_params[k] = klass.send "find_by_#{k}", v
            end
          else
            resolved_params[k] = v
          end
        end
        new resolved_params
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sniff-0.9.0 lib/sniff/emitter.rb
sniff-0.8.7 lib/sniff/emitter.rb
sniff-0.8.6 lib/sniff/emitter.rb
sniff-0.8.5 lib/sniff/emitter.rb
sniff-0.8.4 lib/sniff/emitter.rb
sniff-0.8.3 lib/sniff/emitter.rb
sniff-0.8.2 lib/sniff/emitter.rb
sniff-0.8.1 lib/sniff/emitter.rb
sniff-0.8.0 lib/sniff/emitter.rb