Module: CasualAPI::DSL

Defined in:
lib/dsl.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object



233
234
235
236
237
238
239
240
241
242
# File 'lib/dsl.rb', line 233

def self.extended obj
  # add stacks and a context to an extended object.
  obj.instance_variable_set(:@action_stack, [])
  obj.instance_variable_set(:@domain_stack, [])
  obj.instance_variable_set(:@context_stack, [{}])
  obj.extend Action
  obj.extend Annotation
  obj.extend ExtExpression
  obj.extend SyntaxSugar
end

.normalize(hs) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/dsl.rb', line 276

def self.normalize hs
  description = hs[:description] || {}
  description[:params] ||= {}
  parameters = hs[:action].parameters.map.with_index{|(_,n),i|
    { n => {
            required: i < hs[:action].arity,
            is_file: (hs[:is_file]||[]).include?(n),
            as_file: (hs[:as_file]||[]).include?(n),
            tempfile: (hs[:tempfile]||[]).include?(n),
            is_array: (hs[:is_array]||[]).include?(n),
            is_hash: (hs[:is_hash]||[]).include?(n),
           }
    }
  }.inject({}, :merge)
  { 
    method: hs[:method],
      names: hs[:names],
      description: description,
      domain: hs[:domain],
      action: hs[:action],
      parameters: parameters,
  }
end

.parse(file) ⇒ Object



269
270
271
272
273
274
# File 'lib/dsl.rb', line 269

def self.parse(file)
  obj = Object.new
  obj.extend DSL
  obj.instance_eval(open(file){|f|f.read})
  obj.action_stack.map{|x| normalize x }
end

Instance Method Details

#action_stackObject



243
244
245
# File 'lib/dsl.rb', line 243

def action_stack
  @action_stack
end

#domain(*path, &block) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/dsl.rb', line 247

def domain(*path , &block)
  # annotations don't effect over domain
  clear_annotaion
  @domain_stack.push *path
  @context_stack.push(@context_stack.last.clone)
  block.call
  @context_stack.pop
  @domain_stack.pop path.length
  # annotations don't effect over domain
  clear_annotaion
end

#path(method, *names, &block) ⇒ Object



259
260
261
262
263
264
265
266
267
# File 'lib/dsl.rb', line 259

def path(method, *names, &block)
  meta = @context_stack.last.clone
  meta[:method] = method
  meta[:names] = names
  meta[:domain] = @domain_stack.clone
  meta[:action] = block
  clear_annotaion    # annotations effect only a path.
  @action_stack.push meta
end