Sha256: 3b60238c7a9611e74039510372bcb178c296a17b4be1a0c03f772464db93c11e

Contents?: true

Size: 956 Bytes

Versions: 5

Compression:

Stored size: 956 Bytes

Contents

module Lutaml
  module Model
    class Attribute
      attr_reader :name, :type, :options

      def initialize(name, type, options = {})
        @name = name
        @type = cast_type(type)

        @options = options

        if collection? && !options[:default]
          @options[:default] = -> { [] }
        end
      end

      def cast_type(type)
        case type
        when Class
          type
        when String
          Type.const_get(type)
        when Symbol
          Type.const_get(type.to_s.split("_").collect(&:capitalize).join)
        end
      rescue NameError
        raise ArgumentError, "Unknown Lutaml::Model::Type: #{type}"
      end

      def collection?
        options[:collection] || false
      end

      def default
        return options[:default].call if options[:default].is_a?(Proc)

        options[:default]
      end

      def render_nil?
        options.fetch(:render_nil, false)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lutaml-model-0.3.6 lib/lutaml/model/attribute.rb
lutaml-model-0.3.5 lib/lutaml/model/attribute.rb
lutaml-model-0.3.4 lib/lutaml/model/attribute.rb
lutaml-model-0.3.3 lib/lutaml/model/attribute.rb
lutaml-model-0.3.2 lib/lutaml/model/attribute.rb