Class: Cts::Mpx::Aci::Tasks::Image

Inherits:
Object
  • Object
show all
Includes:
Creatable
Defined in:
lib/cts/mpx/aci/tasks/image.rb

Overview

Image class for gathering a set of service data as a single collection.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImage

Returns a new instance of Image.



60
61
62
63
64
65
66
67
68
# File 'lib/cts/mpx/aci/tasks/image.rb', line 60

def initialize
  @account_id = ""
  @entries = Entries.new
  @date_taken = Time.now
  @files = {}
  @state = :untransformed
  @schema = 1
  @user = nil
end

Instance Attribute Details

#account_idString (readonly)

Returns relative account_id or nil when untransformed.

Returns:

  • (String)

    relative account_id or nil when untransformed



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cts/mpx/aci/tasks/image.rb', line 19

class Image
  include Creatable

  attribute  name: 'entries', kind_of: Entries
  attribute  name: 'schema', kind_of: Integer
  attribute  name: 'user', kind_of: User
  attribute  name: 'account_id', kind_of: String
  attribute  name: 'date_taken', kind_of: Time
  attribute  name: 'state', kind_of: String

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::aciTasks::Image] image with entries loaded and info set.
  def self.load_from_directory(directory, user = nil)
    i = new
    i.load_from_directory directory, user
    i
  end

  # All entries in the entries, including the md5 hash of the data.
  #
  # @return [Hash] filepath is key, value is md5
  def files
    entries.map(&:filepath)
  end

  # Generate information report for the image.
  #
  # @return [Hash] contains the information about the image.
  def info
    {
      account_id: @account_id,
      date_taken: @date_taken.iso8601,
      username:   @user.nil? ? "" : @user.username,
      schema:     @schema,
      state:      @state,
      files:      files
    }
  end

  def initialize
    @account_id = ""
    @entries = Entries.new
    @date_taken = Time.now
    @files = {}
    @state = :untransformed
    @schema = 1
    @user = nil
  end

  # save an image to a directory
  #
  # @param directory [String] the name of the directory to save to
  def save_to_directory(directory)
    entries.each do |entry|
      FileUtils.mkdir_p "#{directory}/#{entry.directory}"
      File.write "#{directory}/#{entry.filepath}", entry.to_s
    end

    File.write "#{directory}/info.json", Oj.dump(info, indent: 2)
    true
  end

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::Aci::Tasks::Image] image with entries loaded and info set.
  def load_from_directory(directory, user = nil)
    raise "#{directory} does not contain a valid image." unless Validators.image_directory? directory

    info = load_json_or_error "#{directory}/info.json"

    ["date_taken", "account_id", "schema", "username", "state"].each do |param|
      instance_variable_set "@#{param}", info[param]
    end

    entries = Entries.new

    info["files"].each do |file|
      begin
        h = load_json_or_error("#{directory}/#{file}")
      rescue Oj::ParseError
        raise "#{directory}/#{file} is readable, but not parsable.   Please run the json through a linter."
      end

      entries.add Entry.create(fields: Fields.create_from_data(data: h[:entry], xmlns: h[:xmlns]))
    end

    @user = user

    true
  end

  # merge two images together
  #
  # @param other_image [Image] Image to merge from
  # @return [Cts::Mpx::Aci::Tasks::Image] new image containg merged results.
  def merge(other_image)
    raise 'an image class must be supplied' unless other_image.is_a? Image
    raise 'cannot merge if the user is different' unless other_image.user == user
    raise 'cannot merge if the account_id is different' unless other_image. == 
    raise 'cannot merge if the state is different' unless other_image.state == state

    new_image = Image.new
    new_image.user = @user
    new_image.entries = entries + other_image.entries
    new_image
  end

  # transform an image to an abstract state
  def transform
    entries.each { |entry| entry.transform user }
    @state = :transformed
    @account_id = nil
    true
  end

  # untransform an image from an abstract state
  # @param target_account [String] account_id to transform to
  def untransform()
    entries.each do |entry|
      entry.fields['ownerId'] = 
      entry.untransform user, 
    end

    @state = :untransformed
    @account_id = 
    true
  end

  private

  def load_json_or_error(file)
    Oj.load File.read file
  rescue Oj::ParseError => exception
    raise "#{exception.message.split(' [').first}: #{file}"
  end
end

#date_takenDateTime (readonly)

Returns Date the image was instantiated.

Returns:

  • (DateTime)

    Date the image was instantiated



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cts/mpx/aci/tasks/image.rb', line 19

