Module: Intercom::GenericHandlers::Tag::ClassMethods

Defined in:
lib/intercom/generic_handlers/tag.rb

Instance Method Summary collapse

Instance Method Details

#generic_tag(method_sym, *arguments, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
56
57
58
# File 'lib/intercom/generic_handlers/tag.rb', line 7

def generic_tag(method_sym, *arguments, &block)

  handler_class = Class.new(GenericHandlers::BaseHandler) do
    def handle
      if method_string.start_with? 'tag_'
        return do_tagging
      elsif method_string.start_with? 'untag_'
        return do_untagging
      else
        raise_no_method_missing_handler
      end
    end

    private

    def do_tagging
      entity.create(:name => arguments[0], tagging_context.to_sym => tag_object_list(arguments))
    end

    def do_untagging
      current_tag = find_tag(arguments[0])
      return untag_via_save(current_tag) if current_tag
    end

    def find_tag(name_arg)
      Intercom::Tag.find(:name => name_arg)
    rescue Intercom::ResourceNotFound
      return nil # Ignore if tag has since been deleted
    end

    def untag_via_save(current_tag)
      current_tag.name = arguments[0]
      current_tag.send("#{untagging_context}=", untag_object_list(arguments))
      current_tag.save
    end
    
    def tag_object_list(args)
      args[1].map { |id| { :id => id } }
    end
    
    def untag_object_list(args)
      to_tag = tag_object_list(args)
      to_tag.map { |tag_object| tag_object[:untag] = true }
      to_tag
    end

    def tagging_context; method_string.gsub(/^tag_/, ''); end
    def untagging_context; method_string.gsub(/^untag_/, ''); end
  end

  handler_class.new(method_sym, arguments, self).handle
end