Class: Type

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type_attributes, macros = []) ⇒ Type

Returns a new instance of Type.

Raises:

  • (ArgumentError)


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
38
39
40
41
42
# File 'lib/simpex/type.rb', line 8

def  initialize(name, type_attributes, macros=[])
  raise "Type name was not given, use e.g. 'Product'  or 'Category'" if name.empty?
  raise ArgumentError.new("Attribute values must be given in an array, use '%w{code name description catalog}'") unless type_attributes.kind_of?(Array)
  raise "Macros was given but is nil" if macros.nil?

  if macros.kind_of? String
    @macros = [macros]
  else
    @macros = macros
  end
  @attributes = []
  type_attributes.each do |attribute|
    next if attribute.nil? || attribute.empty?
    if attribute.kind_of?(Hash)
      @attributes += resolve_to_attributes(attribute)
    else
      @attributes << attribute
      if attribute =~ /^\$/
        if @macros.empty?
          raise ArgumentError.new "In the type '#{name}', you are using a macro but no macros are given at all, pass the macros as an array to the type constructor." 
        end
        if @macros.none?{|m| m.split("=").first == attribute.split("[").first}
          raise ArgumentError.new "In the type '#{name}', you are using the macro '#{attribute}' that is not defined, defined macros are #{@macros.inspect}" 
        end
      end
    end
  end
  @name = name 
  @entries = []
  @impex_command = "INSERT_UPDATE"
  @impex_result = nil
  @memory_safe = false
  @after_each = []
  @before_each = []
end

Instance Attribute Details

#after_eachObject

Returns the value of attribute after_each.



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

def after_each
  @after_each
end

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#entriesObject (readonly)

Returns the value of attribute entries.



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

def entries
  @entries
end

#impex_commandObject

Returns the value of attribute impex_command.



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

def impex_command
  @impex_command
end

#impex_resultObject

Returns the value of attribute impex_result.



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

def impex_result
  @impex_result
end

#macrosObject (readonly)

Returns the value of attribute macros.



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

def macros
  @macros
end

#memory_safe=(value) ⇒ Object (writeonly)

Sets the attribute memory_safe

Parameters:

  • value

    the value to set the attribute memory_safe to.



6
7
8
# File 'lib/simpex/type.rb', line 6

def memory_safe=(value)
  @memory_safe = value
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#<<(entry) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/simpex/type.rb', line 44

def <<(entry)
  @entries << entry
  if @impex_result 
    @impex_result.add_entries_number(1)
  elsif @memory_safe
    raise "impex result object is not set, but we are running in a memory safe impex generation"
  end
end

#empty?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/simpex/type.rb', line 53

def empty?
  self.entries.empty?
end

#find_by(attribute, value) ⇒ Object



89
90
91
# File 'lib/simpex/type.rb', line 89

def find_by(attribute, value)
  self.entries.find_all{|entry| entry.send(attribute) == value}
end

#to_imp(macros = true) ⇒ Object



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
# File 'lib/simpex/type.rb', line 57

def to_imp(macros=true)
  result = ""
  return result if @entries.empty?
  if (!@macros.empty? && macros)
    @macros.each do |macro|
      result << macro 
      result << "\n" 
    end
    result << "\n" 
  end
  result << "#{@impex_command} #{@name}" +  impexify(@attributes)
  unless after_each.empty?
    result << "\"#%afterEach:\n" 
    after_each.each do |bash_line|
      result << bash_line << "\n"
    end
    result << "\"\n" 
  end
  @entries.each do |entry|
    result << entry.to_imp
  end
  result << "\n" 
  unless after_each.empty?
    result << "\"#%afterEach:end\"" 
  end
  result
end

#unregister_by(attribute, value) ⇒ Object



85
86
87
# File 'lib/simpex/type.rb', line 85

def unregister_by(attribute, value)
  self.entries.reject!{|entry| entry.send(attribute) == value}
end