Class: Engine2::Action
- Inherits:
- BasicObject
- Defined in:
- lib/engine2/action.rb
Class Attribute Summary collapse
-
.count ⇒ Object
Returns the value of attribute count.
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#meta_proc ⇒ Object
readonly
Returns the value of attribute meta_proc.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#recheck_access ⇒ Object
readonly
Returns the value of attribute recheck_access.
Instance Method Summary collapse
- #*(&blk) ⇒ Object
- #[](name) ⇒ Object
- #access!(&blk) ⇒ Object
- #access_forbidden! ⇒ Object
- #access_info(handler) ⇒ Object
- #actions_info(handler) ⇒ Object
- #check_access!(handler) ⇒ Object
- #define_action(name, meta_class = DummyMeta, assets = {}, &blk) ⇒ Object
- #define_action_bundle(name, *actions) ⇒ Object
- #define_singleton_method!(name, &blk) ⇒ Object
- #each_action(&blk) ⇒ Object
-
#initialize(parent, name, meta_class, assets) ⇒ Action
constructor
A new instance of Action.
- #inspect ⇒ Object
- #recheck_access! ⇒ Object
- #run_scheme(name, *args, &blk) ⇒ Object
- #setup_action_tree ⇒ Object
-
#to_a_rec(root = true, result = [], &blk) ⇒ Object
optimize.
- #undefine_action(name) ⇒ Object
- #verify_action_usage ⇒ Object
Constructor Details
#initialize(parent, name, meta_class, assets) ⇒ Action
Returns a new instance of Action.
13 14 15 16 17 18 19 20 |
# File 'lib/engine2/action.rb', line 13 def initialize parent, name, , assets Action.count += 1 @number = Action.count @parent = parent @name = name @meta = .new(self, assets) @actions = {} end |
Class Attribute Details
.count ⇒ Object
Returns the value of attribute count.
10 11 12 |
# File 'lib/engine2/action.rb', line 10 def count @count end |
Instance Attribute Details
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
6 7 8 |
# File 'lib/engine2/action.rb', line 6 def actions @actions end |
#meta_proc ⇒ Object (readonly)
Returns the value of attribute meta_proc.
7 8 9 |
# File 'lib/engine2/action.rb', line 7 def @meta_proc end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/engine2/action.rb', line 6 def name @name end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
6 7 8 |
# File 'lib/engine2/action.rb', line 6 def number @number end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/engine2/action.rb', line 6 def parent @parent end |
#recheck_access ⇒ Object (readonly)
Returns the value of attribute recheck_access.
6 7 8 |
# File 'lib/engine2/action.rb', line 6 def recheck_access @recheck_access end |
Instance Method Details
#*(&blk) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/engine2/action.rb', line 22 def * &blk if blk ::Kernel.raise E2Error.new("Meta #{name} with proc already defined for action #{self.inspect}") if @meta_proc @meta_proc = blk end @meta end |
#[](name) ⇒ Object
83 84 85 |
# File 'lib/engine2/action.rb', line 83 def [] name @actions[name] end |
#access!(&blk) ⇒ Object
30 31 32 33 |
# File 'lib/engine2/action.rb', line 30 def access! &blk ::Kernel.raise E2Error.new("Access for action #{name} already defined") if @access_block @access_block = blk end |
#access_forbidden! ⇒ Object
35 36 37 |
# File 'lib/engine2/action.rb', line 35 def access_forbidden! access! &ACCESS_FORBIDDEN end |
#access_info(handler) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/engine2/action.rb', line 107 def access_info handler @actions.inject({}) do |h, (name, a)| h[name] = a.check_access!(handler) h end end |
#actions_info(handler) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/engine2/action.rb', line 87 def actions_info handler info = actions.inject({}) do |h, (name, a)| = a.* h[name] = { meta_type: ., method: .http_method, number: a.number, access: recheck_access ? nil : a.check_access!(handler), recheck_access: a.recheck_access, terminal: a.actions.empty?, meta: !.get.empty?, invokable: .invokable } h end info.first[1][:default] = true unless actions.empty? info end |
#check_access!(handler) ⇒ Object
39 40 41 |
# File 'lib/engine2/action.rb', line 39 def check_access! handler !@access_block || @access_block.(handler) end |
#define_action(name, meta_class = DummyMeta, assets = {}, &blk) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/engine2/action.rb', line 49 def define_action name, = DummyMeta, assets = {}, &blk ::Kernel.raise E2Error.new("Action #{name} already defined") if @actions[name] action = @actions[name] = Action.new(self, name, , assets) action.*.pre_run define_singleton_method! name do |&ablk| # forbidden list action.instance_eval(&ablk) if ablk action end action.instance_eval(&blk) if blk action.*.action_defined action end |
#define_action_bundle(name, *actions) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/engine2/action.rb', line 67 def define_action_bundle name, *actions define_singleton_method!(name) do |&blk| if blk actions.each{|a|__send__(a, &blk)} # if @actions[action] ? else ActionBundle.new(self, actions) end end end |
#define_singleton_method!(name, &blk) ⇒ Object
77 78 79 80 81 |
# File 'lib/engine2/action.rb', line 77 def define_singleton_method! name, &blk class << self;self;end.instance_eval do # __realclass__ define_method name, &blk end end |
#each_action(&blk) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/engine2/action.rb', line 118 def each_action &blk # no self @actions.each_pair do |n, a| a.each_action(&blk) if yield a end end |
#inspect ⇒ Object
138 139 140 |
# File 'lib/engine2/action.rb', line 138 def inspect "Action: #{@name}, meta: #{@meta.class}, meta_type: #{@meta.}" end |
#recheck_access! ⇒ Object
114 115 116 |
# File 'lib/engine2/action.rb', line 114 def recheck_access! @recheck_access = true end |
#run_scheme(name, *args, &blk) ⇒ Object
43 44 45 46 47 |
# File 'lib/engine2/action.rb', line 43 def run_scheme name, *args, &blk result = instance_exec(*args, &SCHEMES[name]) result.instance_eval(&blk) if blk result end |
#setup_action_tree ⇒ Object
142 143 144 145 146 147 148 149 150 151 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 |
# File 'lib/engine2/action.rb', line 142 def setup_action_tree time = ::Time.now model_actions = {} each_action do |action| if model = action.*.assets[:model] model_name = model.name.to_sym model.synchronize_type_info model_actions[model_name] = action.to_a_rec{|a| !a.*.assets[:assoc]} action.run_scheme(model_name) if SCHEMES.schemes[model_name] false else true end end each_action do |action| = action.* model = .assets[:model] assoc = .assets[:assoc] if model && assoc if source_actions = model_actions[model.name.to_sym] source_action = source_actions.select{|sa| sa. && sa.*.class >= .class} # source_action = source_actions.select{|sa| sa.meta_proc && meta.class <= sa.*.class} unless source_action.empty? # raise E2Error.new("Multiple meta candidates for #{action.inspect} found in '#{source_action.inspect}'") if source_action.size > 1 # puts "#{action.inspect} => #{source_action.inspect}\n" .instance_eval(&source_action.first.) end end end .instance_eval(&action.) if action. true end each_action do |action| action.*.post_run action.*. true end ::Kernel::puts "ACTIONS: #{Action.count}, Time: #{::Time.now - time}" # verify_action_usage end |
#to_a_rec(root = true, result = [], &blk) ⇒ Object
optimize
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/engine2/action.rb', line 125 def to_a_rec root = true, result = [], &blk # optimize if root && (yield self) result << self @actions.each_pair do |n, a| if yield a result << a a.to_a_rec(false, result, &blk) end end end result end |
#undefine_action(name) ⇒ Object
62 63 64 65 |
# File 'lib/engine2/action.rb', line 62 def undefine_action name ::Kernel.raise E2Error.new("No action #{name} defined") unless @actions[name] @actions.delete(name) end |
#verify_action_usage ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/engine2/action.rb', line 188 def verify_action_usage t = ::Time.now each_action do |action| = action.* model = .assets[:model] if model model.one_to_many_associations.each do |a| end true else true end end ::Kernel::puts "VERIFY #{::Time.now - t}" end |