Module: DSeL::API::Node::ClassMethods

Defined in:
lib/dsel/api/node.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
# File 'lib/dsel/api/node.rb', line 224

def method_missing( m, *args, &block )
    ms = m.to_s
    if ms.start_with? 'def_'
        to_define = ms.split( 'def_', 2 ).last
        define to_define

        send m, *args, &block
    else
        super( m, *args, &block )
    end
end

Instance Method Details

#branchObject



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/dsel/api/node.rb', line 142

def branch
    t = {
        definers:      self.definers,
        call_handlers: self.call_handlers.map { |h| c = h.dup; c.delete( :method ); c },
        children:      {}
    }

    self.children.each do |name, child|
        t[:children][name] = child.merge( child[:node].branch )
    end

    t
end

#call_handlersObject



55
56
57
# File 'lib/dsel/api/node.rb', line 55

def call_handlers
    @call_handlers ||= []
end

#child?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/dsel/api/node.rb', line 63

def child?
    !root?
end

#childrenObject



134
135
136
# File 'lib/dsel/api/node.rb', line 134

def children
    @children ||= {}
end

#configure(options, &block) ⇒ Object



43
44
45
# File 'lib/dsel/api/node.rb', line 43

def configure( options, &block )
    @last_options = [options, block].compact
end

#define(*types) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dsel/api/node.rb', line 15

def define( *types )
    if types.size > 1
        if has_options?
            fail ArgumentError,
                 "Cannot set options for multiple types: #{types}"
        end

        if has_description?
            fail ArgumentError,
                 "Cannot set description for multiple types: #{types}"
        end
    end

    Generator.define_definers( self, *types )
end

#definersObject



47
48
49
# File 'lib/dsel/api/node.rb', line 47

def definers
    @definers ||= []
end

#describe(description) ⇒ Object



39
40
41
# File 'lib/dsel/api/node.rb', line 39

def describe( description )
    @last_description = description
end

#flush_descriptionObject



157
158
159
160
161
# File 'lib/dsel/api/node.rb', line 157

def flush_description
    d = @last_description
    @last_description = nil
    d
end

#flush_optionsObject



164
165
166
167
168
# File 'lib/dsel/api/node.rb', line 164

def flush_options
    o = @last_options
    @last_options = nil
    o
end

#has_call_handler?(type, *possible_object) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dsel/api/node.rb', line 51

def has_call_handler?( type, *possible_object )
    method_defined? Generator.call_handler_name( type, *possible_object )
end

#has_description?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/dsel/api/node.rb', line 35

def has_description?
    !!@last_description
end

#has_options?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/dsel/api/node.rb', line 31

def has_options?
    !!@last_options
end

#parentObject



79
80
81
# File 'lib/dsel/api/node.rb', line 79

def parent
    @parent
end

#push_call_handler(type, method, *possible_object) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/dsel/api/node.rb', line 171

def push_call_handler( type, method, *possible_object )
    handler = {
        type: type.to_sym
    }

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

    if (options = self.flush_options)
        handler.merge!( options: options )
    end

    if (description = self.flush_description)
        handler.merge!( description: description )
    end

    handler.merge!(
        method: method.to_sym
    )

    call_handlers << handler
    handler
end

#push_child(name, node, s = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
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/node.rb', line 91

def push_child( name, node, s = nil )
    if s && !s.is_a?( Symbol ) && !s.respond_to?( :call )
        fail ArgumentError, 'Subject not Symbol nor responds to #call.'
    end

    node.set_parent( self )

    child = {
        name: name.to_sym,
        node: node
    }

    if (options = self.flush_options)
        child.merge!( options: options )
    end

    if (description = self.flush_description)
        child.merge!( description: description )
    end

    children[name] = child

    define_method name do
        ivar = "@#{name}"

        v = instance_variable_get( ivar )
        return v if v

        sub = @subject
        if s.is_a?( Symbol )
            sub = sub.send( s )
        end

        if s.respond_to?( :call )
            sub = s.call( sub )
        end

        instance_variable_set( ivar, node.new( sub, parent: self ) )
    end

    child
end

#push_children(c) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/dsel/api/node.rb', line 83

def push_children( c )
    c.each do |name, (klass, *args)|
        push_child( name, klass, *args )
    end

    nil
end

#push_definer(type, method) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/dsel/api/node.rb', line 203

def push_definer( type, method )
    definer = {
        type: type.to_sym
    }

    if (options = self.flush_options)
        definer.merge!( options: options )
    end

    if (description = self.flush_description)
        definer.merge!( description: description )
    end

    definer.merge!(
        method: method.to_sym
    )

    definers << definer
    definer
end

#rootObject



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

def root
    return self if root?

    @root ||= begin
        p = @parent
        while p.parent
            p = p.parent
        end
        p
    end
end

#root?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/dsel/api/node.rb', line 59

def root?
    !parent
end

#run(*args, &block) ⇒ Object



11
12
13
# File 'lib/dsel/api/node.rb', line 11

def run( *args, &block )
    DSL::Nodes::API.new( new ).run( *args, &block )
end

#set_parent(node) ⇒ Object



197
198
199
200
# File 'lib/dsel/api/node.rb', line 197

def set_parent( node )
    fail if @parent
    @parent = node
end

#treeObject



138
139
140
# File 'lib/dsel/api/node.rb', line 138

def tree
    root.branch
end