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



82
83
84
85
86
87
88
89
# File 'lib/zeta/instance.rb', line 82

def cache_dir
  @lock.synchronize do
    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
end

#clear_cacheObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/zeta/instance.rb', line 91

def clear_cache
  @lock.synchronize do
    # 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, "**/*.mson")].each{|f| FileUtils.rm(f) }
    Dir[File.join(cache_dir, "**/*.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
end

#config_fileObject



66
67
68
69
# File 'lib/zeta/instance.rb', line 66

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



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

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

#contracts_fulfilled?(reporter = nil) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/zeta/instance.rb', line 53

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
# File 'lib/zeta/instance.rb', line 30

def convert_all!
  @lock.synchronize do
    infrastructure.convert_all!
  end
end

#current_serviceObject



141
142
143
# File 'lib/zeta/instance.rb', line 141

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

#envObject



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

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



49
50
51
# File 'lib/zeta/instance.rb', line 49

def errors
  infrastructure.errors
end

#infrastructureObject



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

def infrastructure
  @lock.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 = {})
  @lock = Monitor.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
  @lock.synchronize do
    i = infrastructure
    clear_cache
    puts "Updating #{cache_dir}" if verbose?
    update_other_contracts
    update_own_contracts
    i.convert_all!
  end
  true
end

#update_own_contractsObject



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

def update_own_contracts
  @lock.synchronize do
    contract_files.each do |file|
      source_file = File.join(config[:contracts_path], file)
      target_file = File.join(cache_dir, config[:service_name], file)
      FileUtils.mkdir_p(File.join(cache_dir, config[:service_name]))
      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
end

#validate_object_to_consume(type, data) ⇒ Object



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

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

#validate_object_to_consume!(type, data) ⇒ Object



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

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

#validate_object_to_publish(type, data) ⇒ Object



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

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

#validate_object_to_publish!(type, data) ⇒ Object



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

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

#verbose=(val) ⇒ Object



145
146
147
# File 'lib/zeta/instance.rb', line 145

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