Sha256: 405f3a37d160d00febded245383aa845dc428adefe5edfda87b86afe3905a234

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module OAPI::Properties
  module ClassMethods
    attr_reader :properties

    def property(name, object = nil) # rubocop:disable Metrics/MethodLength
      @properties ||= {}
      @properties[name] = object

      define_method(name) do |value = nil, ref: nil, &block|
        if object.nil?
          raise ArgumentError, "value must be passed" if value.nil?

          return instance_variable_set(:"@#{name}", value)
        end

        if [NilClass, object, OAPI::Ref].none? { value.is_a?(_1) }
          raise ArgumentError, "given value must be of type #{object} or #{OAPI::Ref} given: #{value}"
        end
        raise ArgumentError, "ref and block are mutual exclusive" if ref && block

        instance_variable_set(:"@#{name}", parse_property_value(value, object, ref, block))
      end
    end
  end

  class << self
    def included(base)
      base.extend(ClassMethods)
    end
  end

  def initialize(&)
    instance_exec(&) if block_given?
  end

  def properties
    self.class.properties.keys.each_with_object({}) do |prop, acc|
      acc[prop] = instance_variable_get(:"@#{prop}")
    end
  end

  def parse_property_value(value, object, ref, block)
    if ref
      OAPI::Ref.new(ref)
    elsif block
      object.new(&block)
    elsif !value.nil?
      value
    else
      raise ArgumentError, "ref, block or value must be provided"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
oapi-0.1.3 lib/oapi/properties.rb
oapi-0.1.2 lib/oapi/properties.rb
oapi-0.1.1 lib/oapi/properties.rb
oapi-0.1.0 lib/oapi/properties.rb