Sha256: a6d2cf4976260fcd70e9f521a6dd35db026b4c079c18be92c987ff6fdd804e0d

Contents?: true

Size: 1017 Bytes

Versions: 11

Compression:

Stored size: 1017 Bytes

Contents

module Soybean
  class ComplexType
    class_attribute :attributes, :instance_reader => true, :instance_writer => true
    self.attributes = []

    class << self
      def attr_accessor(*attrs)
        self.attributes += attrs
        super
      end
    end

    def initialize(*args)
      hash = args.extract_options!
      if args.empty?
        init_from_hash(hash)
      else
        init_from_array(args)
      end
    end

    private

    # @param hash [Hash]
    def init_from_hash(hash)
      check_arguments_number!(hash)
      hash.each do |key, val|
        self.send key.to_sym, val
      end
    end

    # @param arry [Array]
    def init_from_array(arry)
      check_arguments_number! arry
      attributes.each_with_index do |key, i|
        self.send "#{key}=".to_sym, arry.at(i)
      end
    end

    def check_arguments_number!(args)
      if args.size != attributes.size
        raise ArgumentError, "wrong number of arguments(#{args.size} for #{attributes.size})"
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
soybean-2.2.2 lib/soybean/complex_type.rb
soybean-2.2.1 lib/soybean/complex_type.rb
soybean-2.2.0 lib/soybean/complex_type.rb
soybean-2.1.5 lib/soybean/complex_type.rb
soybean-2.1.4 lib/soybean/complex_type.rb
soybean-2.1.3 lib/soybean/complex_type.rb
soybean-2.1.2 lib/soybean/complex_type.rb
soybean-2.1.1 lib/soybean/complex_type.rb
soybean-2.1.0 lib/soybean/complex_type.rb
soybean-2.0.1 lib/soybean/complex_type.rb
soybean-2.0.0 lib/soybean/complex_type.rb