Class: Engine2::Action

Inherits:
BasicObject
Defined in:
lib/engine2/action.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, meta_class, assets) ⇒ Action

Returns a new instance of Action.



14
15
16
17
18
19
20
21
# File 'lib/engine2/action.rb', line 14

def initialize parent, name, meta_class, assets
    Action.count += 1
    @number = Action.count
    @parent = parent
    @name = name
    @meta = meta_class.new(self, assets)
    @actions = {}
end

Class Attribute Details

.countObject

Returns the value of attribute count.



11
12
13
# File 'lib/engine2/action.rb', line 11

def count
  @count
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



7
8
9
# File 'lib/engine2/action.rb', line 7

def actions
  @actions
end

#meta_procObject (readonly)

Returns the value of attribute meta_proc.



8
9
10
# File 'lib/engine2/action.rb', line 8

def meta_proc
  @meta_proc
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/engine2/action.rb', line 7

def name
  @name
end

#numberObject (readonly)

Returns the value of attribute number.



7
8
9
# File 'lib/engine2/action.rb', line 7

def number
  @number
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/engine2/action.rb', line 7

def parent
  @parent
end

#recheck_accessObject (readonly)

Returns the value of attribute recheck_access.



7
8
9
# File 'lib/engine2/action.rb', line 7

def recheck_access
  @recheck_access
end

Instance Method Details

#*(&blk) ⇒ Object Also known as: meta



23
24
25
26
# File 'lib/engine2/action.rb', line 23

def * &blk
    @meta_proc = @meta_proc ? @meta_proc.chain(&blk) : blk if blk
    @meta
end

#[](name) ⇒ Object



90
91
92
# File 'lib/engine2/action.rb', line 90

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



114
115
116
117
118
119
# File 'lib/engine2/action.rb', line 114

def access_info handler
    @actions.inject({}) do |h, (name, a)|
        h[name] = a.check_access!(handler)
        h
    end
end

#actions_info(handler) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/engine2/action.rb', line 94

def actions_info handler
    info = actions.inject({}) do |h, (name, a)|
        meta = a.*
        h[name] = {
            meta_type: meta.meta_type,
            method: meta.http_method,
            number: a.number,
            access: recheck_access ? nil : a.check_access!(handler),
            recheck_access: a.recheck_access,
            terminal: a.actions.empty?,
            meta: !meta.get.empty?,
            invokable: meta.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, meta_class = DummyMeta, assets = {}, &blk
    ::Kernel.raise E2Error.new("Action #{name} already defined") if @actions[name]
    action = @actions[name] = Action.new(self, name, meta_class, 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



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

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_action_invoke(name, meta_class = DummyMeta, assets = {}, &blk) ⇒ Object



68
69
70
71
72
# File 'lib/engine2/action.rb', line 68

def define_action_invoke name, meta_class = DummyMeta, assets = {}, &blk
    define_action name, meta_class, assets do
        self.*.define_singleton_method(:invoke, &blk)
    end
end

#define_action_meta(name, meta_class = DummyMeta, assets = {}, &blk) ⇒ Object



62
63
64
65
66
# File 'lib/engine2/action.rb', line 62

def define_action_meta name, meta_class = DummyMeta, assets = {}, &blk
    define_action name, meta_class, assets do
        self.* &blk
    end
end

#define_singleton_method!(name, &blk) ⇒ Object



84
85
86
87
88
# File 'lib/engine2/action.rb', line 84

def define_singleton_method! name, &blk
    class << self;self;end.instance_eval do # __realclass__
        define_method name, &blk
    end
end

#each_action(&blk) ⇒ Object



125
126
127
128
129
130
# File 'lib/engine2/action.rb', line 125

def each_action &blk
    # no self
    @actions.each_pair do |n, a|
        a.each_action(&blk) if yield a
    end
end

#inspectObject



145
146
147
# File 'lib/engine2/action.rb', line 145

def inspect
    "Action: #{@name}, meta: #{@meta.class}, meta_type: #{@meta.meta_type}"
end

#p(*args) ⇒ Object



212
213
214
# File 'lib/engine2/action.rb', line 212

def p *args
    ::Kernel::p *args
end

#recheck_access!Object



121
122
123
# File 'lib/engine2/action.rb', line 121

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_treeObject



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
187
188
189
190
191
192
193
# File 'lib/engine2/action.rb', line 149

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[model_name, false]
            false
        else
            true
        end
    end

    each_action do |action|
        meta = action.*
        model = meta.assets[:model]
        assoc = meta.assets[:assoc]
        if model && assoc
            if source_actions = model_actions[model.name.to_sym]
                source_action = source_actions.select{|sa| sa.meta_proc && sa.*.class >= meta.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"
                    meta.instance_eval(&source_action.first.meta_proc)
                end
            end
        end

        meta.instance_eval(&action.meta_proc) if action.meta_proc
        true
    end

    each_action do |action|
        action.*.post_run
        action.*.freeze_meta
        true
    end

    ::Kernel::puts "ACTIONS: #{Action.count}, Time: #{::Time.now - time}"
    # verify_action_usage
end

#to_a_rec(root = true, result = [], &blk) ⇒ Object

optimize



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/engine2/action.rb', line 132

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

#verify_action_usageObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/engine2/action.rb', line 195

def verify_action_usage
    t = ::Time.now
    each_action do |action|
        meta = action.*
        model = meta.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