class Image
  include Creatable

  attribute  name: 'entries', kind_of: Entries
  attribute  name: 'schema', kind_of: Integer
  attribute  name: 'user', kind_of: User
  attribute  name: 'account_id', kind_of: String
  attribute  name: 'date_taken', kind_of: Time
  attribute  name: 'state', kind_of: String

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::aciTasks::Image] image with entries loaded and info set.
  def self.load_from_directory(directory, user = nil)
    i = new
    i.load_from_directory directory, user
    i
  end

  # All entries in the entries, including the md5 hash of the data.
  #
  # @return [Hash] filepath is key, value is md5
  def files
    entries.map(&:filepath)
  end

  # Generate information report for the image.
  #
  # @return [Hash] contains the information about the image.
  def info
    {
      account_id: @account_id,
      date_taken: @date_taken.iso8601,
      username:   @user.nil? ? "" : @user.username,
      schema:     @schema,
      state:      @state,
      files:      files
    }
  end

  def initialize
    @account_id = ""
    @entries = Entries.new
    @date_taken = Time.now
    @files = {}
    @state = :untransformed
    @schema = 1
    @user = nil
  end

  # save an image to a directory
  #
  # @param directory [String] the name of the directory to save to
  def save_to_directory(directory)
    entries.each do |entry|
      FileUtils.mkdir_p "#{directory}/#{entry.directory}"
      File.write "#{directory}/#{entry.filepath}", entry.to_s
    end

    File.write "#{directory}/info.json", Oj.dump(info, indent: 2)
    true
  end

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::Aci::Tasks::Image] image with entries loaded and info set.
  def load_from_directory(directory, user = nil)
    raise "#{directory} does not contain a valid image." unless Validators.image_directory? directory

    info = load_json_or_error "#{directory}/info.json"

    ["date_taken", "account_id", "schema", "username", "state"].each do |param|
      instance_variable_set "@#{param}", info[param]
    end

    entries = Entries.new

    info["files"].each do |file|
      begin
        h = load_json_or_error("#{directory}/#{file}")
      rescue Oj::ParseError
        raise "#{directory}/#{file} is readable, but not parsable.   Please run the json through a linter."
      end

      entries.add Entry.create(fields: Fields.create_from_data(data: h[:entry], xmlns: h[:xmlns]))
    end

    @user = user

    true
  end

  # merge two images together
  #
  # @param other_image [Image] Image to merge from
  # @return [Cts::Mpx::Aci::Tasks::Image] new image containg merged results.
  def merge(other_image)
    raise 'an image class must be supplied' unless other_image.is_a? Image
    raise 'cannot merge if the user is different' unless other_image.user == user
    raise 'cannot merge if the account_id is different' unless other_image. == 
    raise 'cannot merge if the state is different' unless other_image.state == state

    new_image = Image.new
    new_image.user = @user
    new_image.entries = entries + other_image.entries
    new_image
  end

  # transform an image to an abstract state
  def transform
    entries.each { |entry| entry.transform user }
    @state = :transformed
    @account_id = nil
    true
  end

  # untransform an image from an abstract state
  # @param target_account [String] account_id to transform to
  def untransform()
    entries.each do |entry|
      entry.fields['ownerId'] = 
      entry.untransform user, 
    end

    @state = :untransformed
    @account_id = 
    true
  end

  private

  def load_json_or_error(file)
    Oj.load File.read file
  rescue Oj::ParseError => exception
    raise "#{exception.message.split(' [').first}: #{file}"
  end
end

#entriesHash

Returns set of entries to generate the image from.

Returns:

  • (Hash)

    set of entries to generate the image from



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cts/mpx/aci/tasks/image.rb', line 19

