Class: PaperPlane::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks, Objectified, Callbacks
Defined in:
lib/paper_plane/base.rb

Direct Known Subclasses

FlightRoutes::Sms

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Base

set the routes to fly



74
75
76
77
78
79
80
81
82
# File 'lib/paper_plane/base.rb', line 74

def initialize(**kwargs)
  @template_formats = template_formats
  @skipped_flight_routes = []
  @flight_routes = self.class.flight_routes
  @paper_plane_name = base_klass_string
  @recipient ||= kwargs.delete(:to)
  @async ||= kwargs.delete(:async) || ENV['RAILS_ENV'] == 'development'
  @params = kwargs
end

Instance Attribute Details

#flight_routesObject (readonly)

Returns the value of attribute flight_routes.



71
72
73
# File 'lib/paper_plane/base.rb', line 71

def flight_routes
  @flight_routes
end

#midObject (readonly)

Returns the value of attribute mid.



71
72
73
# File 'lib/paper_plane/base.rb', line 71

def mid
  @mid
end

#paramsObject (readonly)

Returns the value of attribute params.



71
72
73
# File 'lib/paper_plane/base.rb', line 71

def params
  @params
end

Class Method Details

.flight_routesObject



29
30
31
# File 'lib/paper_plane/base.rb', line 29

def flight_routes
  @flight_routes ||= {}
end

.fly_action(mid, **kwargs) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/paper_plane/base.rb', line 41

def fly_action(mid, **kwargs)
  new(**kwargs).tap do |paper_plane|
    paper_plane.run_callbacks(:flying) do
      paper_plane.send(mid)
      paper_plane.fly(mid)
    end
  end
end

.inherited(subclass) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/paper_plane/base.rb', line 63

def self.inherited(subclass)
  subclass.class_eval do
    object_type :paper_plane
    register_flight_route :email, PaperPlane::FlightRoutes::Email
    register_flight_route :sms, PaperPlane::FlightRoutes::Sms
  end
end

.method_missing(mid, *args, &block) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/paper_plane/base.rb', line 50

def method_missing(mid, *args, &block)
  if instance_methods(false).include? mid
    fly_action(mid, *args)
  else
    super
  end
end

.register_flight_route(route_name, flight_route) ⇒ Object



33
34
35
# File 'lib/paper_plane/base.rb', line 33

def register_flight_route(route_name, flight_route)
  flight_routes[route_name] = flight_route
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/paper_plane/base.rb', line 58

def respond_to_missing?(method_name, include_private = false)
  instance_methods(false).include?(method_name) || super
end

.skip(*flight_routes_types) ⇒ Object



23
24
25
26
27
# File 'lib/paper_plane/base.rb', line 23

def skip(*flight_routes_types)
  define_method(:skipped_flight_routes) do
    @skipped_flight_routes = flight_routes_types
  end
end

.template_formats(**map) ⇒ Object



37
38
39
# File 'lib/paper_plane/base.rb', line 37

def template_formats(**map)
  @template_formats = map || {}
end

Instance Method Details

#contextObject

extracts context from the method in the paper_plane, to be injected in each flight route



113
114
115
# File 'lib/paper_plane/base.rb', line 113

def context
  instance_variables.map { |attribute| [attribute, instance_variable_get(attribute)] }.to_h
end

#fly(mid) ⇒ Object

set message and call private do_fly method



85
86
87
88
89
90
91
92
93
# File 'lib/paper_plane/base.rb', line 85

def fly(mid)
  @mid = mid

  if @async
    do_fly
  else
    PaperPlane::FlyJob.fly_later(self.class.to_s, mid, **params)
  end
end

#fly_now!(mid) ⇒ Object

flies without enqueing jobs



96
97
98
99
100
# File 'lib/paper_plane/base.rb', line 96

def fly_now!(mid)
  @async = true

  fly(mid)
end

#flying_route?(route_type) ⇒ Boolean

determines whether the piegon is going to fly this route

Returns:

  • (Boolean)


108
109
110
# File 'lib/paper_plane/base.rb', line 108

def flying_route?(route_type)
  skipped_flight_routes.exclude? route_type
end

#skip(*flight_routes_types) ⇒ Object

determines which routes the paper_plane should skip



103
104
105
# File 'lib/paper_plane/base.rb', line 103

def skip(*flight_routes_types)
  @skipped_flight_routes.concat(flight_routes_types)
end