Class: BuhoCfdi::BaseNode

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/buho_cfdi/base_node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ BaseNode

Returns a new instance of BaseNode.



8
9
10
11
12
13
14
15
16
# File 'lib/buho_cfdi/base_node.rb', line 8

def initialize(params = {})
  @node_hash = {}

  params.each_pair do |key, value|
    if respond_to? key
      instance_variable_set("@#{key}", value)
    end
  end
end

Instance Attribute Details

#node_hashObject

Returns the value of attribute node_hash.



6
7
8
# File 'lib/buho_cfdi/base_node.rb', line 6

def node_hash
  @node_hash
end

Class Method Details

.attr_accessor(*args) ⇒ Object



77
78
79
80
81
82
# File 'lib/buho_cfdi/base_node.rb', line 77

def attr_accessor(*args)
  @params ||= []
  @params.concat(args)
  
  super(*args)
end

.build_child(params) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/buho_cfdi/base_node.rb', line 66

def build_child(params)
  node = self.new(params)

  if node.valid?
    self.class.send(:attr_reader, self.name.downcase.sub( '::', '_'))
    instance_variable_set("@#{self.name.downcase.sub( '::', '_')}", self.new)
  else
    nil
  end
end

.paramsObject



84
85
86
# File 'lib/buho_cfdi/base_node.rb', line 84

def params
  @params
end

Instance Method Details

#build_child(params) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/buho_cfdi/base_node.rb', line 37

def build_child(params)
  node = self.class.new(params)

  if node.valid?
    self.class.send(:attr_reader, self.class.name.downcase.sub( '::', '_'))
    instance_variable_set("@#{self.class.name.downcase.sub( '::', '_')}", self.class.new)
  else
    nil
  end
end

#build_child!(klass, params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/buho_cfdi/base_node.rb', line 26

def build_child!(klass, params)
  node = klass.new(params)

  if node.valid?
    self.class.send(:attr_reader, klass.name.downcase.sub( '::', '_'))
    instance_variable_set("@#{klass.name.downcase.sub( '::', '_')}", node)
  else
    nil
  end
end

#build_children(klass) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/buho_cfdi/base_node.rb', line 48

def build_children(klass)
  self.class.send(:attr_reader, klass.name.downcase.sub( '::', '_'))

  instance_variable_set(
    "@#{klass.name.downcase.sub( '::', '_')}",
    ::Nodes::Collection.new(klass)
  )

  self
end

#to_hashObject



18
19
20
21
22
23
24
# File 'lib/buho_cfdi/base_node.rb', line 18

def to_hash
  self.class.params.each do |param|
    node_hash[define_locale(param)] = send(param) unless send(param).blank?
  end

  node_hash
end