Class: Docgenerator::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/docgenerator/attribute.rb

Overview

Administrate attributes for elements.

Attributes are the first parameters of element:

element( :type, {}, 'content' )
Element.new( {} )

Constant Summary collapse

@@settings =
{}
@@values =
{}
@@sortkey =

Just a counter to get identic positions between different program runs.

99

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, element) ⇒ Attribute

Each attribute is assigned to an element.



76
77
78
79
80
81
82
83
84
# File 'lib/docgenerator/attribute.rb', line 76

def initialize( name, element )
  @name  = name
  @element  = element
  @attr_content  = []
  @part_of    = [element]  #list of elements, where this element is part of (normaly only one element)
  @part_of_doc  = []      #list of documents, where this element is part of (normaly only one document)
  #@part_of_doc  = element.part_of_doc  May not work - is empty to generation time
              
end

Instance Attribute Details

#nameObject (readonly)

Key of the attribute



86
87
88
# File 'lib/docgenerator/attribute.rb', line 86

def name
  @name
end

Class Method Details

.create(settings = [], values = nil, sortkey = (@@sortkey += 1 )) ⇒ Object

Generic creation of a new class to define a new Atttribute. Settings is a hash with generell settings.

Supported parameters:

  • :required

  • :content adding values to this attribute add it to the content.

  • :values allowed values

  • :html used for html-output

  • :latex used for latex-output

  • :texkeyval used for latex-output with keyval-syntax.

The sortkey is used for a sort-sequence of attributes inside an element



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
# File 'lib/docgenerator/attribute.rb', line 36

def  self.create( settings = [], values = nil, sortkey = (@@sortkey += 1 ) )
  #First some checks
  if ! settings.kind_of?( Array )
    raise "Type error Attribute.create: Expected Array, get #{attr.class}"
  end
  #Generic class creation
  attributeclass = Class.new( Attribute )
  #Set the flags.
  settings.each{|setting|
    case setting
    when :required
      attributeclass.class_eval('def required?(); true; end')
    when :content
      attributeclass.class_eval('def content?();  true; end')
    when :unique
      attributeclass.class_eval('def unique?();  true; end')        
    when :html
      attributeclass.class_eval('def html?();  true; end')
    when :latex
      attributeclass.class_eval('def latex?();  true; end')
    when :texkeyval
      attributeclass.class_eval('def texkeyval?();  true; end')
    when :text
      attributeclass.class_eval('def text?();  true; end')
    when :wiki
      attributeclass.class_eval('def wiki?();  true; end')
    when :creole
      attributeclass.class_eval('def creole?();  true; end')
    else
      DOCGENERATOR_LOGGER.error("Attribute: Unknown setting <#{setting.inspect}>") if DOCGENERATOR_LOGGER.error?
    end
  }
  if values
    attributeclass.class_eval("def allowed_values();  return #{values.inspect}; end")
  end  
  attributeclass.class_eval("def sortkey();  return #{sortkey}; end")
  return attributeclass
end

Instance Method Details

#<<(value) ⇒ Object

Add a value to the attribute content.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/docgenerator/attribute.rb', line 146

def << ( value )
  if ! self.allowed?( value )
    @element.log.warn("Attribute '#{@name}': Incorrect value '#{value.inspect}' for #{@element.inspect}") if @element.log.warn?
  end
  if unique? and @attr_content.size > 0
    @element.log.warn("Attribute #{@name}: More then one value added to #{@element.inspect} (#{value.inspect})") if @element.log.warn?
    return self
  end
  if content?
    @element << value
  else
    @attr_content << value
  end
end

#allowed?(value) ⇒ Boolean

Check if the given value is allowed.

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/docgenerator/attribute.rb', line 129

def allowed?( value )
  return true if allowed_values() == nil
  allowed_values().each{|v|
    if value == v #same value
      return true
    elsif v.is_a?(Regexp) and value =~ v
      return true
    elsif v.class == Class and value.is_a?( v ) #same class
      return true
    end
    #Check next allowed value.
  }
  #No allowed value is set.
  return false
  #~ return allowed_values().include?( value )
end

#allowed_valuesObject

Return all allowed values. Redefined by Attribute.create and used in Attributes.<< By default everything is allowed.

This method must return an Array with the allowed values or classes.



125
126
127
# File 'lib/docgenerator/attribute.rb', line 125

def allowed_values()
  return nil
end

#contentObject

Sometimes the content of an attribute is needed directly.



98
# File 'lib/docgenerator/attribute.rb', line 98

def content();  @attr_content; end

#content?Boolean

Check if the attribute can contain a “content”. Redefined by Attribute.create.

Returns:

  • (Boolean)


96
# File 'lib/docgenerator/attribute.rb', line 96

def content?();  false; end

#context?Boolean

Attribute is used for conTeXt. Only for informational reasons.

Returns:

  • (Boolean)


116
# File 'lib/docgenerator/attribute.rb', line 116

def context?();  false; end

#creole?Boolean

Attribut is used for Creole.

Returns:

  • (Boolean)


110
# File 'lib/docgenerator/attribute.rb', line 110

def creole?();  false; end

#filled?Boolean

Check if content was added

Returns:

  • (Boolean)


102
# File 'lib/docgenerator/attribute.rb', line 102

def filled?();  return @attr_content.compact.size > 0; end

#html?Boolean

Attribut is used for HTML.

Returns:

  • (Boolean)


104
# File 'lib/docgenerator/attribute.rb', line 104

def html?();  false; end

#inspectObject

since Ruby 1.9



165
166
167
# File 'lib/docgenerator/attribute.rb', line 165

def inspect()
  return "<#Attribute #{@name} #{@attr_content.inspect} (in #{@element.inspect}) >"
end

#latex?Boolean

Attribute is used for latex. Only for informational reasons.

Returns:

  • (Boolean)


113
# File 'lib/docgenerator/attribute.rb', line 113

def latex?();  false; end

#required?Boolean

Check if the attribute is required. Redefined by Attribute.create and used in Element.to_s.

Returns:

  • (Boolean)


93
# File 'lib/docgenerator/attribute.rb', line 93

def required?();  false; end

#settingsObject

Return class settings. Can be used for some checks.



88
89
90
# File 'lib/docgenerator/attribute.rb', line 88

def settings()
  @@settings
end

#texkeyval?Boolean

Attribute is used as a keyval-option. See also Element#texkeyval

Returns:

  • (Boolean)


119
# File 'lib/docgenerator/attribute.rb', line 119

def texkeyval?();  false; end

#text?Boolean

Attribut is used for Text.

Returns:

  • (Boolean)


106
# File 'lib/docgenerator/attribute.rb', line 106

def text?();  false; end

#to_sObject

Return the content.



161
162
163
164
# File 'lib/docgenerator/attribute.rb', line 161

def to_s()
  #~ @attr_content.to_s() #Ruby 1.8
  @attr_content.join()  #since Ruby 1.9
end

#unique?Boolean

Values may be added only once.

Returns:

  • (Boolean)


100
# File 'lib/docgenerator/attribute.rb', line 100

def unique?();  false; end

#wiki?Boolean

Attribut is used for Wiki.

Returns:

  • (Boolean)


108
# File 'lib/docgenerator/attribute.rb', line 108

def wiki?();  false; end