Class: ScaffoldParser::Scaffolders::XSD::Parser::Handlers::Klass

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#indent, #indent_string, #single_quote, #wrap_in_namespace

Constructor Details

#initialize(source = nil, elements = []) {|_self| ... } ⇒ Klass

Returns a new instance of Klass.

Yields:

  • (_self)

Yield Parameters:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb', line 11

def initialize(source = nil, elements = [])
  @name = source&.name&.camelize
  @namespace = source.xmlns_prefix&.camelize

  includes, methods = [*elements].partition do |e|
    e.is_a? ModuleInclude
  end
  inherits, methods = methods.partition do |e|
    e.is_a? ClassInherit
  end

  @methods = methods
  @includes = includes
  @inherit_from = inherits.first.base if inherits.any?

  yield self if block_given?
end

Instance Attribute Details

#includesObject

Returns the value of attribute includes.



9
10
11
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb', line 9

def includes
  @includes
end

#inherit_fromObject

Returns the value of attribute inherit_from.



9
10
11
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb', line 9

def inherit_from
  @inherit_from
end

#methodsObject

Returns the value of attribute methods.



9
10
11
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb', line 9

def methods
  @methods
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb', line 9

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



9
10
11
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb', line 9

def namespace
  @namespace
end

Instance Method Details

#==(other) ⇒ Object



37
38
39
40
41
42
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb', line 37

def ==(other)
  name == other.name &&
    namespace == other.namespace &&
    methods == other.methods &&
    inherit_from == other.inherit_from
end

#name_with_prefixObject



29
30
31
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb', line 29

def name_with_prefix
  [namespace, name].compact.map(&:camelize).join('::')
end

#schema(_) ⇒ Object



33
34
35
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb', line 33

def schema(_)
  STACK
end

#to_builder_sObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb', line 90

def to_builder_s
  f = StringIO.new

  if inherit_from
    i = inherit_from.split(':').compact.map(&:camelize).join('::')
    f.puts "class #{name} < #{i}"
  else
    f.puts "class #{name}"
  end
  f.puts "  include ParserCore::BaseBuilder"
  includes.each { |incl| f.puts "  include #{incl.full_ref}" }
  f.puts
  f.puts "  def builder"
  f.puts "    root = Ox::Element.new(name)"
  f.puts "    root = add_attributes_and_namespaces(root)"
  f.puts
  if inherit_from
    f.puts "    super.nodes.each do |n|"
    f.puts "      root << n"
    f.puts "    end"
    f.puts
  end

  if methods.any?
    f.puts methods.map { |method| indent(indent(method.to_builder.lines)).join  }.join("\n")
    f.puts
  end
  if includes.any?
    f.puts "    mega.each do |r|"
    f.puts "      r.nodes.each { |n| root << n }"
    f.puts "    end"
    f.puts
  end
  f.puts "    root"
  f.puts "  end"

  f.puts "end"

  string = f.string.strip

  wrapped = string
  wrapped = wrap_in_namespace(wrapped, namespace) if namespace

  wrapped
end

#to_sObject



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/klass.rb', line 44

def to_s
  f = StringIO.new

  if inherit_from
    i = inherit_from.split(':').compact.map(&:camelize).join('::')
    f.puts "class #{name} < #{i}"
  else
    f.puts "class #{name}"
  end
  f.puts "  include ParserCore::BaseParser"
  includes.each { |incl| f.puts "  include #{incl.full_ref}" }
  if methods.any? || includes.any?
    f.puts if methods.any?
    f.puts methods.map { |method| indent(method.to_s.lines).join  }.join("\n\n")
    f.puts if methods.any?
    f.puts "  def to_h"
    f.puts "    hash = {}"
    f.puts "    hash[:attributes] = attributes"
    f.puts
    methods.each do |method|
      method.to_h_method.lines.each do |line|
        f.puts "    #{line}"
      end
    end
    f.puts if methods.any?
    if includes.any?
      f.puts "    mega.inject(hash) { |memo, r| memo.merge r }"
    else
      f.puts "    hash"
    end
    if inherit_from
      f.puts "    super.merge(hash)"
    end
    f.puts "  end"
  end

  f.puts "end"

  string = f.string.strip

  wrapped = string
  wrapped = wrap_in_namespace(wrapped, namespace) if namespace

  wrapped
end