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.



88
89
90
91
92
93
# File 'lib/generator_spec/matcher.rb', line 88

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.



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

def tree
  @tree
end

Instance Method Details

#descriptionObject



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

def description
  'has directory structure'
end

#directory(name, &block) ⇒ Object



95
96
97
# File 'lib/generator_spec/matcher.rb', line 95

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

#file(name, &block) ⇒ Object



99
100
101
# File 'lib/generator_spec/matcher.rb', line 99

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

#location(name) ⇒ Object



107
108
109
# File 'lib/generator_spec/matcher.rb', line 107

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

#matches?(root) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/generator_spec/matcher.rb', line 115

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



111
112
113
# File 'lib/generator_spec/matcher.rb', line 111

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

#no_file(name) ⇒ Object



103
104
105
# File 'lib/generator_spec/matcher.rb', line 103

def no_file(name)
  @negative_tree << name
end