Class: Cts::Mpx::Aci::Tasks::Image
- Inherits:
-
Object
- Object
- Cts::Mpx::Aci::Tasks::Image
- 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
-
#account_id ⇒ String
readonly
Relative account_id or nil when untransformed.
-
#date_taken ⇒ DateTime
readonly
Date the image was instantiated.
-
#entries ⇒ Hash
Set of entries to generate the image from.
-
#schema ⇒ String
Schema of the image.
-
#state ⇒ String
readonly
:transformed or :untransformed.
-
#user ⇒ User
User to make data service calls with.
Class Method Summary collapse
-
.load_from_directory(directory, user = nil) ⇒ Cts::Mpx::aciTasks::Image
load an image from directory.
Instance Method Summary collapse
-
#files ⇒ Hash
All entries in the entries, including the md5 hash of the data.
-
#info ⇒ Hash
Generate information report for the image.
-
#initialize ⇒ Image
constructor
A new instance of Image.
-
#load_from_directory(directory, user = nil) ⇒ Cts::Mpx::Aci::Tasks::Image
load an image from directory.
-
#merge(other_image) ⇒ Cts::Mpx::Aci::Tasks::Image
merge two images together.
-
#save_to_directory(directory) ⇒ Object
save an image to a directory.
-
#transform ⇒ Object
transform an image to an abstract state.
-
#untransform(target_account) ⇒ Object
untransform an image from an abstract state.
Constructor Details
Instance Attribute Details
#account_id ⇒ String (readonly)
Returns 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.account_id == account_id 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(target_account) entries.each do |entry| entry.fields['ownerId'] = target_account entry.untransform user, target_account end @state = :untransformed @account_id = target_account true end private def load_json_or_error(file) Oj.load File.read file rescue Oj::ParseError => exception raise "#{exception..split(' [').first}: #{file}" end end |
#date_taken ⇒ DateTime (readonly)
Returns 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.account_id == account_id 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(target_account) entries.each do |entry| entry.fields['ownerId'] = target_account entry.untransform user, target_account end @state = :untransformed @account_id = target_account true end private def load_json_or_error(file) Oj.load File.read file rescue Oj::ParseError => exception raise "#{exception..split(' [').first}: #{file}" end end |
#entries ⇒ Hash
Returns 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.account_id == account_id 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(target_account) entries.each do |entry| entry.fields['ownerId'] = target_account entry.untransform user, target_account end @state = :untransformed @account_id = target_account true end private def load_json_or_error(file) Oj.load File.read file rescue Oj::ParseError => exception raise "#{exception..split(' [').first}: #{file}" end end |
#schema ⇒ String
Returns 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.account_id == account_id 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(target_account) entries.each do |entry| entry.fields['ownerId'] = target_account entry.untransform user, target_account end @state = :untransformed @account_id = target_account true end private def load_json_or_error(file) Oj.load File.read file rescue Oj::ParseError => exception raise "#{exception..split(' [').first}: #{file}" end end |
#state ⇒ String (readonly)
Returns :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.account_id == account_id 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(target_account) entries.each do |entry| entry.fields['ownerId'] = target_account entry.untransform user, target_account end @state = :untransformed @account_id = target_account true end private def load_json_or_error(file) Oj.load File.read file rescue Oj::ParseError => exception raise "#{exception..split(' [').first}: #{file}" end end |
#user ⇒ User
Returns 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.account_id == account_id 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(target_account) entries.each do |entry| entry.fields['ownerId'] = target_account entry.untransform user, target_account end @state = :untransformed @account_id = target_account true end private def load_json_or_error(file) Oj.load File.read file rescue Oj::ParseError => exception raise "#{exception..split(' [').first}: #{file}" end end |
Class Method Details
.load_from_directory(directory, user = nil) ⇒ Cts::Mpx::aciTasks::Image
load an image from directory
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
#files ⇒ Hash
All entries in the entries, including the md5 hash of the data.
42 43 44 |
# File 'lib/cts/mpx/aci/tasks/image.rb', line 42 def files entries.map(&:filepath) end |
#info ⇒ Hash
Generate information report for 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
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
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.account_id == account_id 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
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 |
#transform ⇒ Object
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
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/cts/mpx/aci/tasks/image.rb', line 139 def untransform(target_account) entries.each do |entry| entry.fields['ownerId'] = target_account entry.untransform user, target_account end @state = :untransformed @account_id = target_account true end |