Sha256: e1d45850260367140465170ff58175f4f500dafcdd92af2f71776f061600da62

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

module NotifyOn
  module StringInterpolation
    extend ActiveSupport::Concern

    private

      def convert_string(input)
        return nil if input.blank?
        (output = input.to_s).scan(/{[\w\_\.\?]+}/).each do |match|
          result = begin
            match.gsub(/[^\w\_\.\?]/, '').split('.').inject(trigger, :send)
          rescue NoMethodError => e
            match.gsub(/[^\w\_\.\?]/, '').split('.').inject(self, :send)
          end
          output = output.gsub(/#{match}/, result.to_s)
        end
        output.gsub(/\{:env}/, Rails.env.downcase)
              .gsub(/\{:recipient_id}/, recipient_id.to_s)
      end

      def convert_link(input)
        return nil if input.blank?
        input = input.split(/\(|\)|,/).map(&:strip)
        if %w(_path _url).select { |r| input[0].end_with?(r) }.blank?
          return convert_string(input[0])
        end
        args = []
        input[1..-1].each do |arg|
          args << if %w(self :self).include?(arg)
            trigger
          else
            (arg[0] == ':') ? trigger.send(arg[1..-1]) : arg
          end
        end
        Rails.application.routes.url_helpers.send(input[0], *args)
      end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
notify_on-1.0.5 app/models/concerns/notify_on/string_interpolation.rb
notify_on-1.0.4 app/models/concerns/notify_on/string_interpolation.rb
notify_on-1.0.3 app/models/concerns/notify_on/string_interpolation.rb
notify_on-1.0.2 app/models/concerns/notify_on/string_interpolation.rb
notify_on-1.0.1 app/models/concerns/notify_on/string_interpolation.rb