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

Returns a new instance of ActionNode.



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

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.



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

def count
  @count
end

Instance Attribute Details

#access_blockObject (readonly)

Returns the value of attribute access_block.



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

def access_block
  @access_block
end

#meta_procObject (readonly)

Returns the value of attribute meta_proc.



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

def meta_proc
  @meta_proc
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#nodesObject (readonly)

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#recheck_accessObject (readonly)

Returns the value of attribute recheck_access.



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

def recheck_access
  @recheck_access
end

Instance Method Details

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



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

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

#[](name) ⇒ Object



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

def [] name
    @nodes[name]
end

#access!(&blk) ⇒ Object



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

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

#access_forbidden!Object



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

def access_forbidden!
    access! &ACCESS_FORBIDDEN
end

#access_info(handler) ⇒ Object



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

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

#check_access!(handler) ⇒ Object



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

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

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



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

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



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

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



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

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



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

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



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

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

#each_node(&blk) ⇒ Object



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

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

#inspectObject



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

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

#nodes_info(handler) ⇒ Object



94
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
# File 'lib/engine2/action_node.rb', line 94

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



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

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

#recheck_access!Object



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

def recheck_access!
    @recheck_access = true
end

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



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

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

#setup_node_treeObject



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

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}, Thefts: #{thefts}, Time: #{::Time.now - time}"
end

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

optimize



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

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