Class: LabelModel

Inherits:
ASpaceExport::ExportModel show all
Defined in:
backend/app/exporters/models/labels.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ASpaceExport::ExportModel

#apply_map, inherited, model_for, model_for?

Constructor Details

#initialize(obj, tree) ⇒ LabelModel

Returns a new instance of LabelModel.



31
32
33
34
35
36
37
38
39
# File 'backend/app/exporters/models/labels.rb', line 31

def initialize(obj, tree)
  @json = obj
  @tree = tree

  @file = ASUtils.tempfile('labels_')
  append(headers)

  generate_label_rows(self.children)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth) ⇒ Object



80
81
82
83
84
85
86
# File 'backend/app/exporters/models/labels.rb', line 80

def method_missing(meth)
  if @json.respond_to?(meth)
    @json.send(meth)
  else
    nil
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file



29
30
31
# File 'backend/app/exporters/models/labels.rb', line 29

def file
  @file
end

Class Method Details

.from_aspace_object(obj, tree) ⇒ Object



66
67
68
69
70
# File 'backend/app/exporters/models/labels.rb', line 66

def self.from_aspace_object(obj, tree)
  labler = self.new(obj, tree)

  labler
end

.from_resource(obj, tree) ⇒ Object



73
74
75
76
77
# File 'backend/app/exporters/models/labels.rb', line 73

def self.from_resource(obj, tree)
  labler = self.from_aspace_object(obj, tree)

  labler
end

Instance Method Details

#append(row) ⇒ Object



42
43
44
# File 'backend/app/exporters/models/labels.rb', line 42

def append(row)
  CSV.open(@file, 'a', col_sep: "\t") { |csv| csv << row }
end

#childrenObject



104
105
106
107
108
109
110
# File 'backend/app/exporters/models/labels.rb', line 104

def children
  return nil unless @tree.children

  ao_class = self.class.instance_variable_get(:@ao)

  @tree.children.map { |subtree| ao_class.new(subtree) }
end

#full_row(row) ⇒ Object



47
48
49
# File 'backend/app/exporters/models/labels.rb', line 47

def full_row(row)
  [self.repo_name, self.title, self.identifier] + row
end

#generate_label_rows(objects) ⇒ Object



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
158
159
160
161
162
163
164
165
166
167
168
169
# File 'backend/app/exporters/models/labels.rb', line 125

def generate_label_rows(objects)
  @top_containers ||= []
  @series ||= ""

  objects.each do |obj|
    @series = obj.display_string if obj.level == 'series'
    obj.instances.each do |instance|
      next unless (sub = instance['sub_container'])
      # output each unique container per series (a container may be used in multiple series)
      digest = Digest::SHA1.hexdigest(@series + sub['top_container']['ref'])
      next if @top_containers.include?(digest)
      @top_containers << digest

      # We get the Series ( the ancestor AO with the level == 'series' ) and
      # the name of the AO we're processing
      container_row = [@series, obj.display_string]

      # Top Container time
      top = sub['top_container']['_resolved']

      # this will give us:
      #  "#{name} [#{depth}d, #{height}h, #{width}w #{dimension_units}] extent measured by #{extent_dimension}"
      container_row << value_or_blank( top, %w( container_profile _resolved display_string ))

      container_row << "#{value_or_blank( top, %w( type ))}: #{value_or_blank( top, %w( indicator ))}"

      container_row << value_or_blank(top, %w( barcode ))

      # these get the grandchild SubContainers of the Top Container
      # e.g. Carton: 1 and Folder: 71
      container_row << [ value_or_blank( sub, %w( type_2 )), value_or_blank( sub, %w( indicator_2 ) ) ]
        .reject { |v| v.empty? }.join(":")
      container_row << [ value_or_blank( sub, %w( type_3 )), value_or_blank( sub, %w( indicator_3 ) ) ]
        .reject { |v| v.empty? }.join(":")


      current_location = top["container_locations"].find { |loc| loc["status"] === 'current' } || {}
      container_row << value_or_blank( current_location, %w( _resolved title  ) )

      append(full_row(container_row))
    end
    generate_label_rows(obj.children)

  end
end

#headersObject



52
53
54
55
56
57
58
# File 'backend/app/exporters/models/labels.rb', line 52

def headers
  %w(
    Repository\ Name Resource\ Title Resource\ Identifier Series\ Archival\ Object\ Title
    Archival\ Object\ Title Container\ Profile Top\ Container Top\ Container\ Barcode
    SubContainer\ 1 SubContainer\ 2 Current\ Location
  )
end

#identifierObject



88
89
90
91
92
# File 'backend/app/exporters/models/labels.rb', line 88

def identifier
  @identifier ||= [:id_0, :id_1, :id_2, :id_3].map {|i| self.send(i) }.reject {|i| i.nil? }.join("-")

  @identifier
end

#repo_nameObject



95
96
97
98
99
100
101
# File 'backend/app/exporters/models/labels.rb', line 95

def repo_name
  if self.repository && self.repository.has_key?('_resolved')
    self.repository['_resolved']['name']
  else
    "Unknown"
  end
end

#streamObject



61
62
63
# File 'backend/app/exporters/models/labels.rb', line 61

def stream
  File.open(@file).each # enumerator for stream response
end

#value_or_blank(hash, keys = []) ⇒ Object

this is a convenience method to either return either the value from a hash from an array of keys or a blank string ( if it does not exist )



115
116
117
118
119
120
121
122
123
# File 'backend/app/exporters/models/labels.rb', line 115

def value_or_blank(hash, keys = [] )
  keys.reduce(hash) do |memo, k|
    if memo.is_a?(Hash) && memo[k]
      memo[k]
    else
      ""
    end
  end
end