Module: Zeta::Instance

Included in:
Zeta
Defined in:
lib/zeta/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/zeta/instance.rb', line 11

def config
  @config
end

Instance Method Details

#cache_dirObject



80
81
82
83
84
85
# File 'lib/zeta/instance.rb', line 80

def cache_dir
    return @cache_dir if @cache_dir
    full_path = File.expand_path(config[:contracts_cache_path])
    FileUtils.mkdir_p(full_path)
    @cache_dir = full_path
end

#clear_cacheObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/zeta/instance.rb', line 87

def clear_cache
  # I'm afraid of FileUtils.rm_rf so I'll just delete all relevant files
  # and then rmdir all empty directories.
  Dir[File.join(cache_dir, "/support/**/*.mson")].each{|f| FileUtils.rm(f) }
  Dir[File.join(cache_dir, "/support/**/*.json")].each{|f| FileUtils.rm(f) }
  Dir[File.join(cache_dir, '*')].each do |d|
    next unless File.directory?(d)
    FileUtils.rmdir(d) rescue nil
  end
end

#config_fileObject



64
65
66
67
# File 'lib/zeta/instance.rb', line 64

def config_file
  return File.expand_path(@options[:config_file]) if @options[:config_file]
  File.join(Dir.pwd, 'config', 'zeta.yml')
end

#consume_object(type, data) ⇒ Object



129
130
131
# File 'lib/zeta/instance.rb', line 129

def consume_object(type, data)
  current_service.consume_object(type, data)
end

#contracts_fulfilled?(reporter = nil) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/zeta/instance.rb', line 51

def contracts_fulfilled?(reporter = nil)
  reporter ||= Lacerda::Reporters::Stdout.new(verbose: verbose?)
  infrastructure.contracts_fulfilled?(reporter)
end

#convert_all!Object



30
31
32
33
34
35
# File 'lib/zeta/instance.rb', line 30

def convert_all!
  i = infrastructure
  @mutex.synchronize do
    i.convert_all!
  end
end

#current_serviceObject



133
134
135
# File 'lib/zeta/instance.rb', line 133

def current_service
  @current_service ||= infrastructure.services[config[:service_name]]
end

#envObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/zeta/instance.rb', line 69

def env
  return @options[:env].to_sym if @options[:env]
  if Object.const_defined?('Rails')
    Rails.env.to_sym
  else
    guessed = ENV['RAILS_ENV'] || ENV['RACK_ENV']
    raise "No environment given" unless guessed
    guessed
  end
end

#errorsObject



47
48
49
# File 'lib/zeta/instance.rb', line 47

def errors
  infrastructure.errors
end

#infrastructureObject



56
57
58
59
60
61
62
# File 'lib/zeta/instance.rb', line 56

def infrastructure
  @mutex.synchronize do
    return @infrastructure if @infrastructure
    @infrastructure = Lacerda::Infrastructure.new(data_dir: cache_dir, verbose: verbose?)
    @infrastructure
  end
end

#initialize(options = {}) ⇒ Object



13
14
15
16
# File 'lib/zeta/instance.rb', line 13

def initialize(options = {})
  @mutex = Mutex.new
  @options = options
end

#update_contractsObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/zeta/instance.rb', line 18

def update_contracts
  i = infrastructure
  @mutex.synchronize do
    clear_cache
    puts "Updating #{cache_dir}" if verbose?
    update_other_contracts
    update_own_contracts
    i.convert_all!
  end
  true
end

#update_own_contractsObject



37
38
39
40
41
42
43
44
45
# File 'lib/zeta/instance.rb', line 37

def update_own_contracts
  contract_files.each do |file|
    source_file = File.join(config[:contracts_path], file)
    target_file = File.join(cache_dir, config[:service_name], file)
    puts "cp #{source_file} #{target_file}" if verbose?
    FileUtils.rm_f(target_file)
    FileUtils.cp(source_file, target_file) if File.exists?(source_file)
  end
end

#validate_object_to_consume(type, data) ⇒ Object



125
126
127
# File 'lib/zeta/instance.rb', line 125

def validate_object_to_consume(type, data)
  current_service.validate_object_to_consume(type, data)
end

#validate_object_to_consume!(type, data) ⇒ Object



121
122
123
# File 'lib/zeta/instance.rb', line 121

def validate_object_to_consume!(type, data)
  current_service.validate_object_to_consume!(type, data)
end

#validate_object_to_publish(type, data) ⇒ Object



117
118
119
# File 'lib/zeta/instance.rb', line 117

def validate_object_to_publish(type, data)
  current_service.validate_object_to_publish(type, data)
end

#validate_object_to_publish!(type, data) ⇒ Object



113
114
115
# File 'lib/zeta/instance.rb', line 113

def validate_object_to_publish!(type, data)
  current_service.validate_object_to_publish!(type, data)
end

#verbose=(val) ⇒ Object



137
138
139
# File 'lib/zeta/instance.rb', line 137

def verbose=(val)
  @options[:verbose] = !!val
end