Module: Sniff::Emitter::ClassMethods

Defined in:
lib/sniff/emitter.rb

Instance Method Summary collapse

Instance Method Details

#_common_nameObject



63
64
65
# File 'lib/sniff/emitter.rb', line 63

def _common_name
  name.underscore
end

#add_implicit_characteristicsObject



57
58
59
60
61
# File 'lib/sniff/emitter.rb', line 57

def add_implicit_characteristics
  decisions[:emission].committees.map(&:name).reject { |c| characteristics.keys.unshift(:emission).include? c }.each do |c|
    characterize { has c }
  end
end

#from_params_hash(params = Hash.new) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sniff/emitter.rb', line 30

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)


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

def pattern?
  common_name.ends_with? 'pattern'
end