Class: GeneratorSpec::Matcher::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/generator_spec/matcher.rb

Direct Known Subclasses

Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = nil, &block) ⇒ Directory

Returns a new instance of Directory.



64
65
66
67
68
69
# File 'lib/generator_spec/matcher.rb', line 64

def initialize(root = nil, &block)
  @tree = {}
  @negative_tree = []
  @root = root
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#treeObject (readonly)

Returns the value of attribute tree.



62
63
64
# File 'lib/generator_spec/matcher.rb', line 62

def tree
  @tree
end

Instance Method Details

#directory(name, &block) ⇒ Object



71
72
73
# File 'lib/generator_spec/matcher.rb', line 71

def directory(name, &block)
  @tree[name] = block_given? && Directory.new(location(name), &block)
end

#file(name, &block) ⇒ Object



75
76
77
# File 'lib/generator_spec/matcher.rb', line 75

def file(name, &block)
  @tree[name] = File.new(location(name), &block)
end

#location(name) ⇒ Object



83
84
85
# File 'lib/generator_spec/matcher.rb', line 83

def location(name)
  [@root, name].compact.join("/")
end

#matches?(root) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/generator_spec/matcher.rb', line 91

def matches?(root)
  @tree.each do |file, value|
    unless value
      unless root.join(location(file)).exist?
        throw :failure, "#{root}/#{location(file)}"
      end
    else
      value.matches?(root)
    end
  end

  @negative_tree.each do |file|
    if root.join(location(file)).exist?
      throw :failure, [:not, "unexpected #{root}/#{location(file)}"]
    end
  end

  nil
end

#migration(name, &block) ⇒ Object



87
88
89
# File 'lib/generator_spec/matcher.rb', line 87

def migration(name, &block)
  @tree[name] = Migration.new(location(name), &block)
end

#no_file(name) ⇒ Object



79
80
81
# File 'lib/generator_spec/matcher.rb', line 79

def no_file(name)
  @negative_tree << name
end