Class: AMEE::Admin::ItemValueDefinition

Inherits:
Object
  • Object
show all
Includes:
MetaHelper
Defined in:
lib/amee/item_value_definition.rb,
lib/amee/v3/item_value_definition.rb

Instance Attribute Summary collapse

Attributes inherited from Object

#connection, #created, #modified

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MetaHelper

#meta, #putmeta, #puttags

Methods inherited from Object

get_and_parse

Methods included from ParseHelper

#load_xml_doc, #node_value, #xmlpathpreamble

Constructor Details

#initialize(data = {}) ⇒ ItemValueDefinition

Returns a new instance of ItemValueDefinition.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/amee/item_value_definition.rb', line 59

def initialize(data = {})
  @itemdefuid=data[:itemdefuid]
  @name = data[:name]
  @uid = data[:uid]
  @path = data[:path]
  @unit = data[:unit]
  @perunit = data[:perunit]
  @valuetype = data[:valuetype]
  @default = data[:default] == "" ? nil : data[:default]
  @choices = data[:choices] || []
  @versions = data[:versions] || []
  @drill = data[:drill]
  @from_data = @drill ? false : data[:from_data] # Drills are also data, but we don't want them to show as such for clarity
  @from_profile = data[:from_profile]
  super
end

Instance Attribute Details

#choicesObject (readonly)

Returns the value of attribute choices.



76
77
78
# File 'lib/amee/item_value_definition.rb', line 76

def choices
  @choices
end

#defaultObject (readonly)

Returns the value of attribute default.



76
77
78
# File 'lib/amee/item_value_definition.rb', line 76

def default
  @default
end

#itemdefuidObject (readonly)

Returns the value of attribute itemdefuid.



76
77
78
# File 'lib/amee/item_value_definition.rb', line 76

def itemdefuid
  @itemdefuid
end

#nameObject (readonly)

Returns the value of attribute name.



76
77
78
# File 'lib/amee/item_value_definition.rb', line 76

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



76
77
78
# File 'lib/amee/item_value_definition.rb', line 76

def path
  @path
end

#perunitObject (readonly)

Returns the value of attribute perunit.



76
77
78
# File 'lib/amee/item_value_definition.rb', line 76

def perunit
  @perunit
end

#typeObject (readonly)

This function has bee deprecated in favour of the separate profile? data? and drill? functions

Raises:



79
80
81
# File 'lib/amee/item_value_definition.rb', line 79

def type
  @type
end

#uidObject (readonly)

Returns the value of attribute uid.



76
77
78
# File 'lib/amee/item_value_definition.rb', line 76

def uid
  @uid
end

#unitObject (readonly)

Returns the value of attribute unit.



76
77
78
# File 'lib/amee/item_value_definition.rb', line 76

def unit
  @unit
end

#valuetypeObject (readonly)

Returns the value of attribute valuetype.



76
77
78
# File 'lib/amee/item_value_definition.rb', line 76

def valuetype
  @valuetype
end

#versionsObject (readonly)

Returns the value of attribute versions.



76
77
78
# File 'lib/amee/item_value_definition.rb', line 76

def versions
  @versions
end

Class Method Details

.create(connection, itemdefuid, options = {}) ⇒ Object

Raises:



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/amee/item_value_definition.rb', line 209

def self.create(connection,itemdefuid, options = {})
  raise AMEE::NotSupported
  #        unless options.is_a?(Hash)
  #          raise AMEE::ArgumentError.new("Second argument must be a hash of options!")
  #        end
  #        # Send data
  #        response = connection.post("/definitions/itemDefinitions/#{itemdefuid}/itemValueDefintions", options).body
  #        # Parse response
  #        item_value_definition = ItemValueDefinition.parse(connection, response)
  #        # Get the ItemValueDefinition again
  #        return ItemValueDefinition.load(connection,itemdefuid , item_value_definition.uid)
  #      rescue
  #        raise AMEE::BadData.new("Couldn't create ItemValueDefinition. Check that your information is correct.\n#{response}")
