Class: Nfe::Helpers::XmlModel::AttributeBase

Inherits:
Object
  • Object
show all
Defined in:
lib/nfe/helpers/xml_model/attribute_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ AttributeBase

Returns a new instance of AttributeBase.



7
8
9
10
11
12
13
14
15
# File 'lib/nfe/helpers/xml_model/attribute_base.rb', line 7

def initialize(name, options={})
  @name = name

  @type = options[:type] || :text
  @required = options[:required]
  @format = options[:format]
  @klass = options[:klass]
  @xml_alias = (options[:xml_alias] || @name).to_s
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



5
6
7
# File 'lib/nfe/helpers/xml_model/attribute_base.rb', line 5

def format
  @format
end

#klassObject

Returns the value of attribute klass.



5
6
7
# File 'lib/nfe/helpers/xml_model/attribute_base.rb', line 5

def klass
  @klass
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/nfe/helpers/xml_model/attribute_base.rb', line 5

def name
  @name
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/nfe/helpers/xml_model/attribute_base.rb', line 5

def type
  @type
end

#xml_aliasObject

Returns the value of attribute xml_alias.



5
6
7
# File 'lib/nfe/helpers/xml_model/attribute_base.rb', line 5

def xml_alias
  @xml_alias
end

Instance Method Details

#add_to_xml(context, xml, attribute) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/nfe/helpers/xml_model/attribute_base.rb', line 22

def add_to_xml(context, xml, attribute)
  attribute = attr_accessor_value(context, attribute)

  if !attribute.present?
    xml.tag!(xml_alias) if required?
  else
    add_tag(xml, attribute)
  end
end

#parse(context, doc) ⇒ Object



17
18
19
20
# File 'lib/nfe/helpers/xml_model/attribute_base.rb', line 17

def parse(context, doc)
  value = xml_value(doc)
  context.send("#{name}=", get_value(value)) if value.present?
end