Class: ScaffoldParser::Scaffolders::XSD::Parser::Handlers::ListMethod

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

Instance Attribute Summary collapse

Attributes included from BaseMethod

#source

Instance Method Summary collapse

Methods included from Utils

#indent, #indent_string, #single_quote, #wrap_in_namespace

Methods included from BaseMethod

#==, #method_name, #to_s

Constructor Details

#initialize(source) {|_self| ... } ⇒ ListMethod

Returns a new instance of ListMethod.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
17
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb', line 12

def initialize(source)
  @source = source
  @at = [[source.xmlns_prefix, "#{source.name}"].compact.join(':')]

  yield self if block_given?
end

Instance Attribute Details

#atObject

Returns the value of attribute at.



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

def at
  @at
end

#item_classObject

Returns the value of attribute item_class.



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

def item_class
  @item_class
end

Instance Method Details

#all(_) ⇒ Object



66
67
68
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb', line 66

def all(_)
  self
end

#choice(_) ⇒ Object



62
63
64
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb', line 62

def choice(_)
  self
end

#complex_type(source) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb', line 70

def complex_type(source)
  if source.has_name?
    STACK.push Klass.new(source, [self])
  else
    self
  end
end

#element(source) ⇒ Object



78
79
80
81
82
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb', line 78

def element(source)
  if source.has_name?
    to_proxy_list(source, source.name)
  end
end

#extension(source) ⇒ Object



84
85
86
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb', line 84

def extension(source)
  Extension.new(self, source.attributes)
end

#method_bodyObject



19
20
21
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb', line 19

def method_body
  "array_of_at(#{item_class}, #{single_quote(at)})"
end

#name_with_prefixObject



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

def name_with_prefix
  [source.xmlns_prefix, "#{source.name}"].compact.join(':')
end

#sequence(_) ⇒ Object



58
59
60
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb', line 58

def sequence(_)
  self
end

#to_builderObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb', line 35

def to_builder
  f = StringIO.new

  f.puts "if data.key? :#{method_name}"
  if item_class == 'String'
    f.puts "  data[:#{method_name}].map { |i| Ox::Element.new('#{at.first}') << i }.each { |i| root << i }"
  else
    f.puts "  data[:#{method_name}].each { |i| root << #{item_class}.new('#{at.first}', i).builder }"
  end
  f.puts 'end'

  f.string.strip
end

#to_h_methodObject



27
28
29
30
31
32
33
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb', line 27

def to_h_method
  if item_class == 'String'
    "hash[:#{method_name}] = #{method_name} if has? '#{name_with_prefix}'"
  else
    "hash[:#{method_name}] = #{method_name}.map(&:to_h) if has? '#{name_with_prefix}'"
  end
end

#to_proxy_list(source, path) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/scaffold_parser/scaffolders/xsd/parser/handlers/list_method.rb', line 49

def to_proxy_list(source, path)
  ProxyListMethod.new(source) do |m|
    prefixed_path = [source.xmlns_prefix, "#{source.name}"].compact.join(':')

    m.at = [prefixed_path] + @at
    m.item_class = @item_class
  end
end