Module: Sniff::Emitter::ClassMethods

Defined in:
lib/sniff/emitter.rb

Instance Method Summary collapse

Instance Method Details

#_common_nameObject



67
68
69
# File 'lib/sniff/emitter.rb', line 67

def _common_name
  name.underscore
end

#from_params_hash(params = Hash.new) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sniff/emitter.rb', line 40

def from_params_hash(params = Hash.new)
  resolved_params = Hash.new
  instance = new
  associations = reflect_on_all_associations
  params.each do |k, v|
    next if v.blank?
    c = characteristics[k.to_sym]
    next if c.nil?
    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

#pattern?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/sniff/emitter.rb', line 71

def pattern?
  common_name.ends_with? 'pattern'
end