Class: Walker

Inherits:
AST::Processor
  • Object
show all
Defined in:
lib/ember/appkit/rails/walker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, path, api_version) ⇒ Walker

Returns a new instance of Walker



6
7
8
9
10
11
12
13
# File 'lib/ember/appkit/rails/walker.rb', line 6

def initialize(resource, path, api_version)
  @resource = resource
  @file = File.open(path, 'r+')
  @content = @file.read
  @ast = Parser::CurrentRuby.parse(@content)
  @api_version = api_version
  @found_nodes = []
end

Instance Attribute Details

#found_nodesObject

Returns the value of attribute found_nodes



4
5
6
# File 'lib/ember/appkit/rails/walker.rb', line 4

def found_nodes
  @found_nodes
end

#namespace_typeObject

Returns the value of attribute namespace_type



4
5
6
# File 'lib/ember/appkit/rails/walker.rb', line 4

def namespace_type
  @namespace_type
end

Instance Method Details

#handler_missing(node) ⇒ Object



15
16
17
# File 'lib/ember/appkit/rails/walker.rb', line 15

def handler_missing(node)
  walk(node)
end

#invoke!Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ember/appkit/rails/walker.rb', line 54

def invoke!
  process(@ast)
  if found_nodes.empty?
    found_nodes << @ast
  end
  node = found_nodes.last
  begin_pos = node.loc.begin.end_pos

  output = ''

  if namespace_type
    if namespace_type == :version
      output = write_resource
    else
      output = write_version_namespace do
        write_resource
      end
    end
  else
    output = write_api_namespace do
      write_version_namespace do
        write_resource
      end
    end
  end

  @file.rewind
  @file.write(@content.insert(begin_pos + 1, output + "\n"))
  @file.close
end

#on_block(node) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ember/appkit/rails/walker.rb', line 19

def on_block(node)
  if is_namespace?(node)
    if is_namespace_type?(node, :api)
      found_nodes << node
      self.namespace_type = :api
      walk_namespace(node)
    elsif is_namespace_type?(node, "v#{@api_version}".to_sym)
      found_nodes << node
      self.namespace_type = :version
      walk_namespace(node)
    end
  else
    walk(node)
  end
end

#on_send(node) ⇒ Object



35
36
37
38
39
40
# File 'lib/ember/appkit/rails/walker.rb', line 35

def on_send(node)
  if is_resource?(node, @resource.to_sym)
    @found_resource = true
    found_nodes << node
  end
end

#revoke!Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ember/appkit/rails/walker.rb', line 85

def revoke!
  @revoke = true
  process(@ast)
  return if found_nodes.empty? || @found_resource.nil?
  if found_nodes.last.loc.expression
    begin_pos = found_nodes.last.loc.expression.begin_pos
    end_pos = found_nodes.last.loc.expression.end_pos
  else
    begin_pos = found_nodes.last.loc.begin.begin_pos
    end_pos = found_nodes.last.loc.end.end_pos
  end
  begin_pos = begin_pos - @content[0..begin_pos].reverse.index("\n")
  @file.rewind
  @file.write @content.sub(@content[begin_pos...end_pos], '')
  @file.close
end

#write_api_namespaceObject



42
43
44
# File 'lib/ember/appkit/rails/walker.rb', line 42

def write_api_namespace
  "  namespace :api do\n#{yield}\n  end"
end

#write_resourceObject



50
51
52
# File 'lib/ember/appkit/rails/walker.rb', line 50

def write_resource
  "      resources :#{@resource}, except: [:new, :edit]"
end

#write_version_namespaceObject



46
47
48
# File 'lib/ember/appkit/rails/walker.rb', line 46

def write_version_namespace
  "    namespace :v#{@api_version} do\n#{yield}\n    end"
end