Sha256: 17184dcdae5cb840bde756221242d889754a83f9e31edf91f5388add44c1ced0

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

module ScaffoldParser
  module Scaffolders
    class XSD
      class Parser
        module Handlers
          class ProxyListMethod
            include BaseMethod
            include Utils

            attr_accessor :at, :item_class

            def initialize(source)
              @source = source
              yield self if block_given?
            end

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

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

            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

            def to_builder
              f = StringIO.new

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

              f.string.strip
            end

            def sequence(_)
              Sequence.new self
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
scaffold_parser-0.10.0 lib/scaffold_parser/scaffolders/xsd/parser/handlers/proxy_list_method.rb
scaffold_parser-0.9.1 lib/scaffold_parser/scaffolders/xsd/parser/handlers/proxy_list_method.rb
scaffold_parser-0.9.0 lib/scaffold_parser/scaffolders/xsd/parser/handlers/proxy_list_method.rb
scaffold_parser-0.8.0 lib/scaffold_parser/scaffolders/xsd/parser/handlers/proxy_list_method.rb