Module: CasualAPI::DSL

Defined in:
lib/dsl.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/dsl.rb', line 13

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dsl.rb', line 66

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)
  {
    content_type: hs[:content_type],
    method: hs[:method],
    names: hs[:names],
    description: description,
    domain: hs[:domain],
    action: hs[:action],
    parameters: parameters,
    hide: hs[:hide],
  }
end

.parse(file) ⇒ Object



59
60
61
62
63
64
# File 'lib/dsl.rb', line 59

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



23
24
25
# File 'lib/dsl.rb', line 23

def action_stack
  @action_stack
end

#domain(*path, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dsl.rb', line 37

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

#echoObject



27
28
29
# File 'lib/dsl.rb', line 27

def echo
  p "W"
end

#import(path) ⇒ Object



31
32
33
34
35
# File 'lib/dsl.rb', line 31

def import(path)
  if File.exist?(path)
    open(path){|f| eval(f.read, binding) }
  end
end

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



49
50
51
52
53
54
55
56
57
# File 'lib/dsl.rb', line 49

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