lib/never_bounce/api/response/container.rb
# Build a generic scalar oattr.
#
#   scalar_oattr :first_name
#   scalar_oattr :page, type: :integer
#   scalar_oattr :total_valid, type: :integer, allow_nil: true
#
# @return [Symbol]
Build a generic scalar oattr.
Build a generic scalar oattr .
def scalar_oattr(name, options = {})
        o, options = {}, options.dup
        o[k = :allow_nil] = options.include?(k) ? options.delete(k) : false
        o[k = :key] = options.delete(k) || name
        o[k = :type] = options.delete(k) || :any
        raise ArgumentError, "Unknown options: #{options.inspect}" if not options.empty?
        code = []
        code << %{attr_writer :#{name}}
        code << case o[:type]
          when :any
            %{def #{name}; @#{name} ||= body_hash.fetch("#{o[:key]}"); end}
          when :float
            if o[:allow_nil]
              %{
                def #{name}
                  @#{name} ||= unless (v = body_hash.fetch("#{o[:key]}")).nil?
                    Float(v)
                  end
                end
              }
            else
              %{def #{name}; @#{name} ||= Float(body_hash.fetch("#{o[:key]}")); end}
            end
          when :integer
            if o[:allow_nil]
              %{
                def #{name}
                  @#{name} ||= unless (v = body_hash.fetch("#{o[:key]}")).nil?
                    Integer(v)
                  end
                end
              }
            else
              %{def #{name}; @#{name} ||= Integer(body_hash.fetch("#{o[:key]}")); end}
            end
          else
            raise ArgumentError, "Unknown type: #{o[:type].inspect}"
          end
        class_eval code.join(";")
        name
      end
def scalar_oattr ( name , options = { } ) o , options = { } , options . dup o [ k = :allow_nil ] = options . include? ( k ) ? options . delete ( k ) : false o [ k = :key ] = options . delete ( k ) || name o [ k = :type ] = options . delete ( k ) || :any raise ArgumentError , "Unknown options: #{options.inspect}" if not options . empty? code = [ ] code << %{attr_writer :#{name}} code << case o [ :type ] when :any %{def #{name}; @#{name} ||= body_hash.fetch("#{o[:key]}"); end} when :float if o [ :allow_nil ] %{
                def #{name}
                  @#{name} ||= unless (v = body_hash.fetch("#{o[:key]}")).nil?
                    Float(v)
                  end
                end
              } else %{def #{name}; @#{name} ||= Float(body_hash.fetch("#{o[:key]}")); end} end when :integer if o [ :allow_nil ] %{
                def #{name}
                  @#{name} ||= unless (v = body_hash.fetch("#{o[:key]}")).nil?
                    Integer(v)
                  end
                end
              } else %{def #{name}; @#{name} ||= Integer(body_hash.fetch("#{o[:key]}")); end} end else raise ArgumentError , "Unknown type: #{o[:type].inspect}" end class_eval code . join ( ";" ) name end
Build a generic scalar oattr.
Build a generic scalar oattr .
None
Symbol
name
options