class Image
  include Creatable

  attribute  name: 'entries', kind_of: Entries
  attribute  name: 'schema', kind_of: Integer
  attribute  name: 'user', kind_of: User
  attribute  name: 'account_id', kind_of: String
  attribute  name: 'date_taken', kind_of: Time
  attribute  name: 'state', kind_of: String

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::aciTasks::Image] image with entries loaded and info set.
  def self.load_from_directory(directory, user = nil)
    i = new
    i.load_from_directory directory, user
    i
  end

  # All entries in the entries, including the md5 hash of the data.
  #
  # @return [Hash] filepath is key, value is md5
  def files
    entries.map(&:filepath)
  end

  # Generate information report for the image.
  #
  # @return [Hash] contains the information about the image.
  def info
    {
      account_id: @account_id,
      date_taken: @date_taken.iso8601,
      username:   @user.nil? ? "" : @user.username,
      schema:     @schema,
      state:      @state,
      files:      files
    }
  end

  def initialize
    @account_id = ""
    @entries = Entries.new
    @date_taken = Time.now
    @files = {}
    @state = :untransformed
    @schema = 1
    @user = nil
  end

  # save an image to a directory
  #
  # @param directory [String] the name of the directory to save to
  def save_to_directory(directory)
    entries.each do |entry|
      FileUtils.mkdir_p "#{directory}/#{entry.directory}"
      File.write "#{directory}/#{entry.filepath}", entry.to_s
    end

    File.write "#{directory}/info.json", Oj.dump(info, indent: 2)
    true
  end

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::Aci::Tasks::Image] image with entries loaded and info set.
  def load_from_directory(directory, user = nil)
    raise "#{directory} does not contain a valid image." unless Validators.image_directory? directory

    info = load_json_or_error "#{directory}/info.json"

    ["date_taken", "account_id", "schema", "username", "state"].each do |param|
      instance_variable_set "@#{param}", info[param]
    end

    entries = Entries.new

    info["files"].each do |file|
      begin
        h = load_json_or_error("#{directory}/#{file}")
      rescue Oj::ParseError
        raise "#{directory}/#{file} is readable, but not parsable.   Please run the json through a linter."
      end

      entries.add Entry.create(fields: Fields.create_from_data(data: h[:entry], xmlns: h[:xmlns]))
    end

    @user = user

    true
  end

  # merge two images together
  #
  # @param other_image [Image] Image to merge from
  # @return [Cts::Mpx::Aci::Tasks::Image] new image containg merged results.
  def merge(other_image)
    raise 'an image class must be supplied' unless other_image.is_a? Image
    raise 'cannot merge if the user is different' unless other_image.user == user
    raise 'cannot merge if the account_id is different' unless other_image. == 
    raise 'cannot merge if the state is different' unless other_image.state == state

    new_image = Image.new
    new_image.user = @user
    new_image.entries = entries + other_image.entries
    new_image
  end

  # transform an image to an abstract state
  def transform
    entries.each { |entry| entry.transform user }
    @state = :transformed
    @account_id = nil
    true
  end

  # untransform an image from an abstract state
  # @param target_account [String] account_id to transform to
  def untransform()
    entries.each do |entry|
      entry.fields['ownerId'] = 
      entry.untransform user, 
    end

    @state = :untransformed
    @account_id = 
    true
  end

  private

  def load_json_or_error(file)
    Oj.load File.read file
  rescue Oj::ParseError => exception
    raise "#{exception.message.split(' [').first}: #{file}"
  end
end

#schemaString

Returns schema of the image.

Returns:

  • (String)

    schema of the image



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cts/mpx/aci/tasks/image.rb', line 19

