Class: AppDrone::Drone

Inherits:
Object
  • Object
show all
Defined in:
lib/app_drone/drones/flair/flair.rb,
lib/app_drone/drone.rb

Overview

extend drones so they can call ‘flair!` rather than the verbose method this pattern is useful for drones that take a single boolean parameter e.g.

flair: do / don't flair, as opposed to
bootstrap: vendor, responsive, font_awesome...

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, *params) ⇒ Drone

New



10
11
12
13
14
# File 'lib/app_drone/drone.rb', line 10

def initialize(template,*params)
  @template = template
  @params = (params.first || {}).merge({options: {}})
  setup
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/app_drone/drone.rb', line 26

def method_missing(meth, *args, &block)
  if Drone.drones.include?(meth)
    klass = meth.to_app_drone_class
    return (self >> klass)
  else
    super
  end
end

Instance Attribute Details

#paramsObject

DSL: Integration-specific options



70
71
72
# File 'lib/app_drone/drone.rb', line 70

def params
  @params
end

Class Method Details

.categoriesObject



118
119
120
# File 'lib/app_drone/drone.rb', line 118

def categories
  self.descendants.map(&:category).uniq
end

.category(c = nil) ⇒ Object



87
88
89
90
# File 'lib/app_drone/drone.rb', line 87

def category(c=nil)
  return @category if c.nil?
  @category = c
end

.dependantsObject



97
98
99
# File 'lib/app_drone/drone.rb', line 97

def dependants
  self.superclass.drones.select { |d| d.to_app_drone_class.dependencies.include?(self) }.map(&:to_app_drone_class)
end

.dependenciesObject



93
94
95
# File 'lib/app_drone/drone.rb', line 93

def dependencies
  (@dependencies || []).map(&:to_app_drone_class)
end

.depends_on(*klass_symbols) ⇒ Object



92
# File 'lib/app_drone/drone.rb', line 92

def depends_on(*klass_symbols); @dependencies = klass_symbols end

.desc(d = nil) ⇒ Object



82
83
84
85
# File 'lib/app_drone/drone.rb', line 82

def desc(d=nil)
  return @description if d.nil?
  @description = d
end

.dronesObject



114
115
116
# File 'lib/app_drone/drone.rb', line 114

def drones
  self.descendants.map(&:to_sym)
end

.generator_methodObject



112
# File 'lib/app_drone/drone.rb', line 112

def generator_method; @generator_method end

.human_nameObject



126
127
128
# File 'lib/app_drone/drone.rb', line 126

def human_name
  self.to_sym.to_s.humanize.titleize
end

.owns_generator_method(m) ⇒ Object



111
# File 'lib/app_drone/drone.rb', line 111

def owns_generator_method(m); @generator_method = m end

.pairsObject



102
103
104
# File 'lib/app_drone/drone.rb', line 102

def pairs
  (@pairs || []).map(&:to_app_drone_class)
end

.pairs_with(*klass_symbols) ⇒ Object



101
# File 'lib/app_drone/drone.rb', line 101

def pairs_with(*klass_symbols); @pairs = klass_symbols end

.param(name, type, *options) ⇒ Object



76
77
78
# File 'lib/app_drone/drone.rb', line 76

def param(name, type, *options)
  (@params ||= []) << Param.new(name, type, options.first)
end

.param_named(n) ⇒ Object



80
# File 'lib/app_drone/drone.rb', line 80

def param_named(n); params.find { |p| p.name == n } end

.param_with(drone_klass, name, type, *options) ⇒ Object



72
73
74
# File 'lib/app_drone/drone.rb', line 72

def param_with(drone_klass, name, type, *options)
  param(name,type,options.first.merge({ with: drone_klass }))
end

.paramsObject



79
# File 'lib/app_drone/drone.rb', line 79

def params; @params || [] end

.precursorsObject



107
108
109
# File 'lib/app_drone/drone.rb', line 107

def precursors
  (@precursors || []).map(&:to_app_drone_class)
end

.run_after(*klass_symbols) ⇒ Object



106
# File 'lib/app_drone/drone.rb', line 106

def run_after(*klass_symbols); @precursors = klass_symbols end

.to_symObject



122
123
124
# File 'lib/app_drone/drone.rb', line 122

def to_sym
  self.to_s.split('::').last.underscore.to_sym
end

Instance Method Details

#>>(klass) ⇒ Object



24
# File 'lib/app_drone/drone.rb', line 24

def >>(klass); @template.hook(klass); end

#^Object

DSL



23
# File 'lib/app_drone/drone.rb', line 23

def ^; @template end

#alignObject

Expected implementations



45
# File 'lib/app_drone/drone.rb', line 45

def align; end

#do!(partial) ⇒ Object



61
62
63
# File 'lib/app_drone/drone.rb', line 61

def do!(partial)
  @template.do! render(partial), self
end

#do_finally!(partial) ⇒ Object



65
66
67
# File 'lib/app_drone/drone.rb', line 65

def do_finally!(partial)
  @template.do_finally! render(partial), self
end

#executeObject



46
# File 'lib/app_drone/drone.rb', line 46

def execute; end

#flair!Object



34
35
36
# File 'lib/app_drone/drones/flair/flair.rb', line 34

def flair!
  flair.add(self) if pair?(:flair)
end

#notify!(notice) ⇒ Object



40
41
42
# File 'lib/app_drone/drone.rb', line 40

def notify!(notice)
  @template.notify! notice, self
end

#pair?(drone_symbol) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/app_drone/drone.rb', line 35

def pair?(drone_symbol)
  drone_klass = drone_symbol.to_app_drone_class
  return @template.hook?(drone_klass)
end

#param(sym) ⇒ Object



16
17
18
19
20
# File 'lib/app_drone/drone.rb', line 16

def param(sym)
  param_value = @params[sym]
  default_value = self.class.param_named(sym).options[:default]
  return param_value || default_value
end

#render(partial, opts = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/app_drone/drone.rb', line 51

def render(partial, opts={})
  class_name = self.class.to_sym
  template_path = "/drones/#{class_name}/#{partial}.erb"
  full_path = File.dirname(__FILE__) + template_path
  snippet = ERB.new File.read(full_path)
  output = snippet.result(binding)
  output = "# --- \n# #{self.class.to_s}\n# ---\n" + output unless opts[:skip_stamp]
  return output
end

#setupObject

Optional implementations



49
# File 'lib/app_drone/drone.rb', line 49

def setup; end