Class: TypeEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/simpex/type_entry.rb

Direct Known Subclasses

Catalog

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, values) ⇒ TypeEntry

Returns a new instance of TypeEntry.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/simpex/type_entry.rb', line 7

def  initialize(type, values)
  raise ArgumentError.new("Type must be a Type") unless type.kind_of? Type
  raise ArgumentError.new("Values must be a hash or an array") if (!values.kind_of?(Hash) ) && !values.kind_of?(Array)

  @type = type

  @values = @type.attributes.inject({}) do |result, att|
    result[att] = nil
    result
  end 

  if values.kind_of?(Hash)
    values.each do |key, value|
      real_att_name = guess_attribute(key)
      if real_att_name
        set(real_att_name, value)
      end
    end
  else 
    if @type.attributes.size < values.size
      raise ArgumentError.new "The number of values for the type entry is more (#{values.size}) then defined (#{@type.attributes.size}), inside the type '#{type.name}' following attributes are expected #{type.attributes.inspect}" 
    end
    if @type.attributes.size > values.size
      raise ArgumentError.new "The number of values for the type entry is less (#{values.size}) then defined (#{@type.attributes.size}), inside the type '#{type.name}' following attributes are expected #{type.attributes.inspect}" 
    end
    @type.attributes.each_with_index do |att,index|
      set(att, values[index])
    end
  end
  type << self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ Object



48
49
50
# File 'lib/simpex/type_entry.rb', line 48

def method_missing(id, *args)
  self.get("#{id}")
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/simpex/type_entry.rb', line 5

def type
  @type
end

Instance Method Details

#cat_ver_specific(attr_name) ⇒ Object



68
69
70
# File 'lib/simpex/type_entry.rb', line 68

def cat_ver_specific(attr_name)
  self.get(attr_name) + ":" + self.get("catalogVersion")
end

#get(att_name) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/simpex/type_entry.rb', line 39

def get(att_name)
  attr_names = @values.keys
  if attr_names.include?(att_name)
    @values[att_name]
  else
    @values[guess_attribute(att_name)]
  end
end

#set(key, value) ⇒ Object



52
53
54
# File 'lib/simpex/type_entry.rb', line 52

def set(attributes)
  @values.merge!(attributes)
end

#to_impObject



64
65
66
# File 'lib/simpex/type_entry.rb', line 64

def to_imp
  impexify(@values.values)
end