class Image
  include Creatable

  attribute  name: 'entries', kind_of: Entries
  attribute  name: 'schema', kind_of: Integer
  attribute  name: 'user', kind_of: User
  attribute  name: 'account_id', kind_of: String
  attribute  name: 'date_taken', kind_of: Time
  attribute  name: 'state', kind_of: String

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::aciTasks::Image] image with entries loaded and info set.
  def self.load_from_directory(directory, user = nil)
    i = new
    i.load_from_directory directory, user
    i
  end

  # All entries in the entries, including the md5 hash of the data.
  #
  # @return [Hash] filepath is key, value is md5
  def files
    entries.map(&:filepath)
  end

  # Generate information report for the image.
  #
  # @return [Hash] contains the information about the image.
  def info
    {
      account_id: @account_id,
      date_taken: @date_taken.iso8601,
      username:   @user.nil? ? "" : @user.username,
      schema:     @schema,
      state:      @state,
      files:      files
    }
  end

  def initialize
    @account_id = ""
    @entries = Entries.new
    @date_taken = Time.now
    @files = {}
    @state = :untransformed
    @schema = 1
    @user = nil
  end

  # save an image to a directory
  #
  # @param directory [String] the name of the directory to save to
  def save_to_directory(directory)
    entries.each do |entry|
      FileUtils.mkdir_p "#{directory}/#{entry.directory}"
      File.write "#{directory}/#{entry.filepath}", entry.to_s
    end

    File.write "#{directory}/info.json", Oj.dump(info, indent: 2)
    true
  end

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::Aci::Tasks::Image] image with entries loaded and info set.
  def load_from_directory(directory, user = nil)
    raise "#{directory} does not contain a valid image." unless Validators.image_directory? directory

    info = load_json_or_error "#{directory}/info.json"

    ["date_taken", "account_id", "schema", "username", "state"].each do |param|
      instance_variable_set "@#{param}", info[param]
    end

    entries = Entries.new

    info["files"].each do |file|
      begin
        h = load_json_or_error("#{directory}/#{file}")
      rescue Oj::ParseError
        raise "#{directory}/#{file} is readable, but not parsable.   Please run the json through a linter."
      end

      entries.add Entry.create(fields: Fields.create_from_data(data: h[:entry], xmlns: h[:xmlns]))
    end

    @user = user

    true
  end

  # merge two images together
  #
  # @param other_image [Image] Image to merge from
  # @return [Cts::Mpx::Aci::Tasks::Image] new image containg merged results.
  def merge(other_image)
    raise 'an image class must be supplied' unless other_image.is_a? Image
    raise 'cannot merge if the user is different' unless other_image.user == user
    raise 'cannot merge if the account_id is different' unless other_image. == 
    raise 'cannot merge if the state is different' unless other_image.state == state

    new_image = Image.new
    new_image.user = @user
    new_image.entries = entries + other_image.entries
    new_image
  end

  # transform an image to an abstract state
  def transform
    entries.each { |entry| entry.transform user }
    @state = :transformed
    @account_id = nil
    true
  end

  # untransform an image from an abstract state
  # @param target_account [String] account_id to transform to
  def untransform()
    entries.each do |entry|
      entry.fields['ownerId'] = 
      entry.untransform user, 
    end

    @state = :untransformed
    @account_id = 
    true
  end

  private

  def load_json_or_error(file)
    Oj.load File.read file
  rescue Oj::ParseError => exception
    raise "#{exception.message.split(' [').first}: #{file}"
  end
end

#stateString (readonly)

Returns :transformed or :untransformed.

Returns:

  • (String)

    :transformed or :untransformed



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cts/mpx/aci/tasks/image.rb', line 19

class Image
  include Creatable

  attribute  name: 'entries', kind_of: Entries
  attribute  name: 'schema', kind_of: Integer
  attribute  name: 'user', kind_of: User
  attribute  name: 'account_id', kind_of: String
  attribute  name: 'date_taken', kind_of: Time
  attribute  name: 'state', kind_of: String

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::aciTasks::Image] image with entries loaded and info set.
  def self.load_from_directory(directory, user = nil)
    i = new
    i.load_from_directory directory, user
    i
  end

  # All entries in the entries, including the md5 hash of the data.
  #
  # @return [Hash] filepath is key, value is md5
  def files
    entries.map(&:filepath)
  end

  # Generate information report for the image.
  #
  # @return [Hash] contains the information about the image.
  def info
    {
      account_id: @account_id,
      date_taken: @date_taken.iso8601,
      username:   @user.nil? ? "" : @user.username,
      schema:     @schema,
      state:      @state,
      files:      files
    }
  end

  def initialize
    @account_id = ""
    @entries = Entries.new
    @date_taken = Time.now
    @files = {}
    @state = :untransformed
    @schema = 1
    @user = nil
  end

  # save an image to a directory
  #
  # @param directory [String] the name of the directory to save to
  def save_to_directory(directory)
    entries.each do |entry|
      FileUtils.mkdir_p "#{directory}/#{entry.directory}"
      File.write "#{directory}/#{entry.filepath}", entry.to_s
    end

    File.write "#{directory}/info.json", Oj.dump(info, indent: 2)
    true
  end

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::Aci::Tasks::Image] image with entries loaded and info set.
  def load_from_directory(directory, user = nil)
    raise "#{directory} does not contain a valid image." unless Validators.image_directory? directory

    info = load_json_or_error "#{directory}/info.json"

    ["date_taken", "account_id", "schema", "username", "state"].each do |param|
      instance_variable_set "@#{param}", info[param]
    end

    entries = Entries.new

    info["files"].each do |file|
      begin
        h = load_json_or_error("#{directory}/#{file}")
      rescue Oj::ParseError
        raise "#{directory}/#{file} is readable, but not parsable.   Please run the json through a linter."
      end

      entries.add Entry.create(fields: Fields.create_from_data(data: h[:entry], xmlns: h[:xmlns]))
    end

    @user = user

    true
  end

  # merge two images together
  #
  # @param other_image [Image] Image to merge from
  # @return [Cts::Mpx::Aci::Tasks::Image] new image containg merged results.
  def merge(other_image)
    raise 'an image class must be supplied' unless other_image.is_a? Image
    raise 'cannot merge if the user is different' unless other_image.user == user
    raise 'cannot merge if the account_id is different' unless other_image. == 
    raise 'cannot merge if the state is different' unless other_image.state == state

    new_image = Image.new
    new_image.user = @user
    new_image.entries = entries + other_image.entries
    new_image
  end

  # transform an image to an abstract state
  def transform
    entries.each { |entry| entry.transform user }
    @state = :transformed
    @account_id = nil
    true
  end

  # untransform an image from an abstract state
  # @param target_account [String] account_id to transform to
  def untransform()
    entries.each do |entry|
      entry.fields['ownerId'] = 
      entry.untransform user, 
    end

    @state = :untransformed
    @account_id = 
    true
  end

  private

  def load_json_or_error(file)
    Oj.load File.read file
  rescue Oj::ParseError => exception
    raise "#{exception.message.split(' [').first}: #{file}"
  end