end

.delete(connection, itemdefuid, item_value_definition) ⇒ Object

Raises:



224
225
226
227
228
229
230
231
232
233
# File 'lib/amee/item_value_definition.rb', line 224

def self.delete(connection, itemdefuid,item_value_definition)
  raise AMEE::NotSupported
  #        # Deleting takes a while... up the timeout to 120 seconds temporarily
  #        t = connection.timeout
  #        connection.timeout = 120
  #        connection.delete("/definitions/itemDefinitions/#{itemdefuid}/itemValueDefinitions/" + item_value_definition.uid)
  #        connection.timeout = t
  #      rescue
  #        raise AMEE::BadData.new("Couldn't delete ItemValueDefinition. Check that your information is correct.")
end

.from_json(json) ⇒ Object



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
# File 'lib/amee/item_value_definition.rb', line 112

def self.from_json(json)
  # Read JSON
  doc = JSON.parse(json)
  data = {}
  p=doc['itemValueDefinition']
  data[:uid] = p['uid']
  data[:itemdefuid] = p['itemDefinition']['uid']
  data[:created] = DateTime.parse(p['created'])
  data[:modified] = DateTime.parse(p['modified'])
  data[:name] = p['name']
  data[:path] = p['path']
  data[:unit] = p['unit']
  data[:perunit] = p['perUnit']
  data[:valuetype] = p['valueDefinition']['valueType']
  data[:default] = p['value']
  data[:choices] = p['choices'].split(',')
  data[:versions] = p['apiVersions'].map{|v| v['apiVersion']}
  data[:drill]=p['drillDown']
  data[:from_profile]=p['fromProfile']
  data[:from_data]=p['fromData']
  # Create object
  ItemValueDefinition.new(data)
rescue
  raise AMEE::BadData.new("Couldn't load ItemValueDefinition from JSON. Check that your URL is correct.\n#{json}")
end

.from_xml(xml, is_list = true) ⇒ Object



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
# File 'lib/amee/item_value_definition.rb', line 142

def self.from_xml(xml, is_list = true)
  prefix = is_list ? "/Resources/ItemValueDefinitionsResource/" : "/Resources/ItemValueDefinitionResource/"
  # Parse data from response into hash
  doc = REXML::Document.new(xml)
  data = {}
  data[:uid] = REXML::XPath.first(doc, prefix + "ItemValueDefinition/@uid").to_s
  data[:itemdefuid] = REXML::XPath.first(doc, prefix + "ItemValueDefinition/ItemDefinition/@uid").to_s
  data[:created] = DateTime.parse(REXML::XPath.first(doc, prefix + "ItemValueDefinition/@created").to_s)
  data[:modified] = DateTime.parse(REXML::XPath.first(doc, prefix + "ItemValueDefinition/@modified").to_s)
  data[:name] = REXML::XPath.first(doc, prefix + "ItemValueDefinition/Name").text
  data[:path] = REXML::XPath.first(doc, prefix + "ItemValueDefinition/Path").text
  uxml=REXML::XPath.first(doc, prefix + "ItemValueDefinition/Unit")
  data[:unit] = uxml ? uxml.text : nil
  puxml=data[:perunit] = REXML::XPath.first(doc, prefix + "ItemValueDefinition/PerUnit")
  data[:perunit] = puxml ? puxml.text : nil
  data[:valuetype] = REXML::XPath.first(doc, prefix + "ItemValueDefinition/ValueDefinition/ValueType").text
  data[:default] = REXML::XPath.first(doc, prefix + "ItemValueDefinition/Value").text
  data[:choices] = REXML::XPath.first(doc, prefix + "ItemValueDefinition/Choices").text.split(',') rescue nil
  data[:versions] = REXML::XPath.each(doc, prefix + "ItemValueDefinition/APIVersions/APIVersion").map{|v| v.text}
  data[:drill]=(x('DrillDown',:doc => doc)=='true')
  data[:from_profile]=(x('FromProfile',:doc => doc)=='true')
  data[:from_data]=(x('FromData',:doc => doc)=='true')
  # Create object
  ItemValueDefinition.new(data)
