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.



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

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.



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

def count
  @count
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



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

def actions
  @actions
end

#meta_procObject (readonly)

Returns the value of attribute meta_proc.



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

def meta_proc
  @meta_proc
end

#meta_proc_chainedObject (readonly)

Returns the value of attribute meta_proc_chained.



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

def meta_proc_chained
  @meta_proc_chained
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#recheck_accessObject (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 Also known as: meta



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/engine2/action.rb', line 22

def * &blk
    if blk
        @meta_proc = if meta_proc = @meta_proc
            @meta_proc_chained = true
            ::Kernel::lambda do |obj|
                obj.instance_eval(&meta_proc)
                obj.instance_eval(&blk)
            end
        else
            blk
        end
    end
    @meta
end

#[](name) ⇒ Object



98
99
100
# File 'lib/engine2/action.rb', line 98

def [] name
    @actions[name]
end

#access!(&blk) ⇒ Object



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

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

#access_forbidden!Object



44
45
46
# File 'lib/engine2/action.rb', line 44

def access_forbidden!
    access! &ACCESS_FORBIDDEN
end

#access_info(handler) ⇒ Object



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

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

#actions_info(handler) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/engine2/action.rb', line 102

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



48
49
50
# File 'lib/engine2/action.rb', line 48

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

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



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/engine2/action.rb', line 58

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



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

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



71
72
73
74
75
# File 'lib/engine2/action.rb', line 71

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



92
93
94
95
96
# File 'lib/engine2/action.rb', line 92

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

#each_action(&blk) ⇒ Object



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

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

#inspectObject



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

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

#recheck_access!Object



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

def recheck_access!
    @recheck_access = true
end

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



52
53
54
55
56
# File 'lib/engine2/action.rb', line 52

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

#setup_action_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
# File 'lib/engine2/action.rb', line 157

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|
        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



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

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



77
78
79
80
# File 'lib/engine2/action.rb', line 77

def undefine_action name
    ::Kernel.raise E2Error.new("No action #{name} defined") unless @actions[name]
    @actions.delete(name)
end

#verify_action_usageObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/engine2/action.rb', line 203

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