Class: Engine2::Action

Inherits:
Object show all
Defined in:
lib/engine2/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, assets, static = self) ⇒ Action

Returns a new instance of Action.



33
34
35
36
37
38
# File 'lib/engine2/action.rb', line 33

def initialize node, assets, static = self
    @meta = {}
    @node = node
    @assets = assets
    @static = static
end

Instance Attribute Details

#assetsObject (readonly)

Returns the value of attribute assets.



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

def assets
  @assets
end

#invokableObject (readonly)

Returns the value of attribute invokable.



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

def invokable
  @invokable
end

#metaObject (readonly)

Returns the value of attribute meta.



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

def meta
  @meta
end

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

#staticObject (readonly)

Returns the value of attribute static.



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

def static
  @static
end

Class Method Details

.action_type(at = nil) ⇒ Object



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

def action_type at = nil
    at ? @action_type = at : @action_type
end

.http_method(hm = nil) ⇒ Object



13
14
15
# File 'lib/engine2/action.rb', line 13

def http_method hm = nil
    hm ? @http_method = hm : @http_method
end

.inherit(&blk) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/engine2/action.rb', line 21

def inherit &blk
    cls = Class.new self do
        action_type superclass.action_type
    end

    cls.instance_eval &blk if block_given?
    cls
end

.inherited(cls) ⇒ Object



17
18
19
# File 'lib/engine2/action.rb', line 17

def inherited cls
    cls.http_method http_method
end

Instance Method Details

#action_typeObject



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

def action_type
    @action_type || (raise E2Error.new("No action_type for action #{self.class}"))
end

#arguments(args) ⇒ Object



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

def arguments args
    (@meta[:arguments] ||= {}).merge! args
end

#check_anonymous_action_class(name) ⇒ Object

Raises:



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

def check_anonymous_action_class name
    raise E2Error.new("Defining method '#{name}'' for named class '#{self.class}', consider using #inherit") if self.class.name
end

#check_static_actionObject

Raises:



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

def check_static_action
    raise E2Error.new("Static action required") if dynamic?
end

#define_invoke(&blk) ⇒ Object



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

def define_invoke &blk
    check_static_action
    define_method :invoke, &blk
    # self.class.class_eval{define_method :invoke, &blk}
end

#define_method(name, &blk) ⇒ Object



56
57
58
59
# File 'lib/engine2/action.rb', line 56

def define_method name, &blk
    check_anonymous_action_class name
    self.class.class_eval{define_method name, &blk}
end

#dynamic?Boolean

Returns:

  • (Boolean)


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

def dynamic?
    self != @static
end

#execute(command) ⇒ Object



88
89
90
# File 'lib/engine2/action.rb', line 88

def execute command
    (@meta[:execute] ||= []) << command
end

#freeze_actionObject



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

def freeze_action
    hash = @meta
    hash.freeze
    # hash.each_pair{|k, v| freeze(v) if v.is_a? Hash}
    freeze
end

#http_methodObject



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

def http_method
    @http_method # || (raise E2Error.new("No http method for action #{self.class}"))
end

#invoke!(handler) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/engine2/action.rb', line 67

def invoke! handler
    if rmp = @request_action_proc
        action = self.class.new(node, assets, self)
        result = action.instance_exec(handler, *action.request_action_proc_params(handler), &rmp)
        action.post_process
        response = @requestable ? (result.is_a?(Hash) ? result : {}) : action.invoke(handler)
        response[:meta] = action.meta
        response
    else
        invoke(handler)
    end
end

#join_keys(id) ⇒ Object



171
172
173
# File 'lib/engine2/action.rb', line 171

def join_keys id
    Sequel::join_keys(id)
end

#lookup(*keys) ⇒ Object

def []= *keys, value

@meta.path!(*keys, value)

end



104
105
106
107
108
109
110
111
112
# File 'lib/engine2/action.rb', line 104

def lookup *keys
    if dynamic? # we are the request action
        value = @meta.path(*keys)
        value.nil? ? @static.meta.path(*keys) : value
        # value || @static.value.path(keys)
    else
        @meta.path(*keys)
    end
end

#merge(*keys) ⇒ Object



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

def merge *keys
    if keys.length == 1
        key = keys.first
        dynamic? ? @static.meta[key].merge(@meta[key] || {}) : @meta[key]
    else
        dynamic? ? @static.meta.path(*keys).merge(@meta.path(*keys)) : @meta.path(*keys)
    end
end

#node_definedObject



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

def node_defined
end

#post_processObject



164
165
# File 'lib/engine2/action.rb', line 164

def post_process
end

#post_runObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/engine2/action.rb', line 149

def post_run
    if respond_to? :invoke
        @invokable = true
    else
        if @request_action_proc
            @invokable = true
            @requestable = true
        else
            @meta[:invokable] = false
        end
    end
    @meta[:dynamic_meta] = true if @request_action_proc
    post_process
end

#pre_runObject



141
142
143
144
# File 'lib/engine2/action.rb', line 141

def pre_run
    @action_type = self.class.action_type
    @http_method = self.class.http_method
end

#repeat(time) ⇒ Object



80
81
82
# File 'lib/engine2/action.rb', line 80

def repeat time
    @meta[:repeat] = time
end

#request(&blk) ⇒ Object

Raises:



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

def request &blk
    raise E2Error.new("No block given for request action") unless blk
    raise E2Error.new("No request block in request action allowed") if dynamic?
    @request_action_proc = @request_action_proc ? @request_action_proc.chain_args(&blk) : blk
    nil
end

#request_action_proc_params(handler) ⇒ Object



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

def request_action_proc_params handler
    []
end

#split_keys(id) ⇒ Object



167
168
169
# File 'lib/engine2/action.rb', line 167

def split_keys id
    Sequel::split_keys(id)
end