Class: ScaffoldParser::Scaffolders::XSD::Parser::Handlers::Module

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.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, methods = []) {|_self| ... } ⇒ Module

Returns a new instance of Module.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb', line 13

def initialize(source = nil, methods = [])
  @name = "Groups::#{source.name.camelize}"
  @methods = methods

  @namespace = source.xmlns_prefix&.camelize

  yield self if block_given?
end

Instance Attribute Details

#includesObject

Returns the value of attribute includes.



11
12
13
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb', line 11

def includes
  @includes
end

#inherit_fromObject

Returns the value of attribute inherit_from.



11
12
13
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb', line 11

def inherit_from
  @inherit_from
end

#methodsObject

Returns the value of attribute methods.



11
12
13
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb', line 11

def methods
  @methods
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb', line 11

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



11
12
13
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb', line 11

def namespace
  @namespace
end

Instance Method Details

#name_with_prefixObject



26
27
28
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb', line 26

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

#schema(_) ⇒ Object



22
23
24
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb', line 22

def schema(_)
  STACK
end

#to_builder_sObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb', line 60

def to_builder_s
  string =
    ModuleTemplate.new(name.demodulize) do |template|
      template.namespaces = ['Groups'].compact

      meth = StringIO.new
      meth.puts "  def builder"
      meth.puts "    root = Ox::Element.new(name)"
      meth.puts "    root = add_attributes_and_namespaces(root)"
      meth.puts
      meth.puts methods.map { |method| indent(indent(method.to_builder.lines)).join  }.join("\n")
      meth.puts
      meth.puts "    root"
      meth.puts "  end"

      template.methods = [meth.string]
    end.to_s

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

  wrapped
end

#to_sObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/module.rb', line 30

def to_s
  string =
    ModuleTemplate.new(name.demodulize) do |template|
      template.namespaces = ['Groups'].compact

      methods.each { |method| template.methods << indent(method.to_s.lines).join  }

      meth = StringIO.new
      meth.puts "  def to_h"
      meth.puts "    hash = {}"
      meth.puts "    hash[:attributes] = attributes"
      meth.puts
      methods.each do |method|
        method.to_h_method.lines.each do |line|
          meth.puts "    #{line}"
        end
      end
      meth.puts
      meth.puts "    hash"
      meth.puts "  end"

      template.methods << meth.string
    end.to_s

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

  wrapped
end