Class: Engine2::Meta

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, assets, static = self) ⇒ Meta

Returns a new instance of Meta.



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

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

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



4
5
6
# File 'lib/engine2/meta.rb', line 4

def action
  @action
end

#assetsObject (readonly)

Returns the value of attribute assets.



4
5
6
# File 'lib/engine2/meta.rb', line 4

def assets
  @assets
end

#invokableObject (readonly)

Returns the value of attribute invokable.



4
5
6
# File 'lib/engine2/meta.rb', line 4

def invokable
  @invokable
end

#staticObject (readonly)

Returns the value of attribute static.



4
5
6
# File 'lib/engine2/meta.rb', line 4

def static
  @static
end

Class Method Details

.http_method(hm = nil) ⇒ Object



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

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

.inherited(cls) ⇒ Object



15
16
17
# File 'lib/engine2/meta.rb', line 15

def inherited cls
    cls.http_method http_method
end

.meta_type(mt = nil) ⇒ Object



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

def meta_type mt = nil
    mt ? @meta_type = mt : @meta_type
end

Instance Method Details

#action_definedObject



113
114
# File 'lib/engine2/meta.rb', line 113

def action_defined
end

#check_static_metaObject

Raises:



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

def check_static_meta
    raise E2Error.new("Static meta required") if dynamic?
end

#dynamic?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/engine2/meta.rb', line 58

def dynamic?
    self != @static
end

#freeze_metaObject



89
90
91
92
93
94
# File 'lib/engine2/meta.rb', line 89

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

#getObject



54
55
56
# File 'lib/engine2/meta.rb', line 54

def get
    @meta
end

#http_methodObject



29
30
31
# File 'lib/engine2/meta.rb', line 29

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

#invoke!(handler) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/engine2/meta.rb', line 41

def invoke! handler
    if rmp = @request_meta_proc
        meta = self.class.new(action, assets, self)
        meta.instance_exec(handler, *meta.request_meta_proc_params(handler), &rmp)
        meta.post_process
        response = meta.invoke(handler)
        response[:meta] = meta.get
        response
    else
        invoke(handler)
    end
end

#lookup(*keys) ⇒ Object

def []= *keys, value

@meta.path!(*keys, value)

end



70
71
72
73
74
75
76
77
78
# File 'lib/engine2/meta.rb', line 70

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

#merge(*keys) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/engine2/meta.rb', line 80

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

#meta_typeObject



33
34
35
# File 'lib/engine2/meta.rb', line 33

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

#post_processObject



121
122
# File 'lib/engine2/meta.rb', line 121

def post_process
end

#post_runObject



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

def post_run
    @invokable = respond_to?(:invoke)
    post_process
end

#pre_runObject



108
109
110
111
# File 'lib/engine2/meta.rb', line 108

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

#request(&blk) ⇒ Object

Raises:



100
101
102
103
104
105
106
# File 'lib/engine2/meta.rb', line 100

def request &blk
    raise E2Error.new("No block given for request meta") unless blk
    raise E2Error.new("Request meta already supplied") if @request_meta_proc
    raise E2Error.new("No request block in request meta allowed") if dynamic?
    @request_meta_proc = blk
    nil
end

#request_meta_proc_params(handler) ⇒ Object



96
97
98
# File 'lib/engine2/meta.rb', line 96

def request_meta_proc_params handler
    []
end

#split_keys(id) ⇒ Object



124
125
126
# File 'lib/engine2/meta.rb', line 124

def split_keys id
    Sequel::split_keys(id)
end