Sha256: acd0e189ef24ac8b29bc6fb98f94d63bc37b82b4380f4bea2e2bae4c120c3c8f

Contents?: true

Size: 1.91 KB

Versions: 25

Compression:

Stored size: 1.91 KB

Contents

require 'fast_timestamp'
require 'common_name'

module Sniff
  module Emitter
    def self.included(target)
      target.instance_eval do
        extend ClassMethods
        include CommonName
      end
    end
    
    def parent_class
      Emitter
    end
          
    def visible_effective_characteristics
      characteristics.effective.reject { |_, c| c.hidden? }
    end
      
    def retired?
      has_attribute?(:retirement) and retirement
    end
    
    def pattern?
      self.class.pattern?
    end
    
    module ClassMethods
      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

      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
      
      def _common_name
        name.underscore
      end

      def pattern?
        common_name.ends_with? 'pattern'
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
sniff-0.2.0 lib/sniff/emitter.rb
sniff-0.1.17 lib/sniff/emitter.rb
sniff-0.1.16 lib/sniff/emitter.rb
sniff-0.1.15 lib/sniff/emitter.rb
sniff-0.1.14 lib/sniff/emitter.rb
sniff-0.1.13 lib/sniff/emitter.rb
sniff-0.1.12 lib/sniff/emitter.rb
sniff-0.1.10 lib/sniff/emitter.rb
sniff-0.1.9 lib/sniff/emitter.rb
sniff-0.1.8 lib/sniff/emitter.rb
sniff-0.1.7 lib/sniff/emitter.rb
sniff-0.1.6 lib/sniff/emitter.rb
sniff-0.1.5 lib/sniff/emitter.rb
sniff-0.1.4 lib/sniff/emitter.rb
sniff-0.1.3 lib/sniff/emitter.rb
sniff-0.1.2 lib/sniff/emitter.rb
sniff-0.1.1 lib/sniff/emitter.rb
sniff-0.1.0 lib/sniff/emitter.rb
sniff-0.0.19 lib/sniff/emitter.rb
sniff-0.0.18 lib/sniff/emitter.rb