Class: ViewSpec::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/view_spec/registry.rb

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



7
8
9
10
# File 'lib/view_spec/registry.rb', line 7

def initialize
  @specs = []
  @source_files = {}
end

Instance Method Details

#add(source_path, subject, &block) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/view_spec/registry.rb', line 12

def add(source_path, subject, &block)
  delete(subject)

  source_file = source_file(source_path)
  @specs << Spec.new(source_file, subject, &block)
  @specs.last
end

#clear!Object



37
38
39
40
# File 'lib/view_spec/registry.rb', line 37

def clear!
  @specs.clear
  @source_files.clear
end

#delete(subject) ⇒ Object



32
33
34
35
# File 'lib/view_spec/registry.rb', line 32

def delete(subject)
  index = @specs.index { _1.subject == subject }
  @specs.delete_at(index) unless index.nil?
end

#find(subject) ⇒ Object



24
25
26
# File 'lib/view_spec/registry.rb', line 24

def find(subject)
  find_where({subject:})
end

#find_where(conditions) ⇒ Object



42
43
44
# File 'lib/view_spec/registry.rb', line 42

def find_where(conditions)
  where(conditions).first
end

#has?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/view_spec/registry.rb', line 28

def has?(...)
  !!find(...)
end

#source_file(path) ⇒ Object



20
21
22
# File 'lib/view_spec/registry.rb', line 20

def source_file(path)
  @source_files[path.to_s] ||= SourceFile.new(path)
end

#where(conditions) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/view_spec/registry.rb', line 46

def where(conditions)
  @specs.filter do |spec|
    !!conditions.each do |key, value|
      break false unless spec.respond_to?(key) && (spec.public_send(key) == value)
    end
  end
end