Sha256: 1e355f68610706d7052d324b3220557d0ae58fafa8479e95a8881d988780e505

Contents?: true

Size: 1.26 KB

Versions: 11

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module IronBank
  module Describe
    # Describe a field in Zuora: name, label, type, etc.
    #
    class Field
      private_class_method :new

      TEXT_VALUES = %i[
        name
        label
        type
      ].freeze

      PLURAL_VALUES = %i[
        options
        contexts
      ].freeze

      BOOLEAN_VALUES = %i[
        selectable
        createable
        updateable
        filterable
        custom
        required
      ].freeze

      def self.from_xml(doc)
        new(doc)
      end

      # Defined separately because the node name is not ruby-friendly
      def max_length
        doc.at_xpath('.//maxlength').text.to_i
      end

      TEXT_VALUES.each do |val|
        define_method(val) { doc.at_xpath(".//#{val}").text }
      end

      PLURAL_VALUES.each do |val|
        singular = val.to_s.chop
        define_method(val) { doc.xpath(".//#{val}/#{singular}").map(&:text) }
      end

      BOOLEAN_VALUES.each do |val|
        define_method(:"#{val}?") { doc.at_xpath(".//#{val}").text == 'true' }
      end

      def inspect
        "#<#{self.class}:0x#{(object_id << 1).to_s(16)} #{name} (#{type})>"
      end

      private

      attr_reader :doc

      def initialize(doc)
        @doc = doc
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
iron_bank-2.2.0 lib/iron_bank/describe/field.rb
iron_bank-2.1.0 lib/iron_bank/describe/field.rb
iron_bank-2.0.2 lib/iron_bank/describe/field.rb
iron_bank-2.0.1 lib/iron_bank/describe/field.rb
iron_bank-2.0.0 lib/iron_bank/describe/field.rb
iron_bank-1.0.4 lib/iron_bank/describe/field.rb
iron_bank-1.0.3 lib/iron_bank/describe/field.rb
iron_bank-1.0.2 lib/iron_bank/describe/field.rb
iron_bank-1.0.1 lib/iron_bank/describe/field.rb
iron_bank-1.0.0 lib/iron_bank/describe/field.rb
iron_bank-0.7.1 lib/iron_bank/describe/field.rb