Class: IronBank::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_bank/schema.rb

Overview

Representation of all Zuora objects and their fields for a Zuora tenant, with import/export capabilities.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Schema

Returns a new instance of Schema.



59
60
61
# File 'lib/iron_bank/schema.rb', line 59

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



57
58
59
# File 'lib/iron_bank/schema.rb', line 57

def client
  @client
end

Class Method Details

.directoryObject



10
11
12
# File 'lib/iron_bank/schema.rb', line 10

def self.directory
  IronBank.configuration.schema_directory
end

.excluded_fieldsObject



41
42
43
44
45
46
# File 'lib/iron_bank/schema.rb', line 41

def self.excluded_fields
  @excluded_fields ||= IronBank::Resources.constants.each.with_object({}) do |resource, fields|
    fields[resource.to_s] =
      IronBank::Describe::ExcludedFields.call(object_name: resource)
  end
end

.exportObject



14
15
16
17
18
19
# File 'lib/iron_bank/schema.rb', line 14

def self.export
  FileUtils.mkdir_p(directory) unless Dir.exist?(directory)
  new(IronBank.client).export
  reset
  import
end

.for(object_name) ⇒ Object



21
22
23
# File 'lib/iron_bank/schema.rb', line 21

def self.for(object_name)
  import[object_name]
end

.importObject



25
26
27
28
29
30
31
# File 'lib/iron_bank/schema.rb', line 25

def self.import
  @import ||= Dir["#{directory}/*.xml"].each.with_object({}) do |name, data|
    doc    = File.open(name) { |file| Nokogiri::XML(file) }
    object = IronBank::Describe::Object.from_xml(doc)
    data[object.name] = object
  end
end

.resetObject



33
34
35
36
37
38
39
# File 'lib/iron_bank/schema.rb', line 33

def self.reset
  remove_instance_variable(:@import)

  IronBank::Resources.constants.each do |resource|
    IronBank::Resources.const_get(resource).reset
  end
end

Instance Method Details

#exportObject



48
49
50
51
52
53
54
55
# File 'lib/iron_bank/schema.rb', line 48

def export
  tenant.objects.compact.each do |object|
    object.export
  rescue IronBank::Describe::Object::InvalidXML
    # TODO: log the object error
    next
  end
end

#tenantObject



63
64
65
# File 'lib/iron_bank/schema.rb', line 63

def tenant
  @tenant ||= IronBank::Describe::Tenant.from_connection(client.connection)
end