rescue
  raise AMEE::BadData.new("Couldn't load ItemValueDefinition from XML. Check that your URL is correct.\n#{xml}")
end

.get(connection, path, options = {}) ⇒ Object



170
171
172
173
174
175
176
177
178
179
# File 'lib/amee/item_value_definition.rb', line 170

def self.get(connection, path, options = {})
  # Load data from path
  response = connection.get(path, options).body
  # Parse response
  item_definition = ItemValueDefinition.parse(connection, response, false)
  # Done
  return item_definition
rescue
  raise AMEE::BadData.new("Couldn't load ItemValueDefinition. Check that your URL is correct.\n#{response}")
end

.list(connection) ⇒ Object



95
96
97
# File 'lib/amee/item_value_definition.rb', line 95

def self.list(connection)
  ItemValueDefinitionList.new(connection)
end

.load(connection, itemdefuid, ivduid, options = {}) ⇒ Object



183
184
185
# File 'lib/amee/item_value_definition.rb', line 183

def self.load(connection,itemdefuid,ivduid,options={})
  ItemValueDefinition.get(connection,"/definitions/itemDefinitions/#{itemdefuid}/itemValueDefinitions/#{ivduid}",options)
end

.parse(connection, response, is_list = true) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/amee/item_value_definition.rb', line 99

def self.parse(connection, response, is_list = true)
  # Parse data from response
  if response.is_json?
    item_definition = ItemValueDefinition.from_json(response)
  else
    item_definition = ItemValueDefinition.from_xml(response, is_list)
  end
  # Store connection in object for future use
  item_definition.connection = connection
  # Done
  return item_definition
end

.update(connection, path, options = {}) ⇒ Object

Raises:



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/amee/item_value_definition.rb', line 191

def self.update(connection, path, options = {})
  raise AMEE::NotSupported
  #        # Do we want to automatically fetch the item afterwards?
  #        get_item = options.delete(:get_item)
  #        get_item = true if get_item.nil?
  #        # Go
  #        response = connection.put(path, options)
  #        if get_item
  #          if response.body.empty?
  #            return ItemValueDefinition.get(connection, path)
  #          else
  #            return ItemValueDefinition.parse(connection, response.body)
  #          end
  #        end
  #      rescue
  #        raise AMEE::BadData.new("Couldn't update ItemValueDefinition. Check that your information is correct.\n#{response}")
end

.xmlpathpreambleObject



138
139
140
# File 'lib/amee/item_value_definition.rb', line 138

def self.xmlpathpreamble
  "/Resources//ItemValueDefinition/"
end

Instance Method Details

#clear_usages!Object



58
59
60
# File 'lib/amee/v3/item_value_definition.rb', line 58

def clear_usages!
  @usages = {}
end

#compulsory?(usage) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/amee/v3/item_value_definition.rb', line 72

def compulsory?(usage)
  usage_type(usage) == :compulsory
end

#data?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/amee/item_value_definition.rb', line 87

def data?
  @from_data
end

#drill?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/amee/item_value_definition.rb', line 91

def drill?
  @drill
end

#expire_cacheObject



92
93
94
95
# File 'lib/amee/v3/item_value_definition.rb', line 92

def expire_cache
  @connection.expire_matching("/#{AMEE::Connection.api_version}/definitions/#{itemdefuid}/values/#{uid}.*")
  expire_cache_without_v3
end

#expire_cache_without_v3Object



91
# File 'lib/amee/v3/item_value_definition.rb', line 91

alias_method :expire_cache_without_v3, :expire_cache

#forbidden?(usage) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/amee/v3/item_value_definition.rb', line 75

def forbidden?(usage)
  usage_type(usage) == :forbidden
end

#full_pathObject



