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.



30
31
32
33
34
35
# File 'lib/engine2/action.rb', line 30

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

.inheritObject



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

def inherit
    Class.new self do
        action_type superclass.action_type
    end
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



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

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

#arguments(args) ⇒ Object



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

def arguments args
    @meta[:arguments] = args
end

#check_static_actionObject

Raises:



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

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

#define_invoke(&blk) ⇒ Object



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

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

#dynamic?Boolean

Returns:

  • (Boolean)


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

def dynamic?
    self != @static
end

#execute(command) ⇒ Object



75
76
77
# File 'lib/engine2/action.rb', line 75

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

#freeze_actionObject



110
111
112
113
114
115
# File 'lib/engine2/action.rb', line 110

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

#http_methodObject



37
38
39
# File 'lib/engine2/action.rb', line 37

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

#invoke!(handler) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/engine2/action.rb', line 54

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 || {}) : action.invoke(handler)
        response[:meta] = action.meta
        response
    else
        invoke(handler)
    end
end

#lookup(*keys) ⇒ Object

def []= *keys, value

@meta.path!(*keys, value)

end



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

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



101
102
103
104
105
106
107
108
# File 'lib/engine2/action.rb', line 101

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



133
134
# File 'lib/engine2/action.rb', line 133

def node_defined
end

#post_processObject



151
152
# File 'lib/engine2/action.rb', line 151

def post_process
end

#post_runObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/engine2/action.rb', line 136

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



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

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

#repeat(time) ⇒ Object



67
68
69
# File 'lib/engine2/action.rb', line 67

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

#request(&blk) ⇒ Object

Raises:



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

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



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

def request_action_proc_params handler
    []
end

#split_keys(id) ⇒ Object



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

def split_keys id
    Sequel::split_keys(id)
end