Module: Kantox::Roles
- Defined in:
- lib/kantox/roles.rb,
lib/kantox/roles/version.rb
Constant Summary collapse
- VERSION =
"1.2.1"
Class Attribute Summary collapse
-
.controller ⇒ Object
readonly
Returns the value of attribute controller.
Class Method Summary collapse
- .applied ⇒ Object
- .apply_all_strategies ⇒ Object
- .apply_strategies(klazz) ⇒ Object
- .apply_strategy(klazz, m, strategy) ⇒ Object
-
.configure(hos = nil) {|@options| ... } ⇒ Object
Configures Roles by hash or yaml from string or file.
-
.init(controller) ⇒ Object
Main initializer.
- .options ⇒ Object
- .patch_code(im, strategy) ⇒ Object
- .postpone_strategies(klazz) ⇒ Object
- .postponed ⇒ Object
Class Attribute Details
.controller ⇒ Object (readonly)
Returns the value of attribute controller.
21 22 23 |
# File 'lib/kantox/roles.rb', line 21 def controller @controller end |
Class Method Details
.applied ⇒ Object
194 |
# File 'lib/kantox/roles.rb', line 194 def applied ; @applied ||= {} ; end |
.apply_all_strategies ⇒ Object
61 62 63 |
# File 'lib/kantox/roles.rb', line 61 def apply_all_strategies .keys.each { |klazz| postpone_strategies klazz } end |
.apply_strategies(klazz) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/kantox/roles.rb', line 93 def apply_strategies klazz return if [klazz].nil? # no strategies to apply Kantox::Helpers.debug "Request to apply strategies to [#{klazz}]" [klazz].each do |m, strategies| next unless Kernel.const_defined? klazz Kernel.const_get(klazz).instance_methods.select do |im| im =~ Regexp.new("\\A#{m.gsub('*', '(?:.*?)')}\\z") end.each do |sim| [*strategies].each do |strategy| apply_strategy klazz, sim, strategy end end end end |
.apply_strategy(klazz, m, strategy) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/kantox/roles.rb', line 110 def apply_strategy klazz, m, strategy km = "#{klazz}##{m}" case applied[km] when :success Kantox::Helpers.debug("Trying to reapply #{strategy} to #{km}. «Skipped».") return when NilClass, :error Kantox::Helpers.debug("Will apply strategy [#{strategy}] to [#{km}]") else fail LameError.new end if (im = Kantox::Helpers.get_instance_method(km)).nil? @applied[km] = :error return end pc = patch_code im, strategy patch = " def #{im[:method][:name]} *args begin #{pc} rescue Kantox::Strategies::StrategyError => e Kantox::Helpers.info '«Denied #{klazz}##{m}» due to #{strategy} strategy.' raise Kantox::Exceptions::NotAllowed, e.message rescue Kantox::Exceptions::NotAllowed => e Kantox::Helpers.info '«Denied #{klazz}##{m}» (deep) due to #{strategy} strategy.' raise Kantox::Exceptions::NotAllowed, e.message rescue Kantox::Exceptions::StandardError => e Kantox::Helpers.catched 'Guarged «#{klazz}##{m}» throws a [kantox] exception.', e raise e rescue ::StandardError => e Kantox::Helpers.catched 'Guarged «#{klazz}##{m}» throws a [generic] exception.', e raise e end end " im[:class].send :alias_method, "∃#{im[:method][:name]}", "#{im[:method][:name]}" im[:class].class_eval patch applied[km] = :success end |
.configure(hos = nil) {|@options| ... } ⇒ Object
Configures Roles by hash or yaml from string or file. Whether code block
is passed, it is processed with @options instance.
53 54 55 56 57 58 59 |
# File 'lib/kantox/roles.rb', line 53 def configure hos = nil @options = Kantox::Helpers.merge_hash_or_string , hos yield @options if block_given? # @options.select! { |_, v| v.is_a? Enumerator } # FIXME WARNING OR LIKE apply_all_strategies # FIXME Do we need this? @options end |
.init(controller) ⇒ Object
Main initializer. This method is to be called before any controller
is instantiated to prevent leakage of controllers handled.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/kantox/roles.rb', line 26 def init controller return @controller if @controller == controller fail LameError.new("Roles were already initialized for [#{@controller}]. Tried: [#{controller}]") \ unless @controller.nil? || # The following must satisfy zeus. Zeus reloads controller class # making a comparision to fail. This is an ugly hack to f*ck zeus. Rails.env.development? && !ENV['ZEUS_MASTER_FD'].nil? && @controller.name == controller.name @controller = controller if @controller.method(:inherited).owner == @controller Kantox::Helpers.warn "#{controller}#inherited was already defined. Realiasing." # FIXME @controller.send :alias_method, "∃inherited", :inherited end @controller.class_eval ' def self.inherited subclass Kantox::Roles.postpone_strategies subclass end ' end |
.options ⇒ Object
196 |
# File 'lib/kantox/roles.rb', line 196 def ; @options ||= Hashie::Mash.new ; end |
.patch_code(im, strategy) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/kantox/roles.rb', line 152 def patch_code im, strategy Kantox::Helpers.debug "Strategy: #{strategy.class} :: [#{strategy}] " args_as_string = im[:params][:string].empty? ? '' : '*args' case strategy when String, Symbol " strategy = Kantox::Helpers.get_simple_strategy('#{strategy}') fail Kantox::Strategies::StrategyError(nil, '#{im[:method][:name]}') if strategy.nil? strategy.call(self, '#{im[:class]}##{im[:method][:name]}', *args) ∃#{im[:method][:name]} #{args_as_string} " when Array case strategy.first.to_s when 'runner' " strategy = Kantox::Helpers.get_#{strategy.first}_strategy('#{strategy.last}') fail Kantox::Strategies::StrategyError(nil, '#{im[:method][:name]}') if strategy.nil? p = self.method('∃#{im[:method][:name]}').to_proc strategy.call(self, *args, &p) " when 'lambda' " strategy = Kantox::Helpers.get_#{strategy.first}_strategy('#{strategy.last}') fail Kantox::Strategies::StrategyError(nil, '#{im[:method][:name]}') if strategy.nil? strategy.call(self, '#{im[:class]}##{im[:method][:name]}') ∃#{im[:method][:name]} #{args_as_string} " else # Will try to instantiate the class with a given name " strategy = Kantox::Helpers.get_object_strategy('#{strategy.first}', '#{strategy.last}') fail Kantox::Strategies::StrategyError(nil, '#{im[:method][:name]}') if strategy.nil? strategy.to_proc.call(self, '#{im[:class]}##{im[:method][:name]}') ∃#{im[:method][:name]} #{args_as_string} " end else fail LameError.new "Unknown strategy: #{strategy}." end end |
.postpone_strategies(klazz) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/kantox/roles.rb', line 65 def postpone_strategies klazz return if postponed[klazz.to_s] # FIXME FIXME FIXME case klazz when Class (@postponed[klazz.to_s] = TracePoint.new(:end) do |tp| if tp.self == klazz apply_strategies klazz.to_s Kantox::Helpers.info "Strategies successfully applied to #{klazz} (callback in inherited)." tp.disable end end).enable when String, Symbol if Kernel.const_defined?(klazz) apply_strategies klazz else (@postponed[klazz] = TracePoint.new(:end) do |tp| if tp.self.name == klazz apply_strategies klazz Kantox::Helpers.info "Strategies successfully applied to #{klazz} (eager waiting)." tp.disable end end).enable end else fail LameError.new("Postpone strategies were called with inknown parameter type [#{klazz} :: #{klazz.class}]") end end |
.postponed ⇒ Object
195 |
# File 'lib/kantox/roles.rb', line 195 def postponed ; @postponed ||= {} ; end |