Class: DSeL::API::Generator

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/dsel/api/generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



53
54
55
# File 'lib/dsel/api/generator.rb', line 53

def initialize
    reset
end

Instance Attribute Details

#last_callHash (readonly)

Returns Data on the last call made.

Returns:

  • (Hash)

    Data on the last call made.



12
13
14
# File 'lib/dsel/api/generator.rb', line 12

def last_call
  @last_call
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



264
265
266
267
268
269
270
# File 'lib/dsel/api/generator.rb', line 264

def method_missing( sym, *args, &block )
    if instance.respond_to?( sym )
        instance.send( sym, *args, &block )
    else
        super( sym, *args, &block )
    end
end

.respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


272
273
274
# File 'lib/dsel/api/generator.rb', line 272

def respond_to?( *args )
    super || instance.respond_to?( *args )
end

Instance Method Details

#call_handler_catch_all_name(type) ⇒ Object



152
153
154
# File 'lib/dsel/api/generator.rb', line 152

def call_handler_catch_all_name( type )
    "_#{type}_catch_all_#{token}".to_sym
end

#call_handler_name(type, *possible_object) ⇒ Object



140
141
142
143
144
# File 'lib/dsel/api/generator.rb', line 140

def call_handler_name( type, *possible_object )
    possible_object.empty? ?
        call_handler_catch_all_name( type ) :
        call_handler_with_object_name( type, possible_object.first )
end

#call_handler_with_object_name(type, object) ⇒ Object



147
148
149
# File 'lib/dsel/api/generator.rb', line 147

def call_handler_with_object_name( type, object )
    "_#{type}_#{call_object_hash_for( object )}_#{token}".to_sym
end

#call_object_hasher=(hasher) ⇒ Object

Note:

Defaults to `Object#hash` and assumes case-insensitive strings.

Sets a way to calculate unique routing hashes for call objects.

Parameters:

  • hasher (Symbol, #call)
    • `Symbol`: Object method.

    • `#call`: To be passed each object and return an Integer hash.



43
44
45
46
47
48
49
50
# File 'lib/dsel/api/generator.rb', line 43

def call_object_hasher=( hasher )
    if hasher && !(hasher.is_a?( Symbol ) || hasher.respond_to?( :call ))
        fail ArgumentError,
             "Expected Symbol or #call-able hasher, got: #{hasher.inspect}"
    end

    @call_object_hasher = hasher
end

#calling(node, type, handler, args, *possible_object, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/dsel/api/generator.rb', line 101

def calling( node, type, handler, args, *possible_object, &block )
    synchronize do
        @last_call = {
            node: node,
            type: type
        }

        if !possible_object.empty?
            @last_call.merge!( object: possible_object.first )
        end

        @last_call.merge!(
            handler: handler,
            args:    args
        )

        if last_call_with_caller?
            @last_call[:caller] = caller
        end
    end

    t = Time.now
    r = block.call
    spent = Time.now - t

    synchronize do
        @last_call[:time] = spent
        call_on_call( @last_call )
    end

    r
end

#define_call_handler(node, type, *possible_object, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dsel/api/generator.rb', line 80

def define_call_handler( node, type, *possible_object, &block )
    node.instance_eval do
        handler = Generator.call_handler_name( type, *possible_object )

        if method_defined?( handler )
            fail ArgumentError,
                 'Call handler already exists: ' <<
                     Generator.handler_to_s( self, type, *possible_object )
        end

        # We can get the options from here and do stuff...I don't know.
        push_call_handler( type, handler, *possible_object )
        define_method handler, &block
    end

    define_call_router( node, type )

    nil
end

#define_definers(node, *types) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/dsel/api/generator.rb', line 69

def define_definers( node, *types )
    synchronize do
        types.each do |type|
            define_definer( node, type )
        end
    end

    nil
end

#definer_name(type) ⇒ Object



135
136
137
# File 'lib/dsel/api/generator.rb', line 135

def definer_name( type )
    "def_#{type}".to_sym
end

#handler_to_s(node, type, *possible_object) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/dsel/api/generator.rb', line 157

def handler_to_s( node, type, *possible_object )
    r = "#{node} #{type}"

    if !possible_object.empty?
        object = possible_object.first
        r << ' '
        r << (object.nil? ? 'nil' : object.to_s)
    end

    r
end

#last_call_with_caller!Object



22
23
24
# File 'lib/dsel/api/generator.rb', line 22

def last_call_with_caller!
    @last_call_with_caller = true
end

#last_call_with_caller?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/dsel/api/generator.rb', line 14

def last_call_with_caller?
    @last_call_with_caller
end

#last_call_without_caller!Object



18
19
20
# File 'lib/dsel/api/generator.rb', line 18

def last_call_without_caller!
    @last_call_with_caller = false
end

#on_call(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/dsel/api/generator.rb', line 26

def on_call( &block )
    fail ArgumentError, 'Missing &block' if !block

    synchronize do
        @on_call << block
    end

    self
end

#resetObject



58
59
60
61
62
63
64
65
66
# File 'lib/dsel/api/generator.rb', line 58

def reset
    @mutex   = Monitor.new
    @on_call = []

    @last_call_with_caller = false
    @call_object_hasher    = false

    self
end