235
236
237
# File 'lib/amee/item_value_definition.rb', line 235

def full_path
  "/definitions/itemDefinitions/#{itemdefuid}/itemValueDefinitions/#{uid}"
end

#ignored?(usage) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/amee/v3/item_value_definition.rb', line 78

def ignored?(usage)
  usage_type(usage) == :ignored
end

#initialize_without_v3ItemValueDefinition

Returns a new instance of ItemValueDefinition.

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/amee/v3/item_value_definition.rb', line 9

def initialize(data = {})
  @itemdefuid=data[:itemdefuid]
  @name = data[:name]
  @uid = data[:uid]
  @path = data[:path]
  @unit = data[:unit]
  @perunit = data[:perunit]
  @valuetype = data[:valuetype]
  @default = data[:default] == "" ? nil : data[:default]
  @choices = data[:choices] || []
  @versions = data[:versions] || []
  @drill = data[:drill]
  @from_data = @drill ? false : data[:from_data] # Drills are also data, but we don't want them to show as such for clarity
  @from_profile = data[:from_profile]
  super
end

#metapathObject



15
16
17
# File 'lib/amee/v3/item_value_definition.rb', line 15

def metapath
  "/#{AMEE::Connection.api_version}/definitions/#{itemdefuid}/values/#{uid};wikiDoc;usages"
end

#metaxmlpathpreambleObject



18
19
20
# File 'lib/amee/v3/item_value_definition.rb', line 18

def metaxmlpathpreamble
  '/Representation/ItemValueDefinition/'
end

#optional?(usage) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/amee/v3/item_value_definition.rb', line 69

def optional?(usage)
  usage_type(usage) == :optional
end

#parsemetaObject



26
27
28
29
30
31
32
33
# File 'lib/amee/v3/item_value_definition.rb', line 26

def parsemeta
  data = {}
  data[:wikidoc]=x 'WikiDoc', :meta => true
  names = x('Usages/Usage/Name', :meta => true)
  types = x('Usages/Usage/Type', :meta => true)
  data[:usages] = names && types ? Hash[*(names.zip(types.map{|x| x.downcase.to_sym})).flatten] : {}
  storemeta(data)
end

#profile?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/amee/item_value_definition.rb', line 83

def profile?
  @from_profile
end

#putmeta_argsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/amee/v3/item_value_definition.rb', line 34

def putmeta_args
  str = <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<ItemValueDefinition>
  <WikiDoc>#{meta.wikidoc}</WikiDoc>
  <Usages>
EOF
  @usages.keys.sort.each do |key|
    str += <<EOF
    <Usage>
<Name>#{key}</Name>
<Type>#{@usages[key].to_s.upcase}</Type>
    </Usage>
EOF
  end
  str += <<EOF
  </Usages>
</ItemValueDefinition>
EOF
  {:body => str, :content_type => :xml}
end

#reload(connection) ⇒ Object



187
188
189
# File 'lib/amee/item_value_definition.rb', line 187

def reload(connection)
  ItemValueDefinition.load(connection,itemdefuid,uid)
end

#set_usage_type(usage, type) ⇒ Object



61
62
63
64
# File 'lib/amee/v3/item_value_definition.rb', line 61

def set_usage_type(usage, type)
  loadmeta if @usages.nil?
  @usages[usage] = type
end

#storemeta(options) ⇒ Object



21
22
23
24
25
# File 'lib/amee/v3/item_value_definition.rb', line 21

def storemeta(options)
  @meta=OpenStruct.new
  @meta.wikidoc= options[:wikidoc]
  @usages = options[:usages] || []
end

#usage_type(usage) ⇒ Object



65
66
67
68
# File 'lib/amee/v3/item_value_definition.rb', line 65

def usage_type(usage)
  loadmeta if @usages.nil?
  @usages[usage] || :optional
end

#usagesObject



55
56
57
# File 'lib/amee/v3/item_value_definition.rb', line 55

def usages
  @usages.keys
end