end

#userUser

Returns user to make data service calls with.

Returns:

  • (User)

    user to make data service calls with



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cts/mpx/aci/tasks/image.rb', line 19

class Image
  include Creatable

  attribute  name: 'entries', kind_of: Entries
  attribute  name: 'schema', kind_of: Integer
  attribute  name: 'user', kind_of: User
  attribute  name: 'account_id', kind_of: String
  attribute  name: 'date_taken', kind_of: Time
  attribute  name: 'state', kind_of: String

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::aciTasks::Image] image with entries loaded and info set.
  def self.load_from_directory(directory, user = nil)
    i = new
    i.load_from_directory directory, user
    i
  end

  # All entries in the entries, including the md5 hash of the data.
  #
  # @return [Hash] filepath is key, value is md5
  def files
    entries.map(&:filepath)
  end

  # Generate information report for the image.
  #
  # @return [Hash] contains the information about the image.
  def info
    {
      account_id: @account_id,
      date_taken: @date_taken.iso8601,
      username:   @user.nil? ? "" : @user.username,
      schema:     @schema,
      state:      @state,
      files:      files
    }
  end

  def initialize
    @account_id = ""
    @entries = Entries.new
    @date_taken = Time.now
    @files = {}
    @state = :untransformed
    @schema = 1
    @user = nil
  end

  # save an image to a directory
  #
  # @param directory [String] the name of the directory to save to
  def save_to_directory(directory)
    entries.each do |entry|
      FileUtils.mkdir_p "#{directory}/#{entry.directory}"
      File.write "#{directory}/#{entry.filepath}", entry.to_s
    end

    File.write "#{directory}/info.json", Oj.dump(info, indent: 2)
    true
  end

  # load an image from directory
  #
  # @param  user [String] user to set the image and entries to
  # @return [Cts::Mpx::Aci::Tasks::Image] image with entries loaded and info set.
  def load_from_directory(directory, user = nil)
    raise "#{directory} does not contain a valid image." unless Validators.image_directory? directory

    info = load_json_or_error "#{directory}/info.json"

    ["date_taken", "account_id", "schema", "username", "state"].each do |param|
      instance_variable_set "@#{param}", info[param]
    end

    entries = Entries.new

    info["files"].each do |file|
      begin
        h = load_json_or_error("#{directory}/#{file}")
      rescue Oj::ParseError
        raise "#{directory}/#{file} is readable, but not parsable.   Please run the json through a linter."
      end

      entries.add Entry.create(fields: Fields.create_from_data(data: h[:entry], xmlns: h[:xmlns]))
    end

    @user = user

    true
  end

  # merge two images together
  #
  # @param other_image [Image] Image to merge from
  # @return [Cts::Mpx::Aci::Tasks::Image] new image containg merged results.
  def merge(other_image)
    raise 'an image class must be supplied' unless other_image.is_a? Image
    raise 'cannot merge if the user is different' unless other_image.user == user
    raise 'cannot merge if the account_id is different' unless other_image. == 
    raise 'cannot merge if the state is different' unless other_image.state == state

    new_image = Image.new
    new_image.user = @user
    new_image.entries = entries + other_image.entries
    new_image
  end

  # transform an image to an abstract state
  def transform
    entries.each { |entry| entry.transform user }
    @state = :transformed
    @account_id = nil
    true
  end

  # untransform an image from an abstract state
  # @param target_account [String] account_id to transform to
  def untransform()
    entries.each do |entry|
      entry.fields['ownerId'] = 
      entry.untransform user, 
    end

    @state = :untransformed
    @account_id = 
    true
  end

  private

  def load_json_or_error(file)
    Oj.load File.read file
  rescue Oj::ParseError => exception
    raise "#{exception.message.split(' [').first}: #{file}"
  end
