Class: Engine2::ActionNode

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, action_class, assets) ⇒ ActionNode



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

def initialize parent, name, action_class, assets
    ActionNode.count += 1
    @number = ActionNode.count
    @parent = parent
    @name = name
    @action = action_class.new(self, assets)
    @nodes = {}
end

Class Attribute Details

.countObject

Returns the value of attribute count.



12
13
14
# File 'lib/engine2/action_node.rb', line 12

def count
  @count
end

Instance Attribute Details

#access_blockObject (readonly)

Returns the value of attribute access_block.



9
10
11
# File 'lib/engine2/action_node.rb', line 9

def access_block
  @access_block
end

#meta_procObject (readonly)

Returns the value of attribute meta_proc.



9
10
11
# File 'lib/engine2/action_node.rb', line 9

def meta_proc
  @meta_proc
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#nodesObject (readonly)

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#recheck_accessObject (readonly)

Returns the value of attribute recheck_access.



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

def recheck_access
  @recheck_access
end

Instance Method Details

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



24
25
26
27
# File 'lib/engine2/action_node.rb', line 24

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

#[](name) ⇒ Object



91
92
93
# File 'lib/engine2/action_node.rb', line 91

def [] name
    @nodes[name]
end

#access!(&blk) ⇒ Object



31
32
33
34
# File 'lib/engine2/action_node.rb', line 31

def access! &blk
    ::Kernel.raise E2Error.new("Access for node #{name} already defined") if @access_block
    @access_block = blk
end

#access_forbidden!Object



36
37
38
# File 'lib/engine2/action_node.rb', line 36

def access_forbidden!
    access! &ACCESS_FORBIDDEN
end

#access_info(handler) ⇒ Object



123
124
125
126
127
128
# File 'lib/engine2/action_node.rb', line 123

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

#check_access!(handler) ⇒ Object



40
41
42
# File 'lib/engine2/action_node.rb', line 40

def check_access! handler
    !@access_block || @access_block.(handler)
end

#define_action(name, action_class = InlineAction.inherit, assets = {}, &blk) ⇒ Object



63
64
65
66
67
# File 'lib/engine2/action_node.rb', line 63

def define_action name, action_class = InlineAction.inherit, assets = {}, &blk
    define_node name, action_class, assets do
        self.* &blk
    end
end

#define_invoke(name, action_class = InlineAction.inherit, assets = {}, &blk) ⇒ Object



69
70
71
72
73
# File 'lib/engine2/action_node.rb', line 69

def define_invoke name, action_class = InlineAction.inherit, assets = {}, &blk
    define_node name, action_class, assets do
        self.*.define_invoke &blk
    end
end

#define_node(name, action_class = InlineAction.inherit, assets = {}, &blk) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/engine2/action_node.rb', line 50

def define_node name, action_class = InlineAction.inherit, assets = {}, &blk
    ::Kernel.raise E2Error.new("ActionNode #{name} already defined") if @nodes[name]
    node = @nodes[name] = ActionNode.new(self, name, action_class, assets)
    node.*.pre_run
    define_singleton_method! name do |&ablk| # forbidden list
        node.instance_eval(&ablk) if ablk
        node
    end
    node.instance_eval(&blk) if blk
    node.*.node_defined
    node
end

#define_node_bundle(name, *nodes) ⇒ Object



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

def define_node_bundle name, *nodes
    define_singleton_method!(name) do |&blk|
        if blk
            nodes.each{|a|__send__(a, &blk)} # if @nodes[node] ?
        else
            ActionNodeBundle.new(self, nodes)
        end
    end
end

#define_singleton_method!(name, &blk) ⇒ Object



85
86
87
88
89
# File 'lib/engine2/action_node.rb', line 85

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

#each_node(&blk) ⇒ Object



134
135
136
137
138
139
# File 'lib/engine2/action_node.rb', line 134

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

#inspectObject



154
155
156
# File 'lib/engine2/action_node.rb', line 154

def inspect
    "ActionNode: #{@name}, action: #{@action.class}, action_type: #{@action.action_type}"
end

#nodes_info(handler) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/engine2/action_node.rb', line 95

def nodes_info handler
    info = nodes.reduce({}) do |h, (name, a)|
        action = a.*
        act = {
            action_type: action.action_type,
            method: action.http_method,
            number: a.number,
            terminal: a.nodes.empty?,
            meta: !action.meta.empty?
        }

        act[:access] = true if !recheck_access && a.check_access!(handler)
        act[:recheck_access] = true if a.recheck_access

        if Handler::development?
            act[:action_class] = action.class
            act[:access_block] = a.access_block if a.access_block
            act[:model] = action.assets[:model]
        end

        h[name] = act
        h
    end

    info.first[1][:default] = true unless nodes.empty?
    info
end

#p(*args) ⇒ Object



207
208
209
# File 'lib/engine2/action_node.rb', line 207

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

#recheck_access!Object



130
131
132
# File 'lib/engine2/action_node.rb', line 130

def recheck_access!
    @recheck_access = true
end

#run_scheme(name, *args, &blk) ⇒ Object



44
45
46
47
48
# File 'lib/engine2/action_node.rb', line 44

def run_scheme name, *args, &blk
    result = instance_exec(*args, &SCHEMES[name])
    result.instance_eval(&blk) if blk
    result
end

#setup_node_treeObject



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
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/engine2/action_node.rb', line 158

def setup_node_tree
    time = ::Time.now

    model_nodes = {}
    each_node do |node|
        if model = node.*.assets[:model]
            model_name = model.name.to_sym
            model_nodes[model_name] = node.to_a_rec{|a| !a.*.assets[:assoc]}
            node.run_scheme(model_name) if SCHEMES[model_name, false]
            false
        else
            true
        end
    end

    thefts = 0
    panels = 0
    each_node do |node|
        action = node.*
        model = action.assets[:model]
        assoc = action.assets[:assoc]
        if model && assoc
            if source_nodes = model_nodes[model.name.to_sym]
                source_node = source_nodes.select{|sa| sa.meta_proc && sa.*.class >= action.class}
                # source_node = source_nodes.select{|sa| sa.meta_proc && action.class <= sa.*.class}
                unless source_node.empty?
                    raise E2Error.new("Multiple action candidates for #{node.inspect} found in '#{source_node.inspect}'") if source_node.size > 1
                    # puts "#{node.inspect} => #{source_node.inspect}\n"
                    action.instance_eval(&source_node.first.meta_proc)
                    thefts += 1
                end
            end
        end

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

    each_node do |node|
        meta = node.*
        meta.post_run
        meta.freeze_action
        panels += 1 if node.*.is_a? ActionPanelSupport
        true
    end

    ::Kernel::puts "ACTION NODES: #{ActionNode.count}, Panels: #{panels},  Time: #{::Time.now - time}, Thefts: #{thefts}"
end

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

optimize



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/engine2/action_node.rb', line 141

def to_a_rec root = true, result = [], &blk # optimize
    if root && (yield self)
        result << self
        @nodes.each_pair do |n, a|
            if yield a
                result << a
                a.to_a_rec(false, result, &blk)
            end
        end
    end
    result
end