end

Class Method Details

.load_from_directory(directory, user = nil) ⇒ Cts::Mpx::aciTasks::Image

load an image from directory

Parameters:

  • user (String) (defaults to: nil)

    user to set the image and entries to

Returns:

  • (Cts::Mpx::aciTasks::Image)

    image with entries loaded and info set.



33
34
35
36
37
# File 'lib/cts/mpx/aci/tasks/image.rb', line 33

def self.load_from_directory(directory, user = nil)
  i = new
  i.load_from_directory directory, user
  i
end

Instance Method Details

#filesHash

All entries in the entries, including the md5 hash of the data.

Returns:

  • (Hash)

    filepath is key, value is md5



42
43
44
# File 'lib/cts/mpx/aci/tasks/image.rb', line 42

def files
  entries.map(&:filepath)
end

#infoHash

Generate information report for the image.

Returns:

  • (Hash)

    contains the information about the image.



49
50
51
52
53
54
55
56
57
58
# File 'lib/cts/mpx/aci/tasks/image.rb', line 49

def info
  {
    account_id: @account_id,
    date_taken: @date_taken.iso8601,
    username:   @user.nil? ? "" : @user.username,
    schema:     @schema,
    state:      @state,
    files:      files
  }
end

#load_from_directory(directory, user = nil) ⇒ Cts::Mpx::Aci::Tasks::Image

load an image from directory

Parameters:

  • user (String) (defaults to: nil)

    user to set the image and entries to

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cts/mpx/aci/tasks/image.rb', line 87

def load_from_directory(directory, user = nil)
  raise "#{directory} does not contain a valid image." unless Validators.image_directory? directory

  info = load_json_or_error "#{directory}/info.json"

  ["date_taken", "account_id", "schema", "username", "state"].each do |param|
    instance_variable_set "@#{param}", info[param]
  end

  entries = Entries.new

  info["files"].each do |file|
    begin
      h = load_json_or_error("#{directory}/#{file}")
    rescue Oj::ParseError
      raise "#{directory}/#{file} is readable, but not parsable.   Please run the json through a linter."
    end

    entries.add Entry.create(fields: Fields.create_from_data(data: h[:entry], xmlns: h[:xmlns]))
  end

  @user = user

  true
end

#merge(other_image) ⇒ Cts::Mpx::Aci::Tasks::Image

merge two images together

Parameters:

  • other_image (Image)

    Image to merge from

Returns:



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cts/mpx/aci/tasks/image.rb', line 117

def merge(other_image)
  raise 'an image class must be supplied' unless other_image.is_a? Image
  raise 'cannot merge if the user is different' unless other_image.user == user
  raise 'cannot merge if the account_id is different' unless other_image. == 
  raise 'cannot merge if the state is different' unless other_image.state == state

  new_image = Image.new
  new_image.user = @user
  new_image.entries = entries + other_image.entries
  new_image
end

#save_to_directory(directory) ⇒ Object

save an image to a directory

Parameters:

  • directory (String)

    the name of the directory to save to



73
74
75
76
77
78
79
80
81
# File 'lib/cts/mpx/aci/tasks/image.rb', line 73

def save_to_directory(directory)
  entries.each do |entry|
    FileUtils.mkdir_p "#{directory}/#{entry.directory}"
    File.write "#{directory}/#{entry.filepath}", entry.to_s
  end

  File.write "#{directory}/info.json", Oj.dump(info, indent: 2)
  true
end

#transformObject

transform an image to an abstract state



130
131
132
133
134
135
# File 'lib/cts/mpx/aci/tasks/image.rb', line 130

def transform
  entries.each { |entry| entry.transform user }
  @state = :transformed
  @account_id = nil
  true
end

#untransform(target_account) ⇒ Object

untransform an image from an abstract state

Parameters:

  • target_account (String)

    account_id to transform to



139
140
141
142
143
144
145
146
147
148
# File 'lib/cts/mpx/aci/tasks/image.rb', line 139

def untransform()
  entries.each do |entry|
    entry.fields['ownerId'] = 
    entry.untransform user, 
  end

  @state = :untransformed
  @account_id = 
  true
end